source: Daodan/src/flatline/Flatline_Net.c@ 951

Last change on this file since 951 was 877, checked in by alloc, 11 years ago

Daodan: Moved flatline to subfolder, flatline enabled through patch "flatline"

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