[567] | 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 |
|
---|
| 12 | bool NetUDPServer_Listen(uint16_t port, bool (*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 |
|
---|
| 57 | bool NetUDPServer_Send(sockaddr* address, flatline_packet* 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 |
|
---|
| 64 | int client_sock = 0;
|
---|
| 65 | sockaddr_in client_address;
|
---|
| 66 |
|
---|
| 67 | int NetUDPSocket_Create(uint16_t port)
|
---|
| 68 | {
|
---|
| 69 | int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
---|
| 70 | sockaddr_in address;
|
---|
| 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 | client_address = address;
|
---|
| 98 | return sock;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | int NetTCPSocket_Create(uint16_t port)
|
---|
| 102 | {
|
---|
| 103 | int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
---|
| 104 | sockaddr_in address;
|
---|
| 105 | unsigned long nonBlocking = 1;
|
---|
| 106 |
|
---|
| 107 | if (sock < 0)
|
---|
| 108 | {
|
---|
| 109 | DDrConsole_PrintF("could not create socket");
|
---|
| 110 | return -1;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 |
|
---|
| 114 | memset(&client_address, 0, sizeof(sockaddr_in));
|
---|
| 115 | address.sin_family = AF_INET;
|
---|
| 116 | address.sin_addr.s_addr = htonl(INADDR_ANY);
|
---|
| 117 | address.sin_port = htons(port);
|
---|
| 118 |
|
---|
| 119 | if (bind(sock, (sockaddr*)&address, sizeof(sockaddr_in)) < 0)
|
---|
| 120 | {
|
---|
| 121 | DDrConsole_PrintF("could not bind port %d", port);
|
---|
| 122 | return false;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 |
|
---|
| 126 | if (ioctlsocket(sock, FIONBIO, &nonBlocking))
|
---|
| 127 | {
|
---|
| 128 | DDrConsole_PrintF("failed to set non-blocking socket");
|
---|
| 129 | return false;
|
---|
| 130 | }
|
---|
| 131 | client_sock = sock;
|
---|
| 132 | client_address = address;
|
---|
| 133 | return sock;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 |
|
---|
| 137 | void NetUDPSocket_Close(int socket)
|
---|
| 138 | {
|
---|
| 139 | closesocket(socket);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | int NetUDPSocket_Send(int socket, const sockaddr* address, flatline_packet* data, int datalen)
|
---|
| 143 | {
|
---|
| 144 | //currently only protects against duplicate packets.
|
---|
| 145 | int addr_size;
|
---|
| 146 | #ifdef TRACK_PACKETS
|
---|
| 147 | static uint32_t packet_index = 0;
|
---|
| 148 | packet_index = (packet_index + 1);
|
---|
| 149 | data->packet_index = packet_index;
|
---|
| 150 | #endif
|
---|
| 151 | data->FLATLINE = *(int*)"FLATLINE";
|
---|
| 152 | switch (address->sa_family)
|
---|
| 153 | {
|
---|
| 154 | case AF_INET:
|
---|
| 155 | addr_size = sizeof(sockaddr_in);
|
---|
| 156 | break;
|
---|
| 157 | case AF_INET6:
|
---|
| 158 | addr_size = sizeof(sockaddr_in6);
|
---|
| 159 | break;
|
---|
| 160 | default:
|
---|
| 161 | addr_size = sizeof(sockaddr_storage);
|
---|
| 162 | }
|
---|
| 163 | return sendto(socket, data, datalen, 0, address, addr_size);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | #ifdef TRACK_PACKETS
|
---|
| 167 | uint32_t last_packet = -1;
|
---|
| 168 | #endif
|
---|
| 169 |
|
---|
| 170 | bool NetUDPSocket_Recieve(int socket, sockaddr_storage* address, char* data, uint16_t* datalen)
|
---|
| 171 | {
|
---|
| 172 | int address_size = sizeof(sockaddr_storage);
|
---|
| 173 | uint32_t msg_size = recvfrom(socket, data, 1400, 0, (SOCKADDR *)address, &address_size);
|
---|
| 174 | if (msg_size == 0)
|
---|
| 175 | return false;
|
---|
| 176 | else if (msg_size == SOCKET_ERROR) {
|
---|
| 177 | int errorno = WSAGetLastError();
|
---|
| 178 | if (errorno == WSAEWOULDBLOCK) return false; //no packets.
|
---|
| 179 | else {
|
---|
| 180 | //DDrConsole_PrintF("Packet type %i", ((flatline_packet*)data)->id);
|
---|
| 181 | NetCatchError();
|
---|
| 182 | return false;
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 | /*
|
---|
| 186 | #ifdef TRACK_PACKETS
|
---|
| 187 | if(client_connected && !server_started) {
|
---|
| 188 | uint32_t index_mkr = ((flatline_packet*)(data))->packet_index;
|
---|
| 189 | //fancy way of checking if index_mkr is on one side of the max size of an int and last_packet is on the other
|
---|
| 190 | if( ((index_mkr - last_packet < 0x80000000) && (index_mkr > last_packet))
|
---|
| 191 | //if it is, subtract half the size of an int from each. ^_^
|
---|
| 192 | || ((index_mkr - 0x80000000) > (last_packet - 0x80000000) ) )
|
---|
| 193 | {
|
---|
| 194 | return false;
|
---|
| 195 | }
|
---|
| 196 | else {
|
---|
| 197 | last_packet = index_mkr;
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | #endif
|
---|
| 202 | */
|
---|
| 203 |
|
---|
| 204 | *datalen = msg_size;
|
---|
| 205 | return true;
|
---|
| 206 |
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | DWORD WINAPI StartServer(void* lol){
|
---|
| 210 | NetPlatform_Initalize();
|
---|
| 211 | FLrServer_Run();
|
---|
| 212 | return 0;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | DWORD WINAPI StartClient(void* lol){
|
---|
| 216 | //NetPlatform_Initalize();
|
---|
| 217 | FLrClient_Run((flatline_packet*)lol);
|
---|
| 218 | return 0;
|
---|
| 219 | }
|
---|
| 220 | void NetCatchError(){
|
---|
| 221 | char Message[1024];
|
---|
| 222 | int errorno = WSAGetLastError();
|
---|
| 223 | if(errno == WSAEWOULDBLOCK) {
|
---|
| 224 | return;
|
---|
| 225 | }
|
---|
| 226 | #ifdef WIN32
|
---|
| 227 |
|
---|
| 228 | FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS |
|
---|
| 229 | FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, errorno,
|
---|
| 230 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
---|
| 231 | (LPSTR) Message, 1024, NULL);
|
---|
| 232 | DDrConsole_Print(Message);
|
---|
| 233 | #endif
|
---|
| 234 |
|
---|
| 235 | }
|
---|