- Timestamp:
- Oct 6, 2009, 1:21:11 PM (15 years ago)
- Location:
- Daodan/Flatline/src
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
Daodan/Flatline/src/Flatline.c
r457 r460 1 1 #include "Flatline.h" 2 2 3 bool Net_Listen(uint16_t port, bool (*packet_callback)(char* data, int datalen, int from)) 4 { 5 int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 6 7 if (sock < 0) 8 { 9 DDrStartupMessage("could not create socket"); 10 return false; 11 } 12 13 sockaddr_in address; 14 memset(&address, 0, sizeof(sockaddr_in)); 15 address.sin_family = AF_INET; 16 address.sin_addr.s_addr = htonl(INADDR_ANY); 17 address.sin_port = htons(port); 18 19 if (bind(sock, (sockaddr*)&address, sizeof(sockaddr_in)) < 0) 20 { 21 DDrStartupMessage("could not bind port %d", port); 22 closesocket(sock); 23 return false; 24 } 25 26 char data[65537]; 27 data[sizeof(data) - 1] = '\0'; 28 sockaddr_in from; 29 int recvlen; 30 for (;;) 31 { 32 int fromlen = sizeof(sockaddr_in); 33 memset(&from, 0, sizeof(sockaddr_in)); 34 recvlen = recvfrom(sock, data, sizeof(data) - 1, 0, (sockaddr*)&from, (void*)&fromlen); 35 if (!packet_callback(data, recvlen, ntohl(from.sin_addr.s_addr))) 36 break; 37 } 38 closesocket(sock); 39 return true; 40 } 41 42 int Net_CreateSocket(uint16_t port) 43 { 44 int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 45 46 if (sock < 0) 47 { 48 DDrStartupMessage("could not create socket"); 49 return -1; 50 } 51 52 sockaddr_in address; 53 memset(&address, 0, sizeof(sockaddr_in)); 54 address.sin_family = AF_INET; 55 address.sin_addr.s_addr = htonl(INADDR_ANY); 56 address.sin_port = htons(port); 57 58 if (bind(sock, (sockaddr*)&address, sizeof(sockaddr_in)) < 0) 59 { 60 DDrStartupMessage("could not bind port %d", port); 61 return false; 62 } 63 64 unsigned long nonBlocking = 1; 65 if (ioctlsocket(sock, FIONBIO, &nonBlocking)) 66 { 67 DDrStartupMessage("failed to set non-blocking socket"); 68 return false; 69 } 70 71 return sock; 72 } 73 74 bool Net_Send(int socket, int ip, uint16_t port, char* data, int datalen) 75 { 76 sockaddr_in address; 77 memset(&address, 0, sizeof(sockaddr_in)); 78 address.sin_family = AF_INET; 79 address.sin_addr.s_addr = ip; 80 address.sin_port = htons(port); 81 82 return sendto(socket, data, datalen, 0, (sockaddr*)&address, sizeof(address)); 83 } 84 85 void Net_CloseSocket(int sock) 86 { 87 closesocket(sock); 88 } 89 90 bool FLrListen_PacketCallback(char* data, int datalen, int from) 3 bool FLrServer_PacketCallback(char* data, int datalen, int from) 91 4 { 92 5 static int recieved = 0; … … 98 11 } 99 12 100 bool FLr Listen_Run()13 bool FLrServer_Run() 101 14 { 102 15 return Net_Listen(27777, FLrListen_PacketCallback); -
Daodan/Flatline/src/Flatline.h
r459 r460 8 8 #include <stdbool.h> 9 9 #include <stdint.h> 10 11 #define thread __thread 10 12 11 13 #ifdef WIN32 … … 27 29 #endif 28 30 31 #define pad1_size (sizeof(int64_t) - sizeof(short)) 32 #define pad2_size (128 - (sizeof(short) + pad1_size + sizeof(int64_t))) 33 34 typedef struct { 35 short ss_family; 36 char pad1[pad1_size]; 37 uint64_t pad64; 38 char pad2[pad2_size]; 39 } sockaddr_storage; 40 29 41 typedef struct sockaddr sockaddr; 30 42 typedef struct sockaddr_in sockaddr_in; 31 43 32 bool Net_Listen(uint16_t port, bool (*packet_callback)(char* data, int datalen, int from)); 44 bool NetUDPServer_Listen(uint16_t port, bool (*packet_callback)(char* data, int datalen, int from)); 45 bool NetUDPServer_Send(sockaddr* address, char* data, int datalen) 33 46 34 47 int NetUDPSocket_Create(uint16_t port); … … 36 49 void NetUDPSocket_Close(int sock); 37 50 51 typedef struct { 52 char signature[8]; 53 uint16_t protocol_version; 54 char data[0]; 55 } handshake_packet; 56 57 typedef struct { 58 char country[2]; 59 char name[256]; 60 } connect_send; //signature="CONNECT\0" 61 62 typedef struct { 63 char name[256]; 64 uint32_t numplayers; //signature="STATUS\0\0" 65 } status_recv; 66 67 38 68 bool FLrListen_PacketCallback(char* data, int datalen, int from); 39 69 bool FLrListen_Run();
Note:
See TracChangeset
for help on using the changeset viewer.