add a method for init native socket library

Fixed #6
This commit is contained in:
Kirigaya Kazuto 2018-09-09 16:11:28 +08:00
parent bd3bd376f6
commit 1c4e3e5f24
2 changed files with 29 additions and 16 deletions

View File

@ -56,27 +56,37 @@ using BYTE = unsigned char;
#include <stdexcept> #include <stdexcept>
#include <vector> #include <vector>
class _init_winsock2_2_class int InitNativeSocket()
{ {
public:
_init_winsock2_2_class()
{
myliblog("sockaddr %d sockaddr_in %d sockaddr_in6 %d\n", sizeof(sockaddr), sizeof(sockaddr_in), sizeof(sockaddr_in6)); myliblog("sockaddr %d sockaddr_in %d sockaddr_in6 %d\n", sizeof(sockaddr), sizeof(sockaddr_in), sizeof(sockaddr_in6));
/// Windows Platform need WinSock2.DLL initialization. /// Windows Platform need WinSock2.DLL initialization.
#ifdef _WIN32 #ifdef _WIN32
WORD wd; WORD wd;
WSAData wdt; WSAData wdt;
wd=MAKEWORD(2,2); wd = MAKEWORD(2, 2);
int ret=WSAStartup(wd,&wdt); int ret = WSAStartup(wd, &wdt);
myliblog("WSAStartup() Returns: %d\n",ret); myliblog("WSAStartup() Returns: %d\n", ret);
if(ret<0) if (ret < 0)
{ {
myliblog("WSAGetLastError: %d\n",WSAGetLastError()); myliblog("WSAGetLastError: %d\n", WSAGetLastError());
throw std::runtime_error("Unable to load winsock2.dll. "); return -1;
} }
#endif #endif
return 0;
}
class _init_winsock2_2_class
{
public:
_init_winsock2_2_class()
{
if (InitNativeSocket() < 0)
{
throw std::runtime_error("Unable to Initialize native socket libray.");
}
} }
~_init_winsock2_2_class() ~_init_winsock2_2_class()
{ {

View File

@ -10,6 +10,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
int InitNativeSocket();
enum enum
{ {
GSOCK_OK = 0, GSOCK_OK = 0,
@ -247,6 +249,7 @@ public:
// Call handle() to handle events // Call handle() to handle events
int wait(int timeout); int wait(int timeout);
// callback: void event_handler(vsock& s,int event)
void handle(const std::function<void(vsock&,int)>& callback); void handle(const std::function<void(vsock&,int)>& callback);
~epoll(); ~epoll();