src/network.h

changeset 183
39aa77b7188b
parent 182
04c65336777f
child 184
93c81539b702
equal deleted inserted replaced
182:04c65336777f 183:39aa77b7188b
29 29
30 #ifndef TCHESS_NETWORK_H 30 #ifndef TCHESS_NETWORK_H
31 #define TCHESS_NETWORK_H 31 #define TCHESS_NETWORK_H
32 32
33 #include <sys/socket.h> 33 #include <sys/socket.h>
34 #include <netdb.h> 34 #include <stdint.h>
35 35
36 #ifdef __cplusplus 36 #ifdef __cplusplus
37 extern "C" { 37 extern "C" {
38 #endif 38 #endif
39 39
97 void net_send_data(int socket, uint8_t code, void *data, size_t len); 97 void net_send_data(int socket, uint8_t code, void *data, size_t len);
98 uint8_t net_recieve_code(int socket); 98 uint8_t net_recieve_code(int socket);
99 uint8_t net_recieve_code_async(int socket); 99 uint8_t net_recieve_code_async(int socket);
100 void net_recieve_data(int socket, void *data, size_t len); 100 void net_recieve_data(int socket, void *data, size_t len);
101 101
102
103 /* Functions that convert WORD and DWORD values to terminal-chess
104 * network protocol byte order.
105 *
106 * Note: due to the vast majority of processors using little-endian,
107 * the network byte order of terminal-chess is little-endian, too.
108 * While the standard network byte order is big-endian for historic reasons.
109 */
110
111 static inline uint16_t net_htons(uint16_t s) {
112 #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
113 return s;
114 #else
115 #if !defined(__BYTE_ORDER__)
116 const uint16_t endian_test = 1;
117 if (*(const uint8_t *)&endian_test) {
118 return s;
119 }
120 #endif
121 return (uint16_t)((s >> 8) | (s << 8));
122 #endif
123 }
124
125 static inline uint16_t net_ntohs(uint16_t s) {
126 return net_htons(s);
127 }
128
129 static inline uint32_t net_ntohl(uint32_t l) {
130 #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
131 return l;
132 #else
133 #if !defined(__BYTE_ORDER__)
134 const uint16_t endian_test = 1;
135 if (*(const uint8_t *)&endian_test) {
136 return l;
137 }
138 #endif
139 return ((l & 0x000000FFu) << 24)
140 | ((l & 0x0000FF00u) << 8)
141 | ((l & 0x00FF0000u) >> 8)
142 | ((l & 0xFF000000u) >> 24);
143 #endif
144 }
145
146 static inline uint32_t net_htonl(uint32_t l) {
147 return net_ntohl(l);
148 }
149
102 #ifdef __cplusplus 150 #ifdef __cplusplus
103 } 151 }
104 #endif 152 #endif
105 153
106 #endif /* TCHESS_NETWORK_H */ 154 #endif /* TCHESS_NETWORK_H */

mercurial