This repository was archived by the owner on May 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTemplateServer.cpp
More file actions
205 lines (158 loc) · 5.62 KB
/
TemplateServer.cpp
File metadata and controls
205 lines (158 loc) · 5.62 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define MDK_EXPORT_FUNCTIONS 1 /*Export the methods*/
#include "TemplateServer.h"
#include "Utility.h"
#include <uv.h>
#define DEFAULT_BACKLOG 128
// Parse callbacks from TemplateSocket
extern void libuv_callback_on_close(uv_handle_t *handle);
extern void libuv_callback_allocate_buffer(uv_handle_t *handle, size_t size, uv_buf_t* buf);
void libuv_callback_on_server_tcp_new_connection(uv_stream_t *server, int status);
void libuv_callback_on_server_udp_read(uv_udp_t* handle, ssize_t nread, const uv_buf_t* recv, const struct sockaddr* addr, unsigned int flags);
void libuv_callback_on_server_tcp_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf);
MDKDLLAPI CTemplateServer::CTemplateServer() {}
MDKDLLAPI CTemplateServer::~CTemplateServer() {}
bool MDKDLLAPI CTemplateServer::Bind(const char *ip, int port, bool udp)
{
int r = 0;
struct sockaddr_in addr;
uv_udp_t* real_udp_socket = NULL;
uv_tcp_t* real_tcp_socket = NULL;
// Initialize the socket
if (udp)
{
m_sockType = SOCKET_UDP;
real_udp_socket = (uv_udp_t*)malloc(sizeof(uv_udp_t));
r = uv_udp_init((uv_loop_t*)m_loop, real_udp_socket);
}
else
{
real_tcp_socket = (uv_tcp_t*)malloc(sizeof(uv_tcp_t));
r = uv_tcp_init((uv_loop_t*)m_loop, real_tcp_socket);
}
if (r)
{
LOG_ERROR("Server", "%s listen error: %s\n", udp ? "UDP" : "TCP", uv_strerror(r));
return false;
}
// Resolve ip and port
uv_ip4_addr(ip, port, &addr);
if (udp)
r = uv_udp_bind(real_udp_socket, (const struct sockaddr*)&addr, 0);
else
r = uv_tcp_bind(real_tcp_socket, (const struct sockaddr*)&addr, 0);
if (r)
{
LOG_ERROR("Server", "%s listen error: %s\n", udp ? "UDP" : "TCP", uv_strerror(r));
return false;
}
/* Listen the socket */
if (udp)
r = uv_udp_recv_start((uv_udp_t*)real_udp_socket, (uv_alloc_cb)libuv_callback_allocate_buffer, libuv_callback_on_server_udp_read);
else
r = uv_listen((uv_stream_t*)real_tcp_socket, DEFAULT_BACKLOG, libuv_callback_on_server_tcp_new_connection);
if (r)
{
LOG_ERROR("Server", "%s listen error: %s\n", udp ? "UDP" : "TCP", uv_strerror(r));
return false;
}
// Access the loop from the other functions
if (udp)
{
real_udp_socket->data = (void*)new CClientData(this);
m_socket = (void*)real_udp_socket;
}
else
{
real_tcp_socket->data = (void*)new CClientData(this);
m_socket = (void*)real_tcp_socket;
}
if (r)
{
LOG_ERROR("Server", "%s listen error: %s\n", udp ? "UDP" : "TCP", uv_strerror(r));
return false;
}
return true;
}
void libuv_callback_on_server_tcp_new_connection(uv_stream_t *server_socket, int status)
{
//_t equals to structure in libuv code
uv_tcp_t* client_socket = NULL;
CClientData* client_data = NULL;
if (status < 0 || server_socket->data == NULL)
return; // data is NULL, no pointer to the Template server or cannot accept the connection
client_data = CTemplateSocket::GetSocketExtraData((mdk_socket)server_socket);
client_socket = (uv_tcp_t*)malloc(sizeof(uv_tcp_t));
uv_tcp_init((uv_loop_t*)client_data->GetSocket()->GetLoop(), client_socket);
// Accept the client
status = uv_accept(server_socket, (uv_stream_t *)client_socket);
if (status != 0)
{
uv_close((uv_handle_t*)client_socket, libuv_callback_on_close);
}
client_socket->data = server_socket->data;
if (!client_data->GetSocket()->OnTCPNewConnection((mdk_socket)client_socket, 0))
{
uv_close((uv_handle_t*)client_socket, libuv_callback_on_close);
return;
}
status = uv_read_start((uv_stream_t*)client_socket, libuv_callback_allocate_buffer, libuv_callback_on_server_tcp_read);
if (status != 0)
uv_close((uv_handle_t*)client_socket, libuv_callback_on_close);
}
void libuv_callback_on_server_tcp_read(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
{
if (nread < 0 || stream->data == NULL)
{
uv_close((uv_handle_t*)stream, libuv_callback_on_close);
//if (buf->base)
// free(buf->base);
return;
}
else if (nread == 0)
{
//if (buf->base)
// free(buf->base);
return;
}
buf->base[nread] = '\0';
#ifdef _DEBUG
LOG_INFO("Server", "TCP: Received %s from client", buf->base);
#endif
CTemplateSocket::GetSocketExtraData((mdk_socket)stream)->GetSocket()->OnTCPRead(stream, buf->base, nread);
//if (buf->base)
// free(buf->base);
}
void libuv_callback_on_server_udp_read(uv_udp_t* handle, ssize_t nread, const uv_buf_t* recv, const struct sockaddr* addr, unsigned int flags)
{
CTemplateSocket* socket_ptr = NULL;
char ip[INET_ADDRSTRLEN];
struct sockaddr_in* addr_in = (struct sockaddr_in*)addr;
ip[0] = '\0';
if (nread <= 0)
{
LOG_INFO("Server", "UDP: Received nothing from client");
return;
}
if (handle->data == NULL)
{
LOG_INFO("Server", "UDP: Wrong pointer!");
return;
}
socket_ptr = (CTemplateSocket*)handle->data;
int x = CTemplateServer::GetIPFromSocket(handle);
inet_ntop(AF_INET, &(addr_in->sin_addr), ip, INET_ADDRSTRLEN);
LOG_INFO("Server", "UDP: Connected %s(%d) %s (Flags: %u)", recv->base, nread, ip, flags);
CTemplateSocket::GetSocketExtraData((mdk_socket)handle)->GetSocket()->OnUDPRead(handle, addr, recv->base, nread);
}