forked from pikasTech/PikaPython
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPikaPlatform_socket.c
More file actions
213 lines (192 loc) · 6.13 KB
/
Copy pathPikaPlatform_socket.c
File metadata and controls
213 lines (192 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "PikaPlatform_socket.h"
/*
The functinos start with PIKA_WEAK are weak functions,
you need to override them in your platform.
*/
#ifdef _WIN32
#pragma comment(lib, "ws2_32.lib")
static int pika_platform_winsock_initialized = 0;
int pika_platform_init_winsock() {
if (0 == pika_platform_winsock_initialized) {
WSADATA wsaData;
int res = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (res != 0) {
__platform_printf("WSAStartup failed with error: %d\n", res);
return 1;
}
pika_platform_winsock_initialized = 1;
} else if (0 < pika_platform_winsock_initialized) {
pika_platform_winsock_initialized++;
}
return 0;
}
int pika_platform_cleanup_winsock() {
if (1 == pika_platform_winsock_initialized) {
WSACleanup();
pika_platform_winsock_initialized = 0;
} else if (1 < pika_platform_winsock_initialized) {
pika_platform_winsock_initialized--;
}
return 0;
}
#endif
PIKA_WEAK int pika_platform_socket(int __domain, int __type, int __protocol) {
#if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
return socket(__domain, __type, __protocol);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
PIKA_WEAK int pika_platform_bind(int __fd,
const struct sockaddr* __addr,
socklen_t __addr_len) {
#if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
return bind(__fd, __addr, __addr_len);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
PIKA_WEAK int pika_platform_listen(int __fd, int __n) {
#if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
return listen(__fd, __n);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
PIKA_WEAK int pika_platform_accept(int __fd,
struct sockaddr* __addr,
socklen_t* __addr_len) {
#if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
return accept(__fd, __addr, __addr_len);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
PIKA_WEAK int pika_platform_connect(int __fd,
const struct sockaddr* __addr,
socklen_t __addr_len) {
#if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
return connect(__fd, __addr, __addr_len);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
PIKA_WEAK int pika_platform_htons(int __hostshort) {
#if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
return htons(__hostshort);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
/* gethostbyname */
PIKA_WEAK struct hostent* pika_platform_gethostbyname(const char* __name) {
#if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
return gethostbyname(__name);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
/* inet_ntoa */
PIKA_WEAK char* pika_platform_inet_ntoa(struct in_addr __addr) {
#if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
return inet_ntoa(__addr);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
PIKA_WEAK int pika_platform_send(int __fd,
const void* __buf,
size_t __n,
int __flags) {
#if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
return send(__fd, __buf, __n, __flags);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
PIKA_WEAK int pika_platform_recv(int __fd,
void* __buf,
size_t __n,
int __flags) {
#if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
return recv(__fd, __buf, __n, __flags);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
return -1;
#endif
}
/* gethostname */
PIKA_WEAK int pika_platform_gethostname(char* __name, size_t __len) {
#if defined(__linux__) || defined(_WIN32)
return gethostname(__name, __len);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
return -1;
#endif
}
PIKA_WEAK int pika_platform_getaddrinfo(const char* __name,
const char* __service,
const struct addrinfo* __req,
struct addrinfo** __pai) {
#if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
return getaddrinfo(__name, __service, __req, __pai);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
PIKA_WEAK void pika_platform_freeaddrinfo(struct addrinfo* __ai) {
#if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
freeaddrinfo(__ai);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
PIKA_WEAK int pika_platform_setsockopt(int __fd,
int __level,
int __optname,
const void* __optval,
socklen_t __optlen) {
#if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
return setsockopt(__fd, __level, __optname, __optval, __optlen);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
PIKA_WEAK int pika_platform_fcntl(int fd, int cmd, long arg) {
#if defined(__linux__) || PIKA_LWIP_ENABLE
return fcntl(fd, cmd, arg);
#elif defined(_WIN32)
if (cmd == F_GETFL) {
u_long mode = 0;
ioctlsocket(fd, FIONBIO, &mode);
return (mode ? O_NONBLOCK : 0);
} else if (cmd == F_SETFL) {
u_long mode = (arg & O_NONBLOCK) ? 1 : 0;
return ioctlsocket(fd, FIONBIO, &mode);
}
return -1;
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
/* os file API */
PIKA_WEAK int pika_platform_close(int __fd) {
#if defined(__linux__) || PIKA_LWIP_ENABLE
return close(__fd);
#elif defined(_WIN32)
return closesocket(__fd);
#elif PIKA_FREERTOS_ENABLE
return closesocket(__fd);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}
PIKA_WEAK int pika_platform_write(int __fd, const void* __buf, size_t __nbyte) {
#if defined(__linux__) || PIKA_LWIP_ENABLE
return write(__fd, __buf, __nbyte);
#elif defined(_WIN32)
return send(__fd, __buf, __nbyte, 0);
#else
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
#endif
}