source: Daodan/src/Flatline_Net.c@ 876

Last change on this file since 876 was 876, checked in by gumby, 11 years ago

Daodan pass 1

File size: 5.2 KB
Line 
1#include "Flatline.h"
2#include "Flatline_Server.h"
3
4
5#define TRACK_PACKETS
6
7/*thread */int UDPServer_Socket = 0;
8
9
10
11
12unsigned char NetUDPServer_Listen(uint16_t port, unsigned char (*packet_callback)(char* data, int datalen, int from))
13{
14 sockaddr_in address;
15 UDPServer_Socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
16
17 if (UDPServer_Socket < 0)
18 {
19 DDrConsole_PrintF("could not create socket");
20 return false;
21 }
22
23
24 memset(&address, 0, sizeof(sockaddr_in));
25 address.sin_family = AF_INET;
26 address.sin_addr.s_addr = htonl(INADDR_ANY);
27 address.sin_port = htons(port);
28
29 if (bind(UDPServer_Socket, (sockaddr*)&address, sizeof(sockaddr_in)) < 0)
30 {
31 DDrConsole_PrintF("could not bind port %d", port);
32 closesocket(UDPServer_Socket);
33 return false;
34 }
35 else {
36
37
38 char data[65537] = {0};
39
40 sockaddr_in from;
41 int recvlen;
42 FLrServer_Initialize();
43 for (;;)
44 {
45 int fromlen = sizeof(sockaddr_in);
46 memset(&from, 0, sizeof(sockaddr_in));
47 recvlen = recvfrom(UDPServer_Socket, data, 65537 - 1, 0, (sockaddr*)&from, (void*)&fromlen);
48
49 if (!packet_callback(data, recvlen, ntohl(from.sin_addr.s_addr)))
50 break;
51 }
52 closesocket(UDPServer_Socket);
53 return true;
54 }
55}
56
57unsigned char NetUDPServer_Send(sockaddr* address, char* data, int datalen)
58{
59 //DDrConsole_PrintF("Sending data size %u to %s on socket %i", datalen, inet_ntoa( ((sockaddr_in*)address)->sin_addr ), UDPServer_Socket);
60 //data->FLATLINE = *(int*)"FLATLINE";
61 return NetUDPSocket_Send(UDPServer_Socket, address, data, datalen);
62}
63
64int client_sock = 0;
65sockaddr_in client_address;
66
67int NetUDPSocket_Create(uint16_t port, sockaddr_in* address)
68{
69 int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
70
71 unsigned long nonBlocking = 1;
72 if (sock < 0)
73 {
74 DDrConsole_PrintF("could not create socket");
75 return -1;
76 }
77
78
79// memset(&client_address, 0, sizeof(sockaddr_in));
80 address->sin_family = AF_INET;
81 address->sin_addr.s_addr = htonl(INADDR_ANY);
82 address->sin_port = htons(port);
83
84 if (bind(sock, (sockaddr*)address, sizeof(sockaddr_in)) < 0)
85 {
86 DDrConsole_PrintF("could not bind port %d", port);
87 return false;
88 }
89
90
91 if (ioctlsocket(sock, FIONBIO, &nonBlocking))
92 {
93 DDrConsole_PrintF("failed to set non-blocking socket");
94 return false;
95 }
96 client_sock = sock;
97 return sock;
98}
99
100int NetTCPSocket_Create(uint16_t port, sockaddr_in* address)
101{
102 int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
103
104 unsigned long nonBlocking = 0;
105 if (sock < 0)
106 {
107 DDrConsole_PrintF("could not create socket");
108 return -1;
109 }
110
111
112// memset(&client_address, 0, sizeof(sockaddr_in));
113 //address->sin_family = AF_INET;
114 //address->sin_addr.s_addr = INADDR_ANY;
115 //address->sin_port = htons(port);
116 /*
117 if (bind(sock, (sockaddr*)address, sizeof(sockaddr_in)) < 0)
118 {
119 DDrConsole_PrintF("could not bind port %d", port);
120 return false;
121 }
122 */
123 /*
124 if (ioctlsocket(sock, FIONBIO, &nonBlocking))
125 {
126 DDrConsole_PrintF("failed to set non-blocking socket");
127 return false;
128 }*/
129 //client_sock = sock;
130 return sock;
131}
132
133void NetUDPSocket_Close(int socket)
134{
135 closesocket(socket);
136}
137
138int NetUDPSocket_Send(int socket, const sockaddr* address, char* data, int datalen)
139{
140 //currently only protects against duplicate packets.
141 int addr_size;
142 int sent_bytes;
143#if 0 //#ifdef TRACK_PACKETS
144 static uint32_t packet_index = 0;
145 packet_index = (packet_index + 1);
146 data->packet_index = packet_index;
147#endif
148 //data->FLATLINE = *(int*)"FLATLINE";
149 switch (address->sa_family)
150 {
151 case AF_INET:
152 addr_size = sizeof(sockaddr_in);
153 break;
154 case AF_INET6:
155 addr_size = sizeof(sockaddr_in6);
156 break;
157 default:
158 addr_size = sizeof(sockaddr_storage);
159 }
160 sent_bytes = sendto(socket, data, datalen, 0, address, addr_size);
161
162 if(sent_bytes == SOCKET_ERROR) {
163 NetCatchError();
164 }
165
166 return sent_bytes;
167}
168
169#ifdef TRACK_PACKETS
170uint32_t last_packet = -1;
171#endif
172
173unsigned char NetUDPSocket_Recieve(int socket, sockaddr_storage* address, char* data, uint16_t* datalen)
174{
175 int address_size = sizeof(sockaddr_storage);
176 uint32_t msg_size = recvfrom(socket, data, 1400, 0, (SOCKADDR *)address, &address_size);
177 if (msg_size == 0)
178 return false;
179 else if (msg_size == SOCKET_ERROR) {
180 int errorno = WSAGetLastError();
181 if (errorno == WSAEWOULDBLOCK) return false; //no packets.
182 else {
183 DDrConsole_PrintF("Packet type %i", ((flatline_packet*)data)->id);
184 NetCatchError();
185 return false;
186 }
187 }
188 *datalen = msg_size;
189 return true;
190
191}
192
193DWORD WINAPI StartServer(void* lol){
194 if(NetPlatform_Initalize() && FlatlineInitialize())
195 {
196 FLrServer_Run();
197 }
198 return 0;
199}
200
201DWORD WINAPI StartClient(void* lol){
202 //NetPlatform_Initalize();
203 if(FlatlineInitialize())
204 {
205 FLrClient_Run((flatline_packet*)lol);
206 }
207 return 0;
208}
209void NetCatchError(){
210 char Message[1024];
211 int errorno = WSAGetLastError();
212 if(errno == WSAEWOULDBLOCK) {
213 return;
214 }
215#ifdef WIN32
216
217 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS |
218 FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, errorno,
219 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
220 (LPSTR) Message, 1024, NULL);
221 DDrConsole_Print(Message);
222#endif
223
224}
Note: See TracBrowser for help on using the repository browser.