-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNetlib.cpp
174 lines (145 loc) · 3.27 KB
/
Netlib.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "Netlib.h"
#include "BaseSocket.h"
#include "EventDispatch.h"
int netlib_init()
{
int ret = NETLIB_OK;
#ifdef _WIN32
WSADATA wsaData;
WORD wReqest = MAKEWORDK(1, 1);
if(WSAStartup(wRequest, &wsaData) != 0)
{
ret = NETLIB_ERROR;
}
#endif
return ret;
}
int netlib_destroy()
{
int ret = NETLIB_OK;
#ifdef _WIN32
if(WSACleanup() != 0)
{
ret = NETLIB_ERROR;
}
#endif
return ret;
}
int netlib_listen(
const char* server_ip,
uint16_t port,
callback_t callback,
void* callback_data)
{
CBaseSocket* pSocket = new CBaseSocket();
if(!pSocket)
return NETLIB_ERROR;
int ret = pSocket->Listen(server_ip, port , callback , callback_data);
if(ret == NETLIB_ERROR)
delete pSocket;
return ret;
}
net_handle_t netlib_connect(
const char* server_ip,
uint16_t port,
callback_t callback,
void* callback_data)
{
CBaseSocket* pSocket = new CBaseSocket();
if(!pSocket)
return NETLIB_INVALID_HANDLE;
net_handle_t handle = pSocket->Connect(server_ip, port, callback, callback_data);
if(handle == NETLIB_INVALID_HANDLE)
delete pSocket;
return handle;
}
int netlib_send(net_handle_t handle,void* buf, int len)
{
CBaseSocket* pSocket = FindBaseSocket(handle);
if(!pSocket)
{
return NETLIB_ERROR;
}
int ret = pSocket->Send(buf, len);
pSocket->ReleaseRef();
return ret;
}
int netlib_recv(net_handle_t handle, void* buf, int len)
{
CBaseSocket* pSocket = FindBaseSocket(handle);
if(!pSocket)
return NETLIB_ERROR;
int ret = pSocket->Recv(buf, len);
pSocket->ReleaseRef();
return ret;
}
int netlib_close(net_handle_t handle)
{
CBaseSocket* pSocket = FindBaseSocket(handle);
if(!pSocket)
return NETLIB_ERROR;
int ret = pSocket->Close();
pSocket->ReleaseRef();
return ret;
}
int netlib_option(net_handle_t handle, int opt, void* optval)
{
CBaseSocket* pSocket = FindBaseSocket(handle);
if(!pSocket)
return NETLIB_ERROR;
if((opt >= NETLIB_OPT_GET_REMOTE_IP) && !optval)
return NETLIB_ERROR;
switch(opt)
{
case NETLIB_OPT_SET_CALLBACK:
pSocket->SetCallback((callback_t)optval);
break;
case NETLIB_OPT_SET_CALLBACK_DATA:
pSocket->SetCallbackData(optval);
break;
case NETLIB_OPT_GET_REMOTE_IP:
*(string*)optval = pSocket->GetRemoteIP();
break;
case NETLIB_OPT_GET_REMOTE_PORT:
*(uint16_t*)optval = pSocket->GetRemotePort();
break;
case NETLIB_OPT_GET_LOCAL_IP:
*(string*)optval = pSocket->GetLocalIP();
break;
case NETLIB_OPT_SET_SEND_BUF_SIZE:
pSocket->SetSendBufSize(*(uint32_t*)optval);
break;
case NETLIB_OPT_SET_RECV_BUF_SIZE:
pSocket->SetRecvBufSize(*(uint32_t*)optval);
break;
}
pSocket->ReleaseRef();
return NETLIB_OK;
}
int netlib_register_timer(callback_t callback, void* user_data, uint64_t interval)
{
CEventDispatch::Instance()->AddTimer(callback, user_data, interval);
return 0;
}
int netlib_delete_timer(callback_t callback, void* user_data)
{
CEventDispatch::Instance()->RemoveTimer(callback, user_data);
return 0;
}
int netlib_add_loop(callback_t callback, void* user_data)
{
CEventDispatch::Instance()->AddLoop(callback, user_data);
return 0;
}
void netlib_eventloop(uint32_t wait_timeout)
{
CEventDispatch::Instance()->StartDispatch(wait_timeout);
}
void netlib_stop_event()
{
CEventDispatch::Instance()->StopDispatch();
}
bool netlib_is_running()
{
return CEventDispatch::Instance()->isRunning();
}