diff options
Diffstat (limited to 'core/network.c')
-rw-r--r-- | core/network.c | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/core/network.c b/core/network.c index 8c6fa7b6..7e3b344a 100644 --- a/core/network.c +++ b/core/network.c | |||
@@ -71,7 +71,7 @@ int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length) | |||
71 | the packet data into data | 71 | the packet data into data |
72 | the packet length into length. | 72 | the packet length into length. |
73 | dump all empty packets. */ | 73 | dump all empty packets. */ |
74 | int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length) | 74 | static int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length) |
75 | { | 75 | { |
76 | ADDR addr; | 76 | ADDR addr; |
77 | #ifdef WIN32 | 77 | #ifdef WIN32 |
@@ -88,6 +88,27 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length) | |||
88 | return 0; | 88 | return 0; |
89 | } | 89 | } |
90 | 90 | ||
91 | static packet_handler_callback packethandlers[256] = {0}; | ||
92 | |||
93 | void networking_registerhandler(uint8_t byte, packet_handler_callback cb) | ||
94 | { | ||
95 | packethandlers[byte] = cb; | ||
96 | } | ||
97 | |||
98 | void networking_poll() | ||
99 | { | ||
100 | IP_Port ip_port; | ||
101 | uint8_t data[MAX_UDP_PACKET_SIZE]; | ||
102 | uint32_t length; | ||
103 | |||
104 | while (receivepacket(&ip_port, data, &length) != -1) | ||
105 | { | ||
106 | if (length < 1) continue; | ||
107 | if (!packethandlers[data[0]]) continue; | ||
108 | packethandlers[data[0]](ip_port, data, length); | ||
109 | } | ||
110 | } | ||
111 | |||
91 | /* initialize networking | 112 | /* initialize networking |
92 | bind to ip and port | 113 | bind to ip and port |
93 | ip must be in network order EX: 127.0.0.1 = (7F000001) | 114 | ip must be in network order EX: 127.0.0.1 = (7F000001) |
@@ -149,7 +170,6 @@ int init_networking(IP ip, uint16_t port) | |||
149 | bind(sock, (struct sockaddr*)&addr, sizeof(addr)); | 170 | bind(sock, (struct sockaddr*)&addr, sizeof(addr)); |
150 | 171 | ||
151 | return 0; | 172 | return 0; |
152 | |||
153 | } | 173 | } |
154 | 174 | ||
155 | /* function to cleanup networking stuff */ | 175 | /* function to cleanup networking stuff */ |