5
5
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
6
*/
7
7
8
+ #ifdef _WIN32
9
+ #define _WINSOCK_DEPRECATED_NO_WARNINGS
10
+ #include <winsock2.h>
11
+ typedef int socklen_t ;
12
+ typedef SSIZE_T ssize_t ;
13
+ #else
8
14
#include <arpa/inet.h>
9
- #include <stdio.h>
10
- #include <stdlib.h>
11
- #include <string.h>
12
15
#include <sys/prctl.h>
13
16
#include <sys/socket.h>
14
17
#include <unistd.h>
18
+ #endif
19
+
20
+ #include <stdio.h>
21
+ #include <stdlib.h>
22
+ #include <string.h>
15
23
16
24
#include <umf/ipc.h>
17
25
#include <umf/memory_pool.h>
23
31
#define SIZE_SHM 1024
24
32
25
33
int producer_connect_to_consumer (int port ) {
26
- struct sockaddr_in consumer_addr ;
34
+ #ifdef _WIN32
35
+ WSADATA wsaData ;
36
+ SOCKET producer_socket ;
37
+ #else
27
38
int producer_socket = -1 ;
39
+ #endif
40
+
41
+ struct sockaddr_in consumer_addr ;
42
+
43
+ #ifdef _WIN32
44
+ // initialize Winsock
45
+ if (WSAStartup (MAKEWORD (2 , 2 ), & wsaData ) != 0 ) {
46
+ fprintf (stderr , "Failed. Error Code : %d" , WSAGetLastError ());
47
+ return -1 ;
48
+ }
49
+ #endif
28
50
29
51
// create a producer socket
30
52
producer_socket = socket (AF_INET , SOCK_STREAM , 0 );
31
53
if (producer_socket < 0 ) {
32
54
fprintf (stderr , "[producer] ERROR: Unable to create socket\n" );
33
- return -1 ;
55
+ goto err_WSA_cleanup ;
34
56
}
35
57
36
58
fprintf (stderr , "[producer] Socket created\n" );
37
59
38
60
// set IP address and port the same as for the consumer
39
61
consumer_addr .sin_family = AF_INET ;
40
- consumer_addr .sin_port = htons (port );
62
+ consumer_addr .sin_port = htons (( u_short ) port );
41
63
consumer_addr .sin_addr .s_addr = inet_addr (INET_ADDR );
42
64
43
65
// send connection request to the consumer
@@ -50,10 +72,19 @@ int producer_connect_to_consumer(int port) {
50
72
51
73
fprintf (stderr , "[producer] Connected to the consumer\n" );
52
74
53
- return producer_socket ; // success
75
+ return ( int ) producer_socket ; // success
54
76
55
77
err_close_producer_socket_connect :
78
+ #ifdef _WIN32
79
+ closesocket (producer_socket );
80
+ #else
56
81
close (producer_socket );
82
+ #endif
83
+
84
+ err_WSA_cleanup :
85
+ #ifdef _WIN32
86
+ WSACleanup ();
87
+ #endif
57
88
58
89
return -1 ;
59
90
}
@@ -163,8 +194,8 @@ int main(int argc, char *argv[]) {
163
194
}
164
195
165
196
// send a size of the IPC_handle to the consumer
166
- ssize_t len =
167
- send ( producer_socket , & IPC_handle_size , sizeof (IPC_handle_size ), 0 );
197
+ ssize_t len = ( ssize_t ) send ( producer_socket , ( const char * ) & IPC_handle_size ,
198
+ sizeof (IPC_handle_size ), 0 );
168
199
if (len < 0 ) {
169
200
fprintf (stderr , "[producer] ERROR: unable to send the message\n" );
170
201
goto err_close_producer_socket ;
@@ -179,7 +210,7 @@ int main(int argc, char *argv[]) {
179
210
memset (recv_buffer , 0 , sizeof (recv_buffer ));
180
211
181
212
// receive the consumer's confirmation
182
- len = recv (producer_socket , recv_buffer , sizeof (recv_buffer ), 0 );
213
+ len = ( ssize_t ) recv (producer_socket , recv_buffer , sizeof (recv_buffer ), 0 );
183
214
if (len < 0 ) {
184
215
fprintf (stderr , "[producer] ERROR: error while receiving the "
185
216
"confirmation from the consumer\n" );
@@ -201,7 +232,8 @@ int main(int argc, char *argv[]) {
201
232
}
202
233
203
234
// send the IPC_handle of IPC_handle_size to the consumer
204
- len = send (producer_socket , IPC_handle , IPC_handle_size , 0 );
235
+ len = (ssize_t )send (producer_socket , (const char * )IPC_handle ,
236
+ (int )IPC_handle_size , 0 );
205
237
if (len < 0 ) {
206
238
fprintf (stderr , "[producer] ERROR: unable to send the message\n" );
207
239
goto err_close_producer_socket ;
@@ -253,7 +285,11 @@ int main(int argc, char *argv[]) {
253
285
}
254
286
255
287
err_close_producer_socket :
288
+ #ifdef _WIN32
289
+ closesocket (producer_socket );
290
+ #else
256
291
close (producer_socket );
292
+ #endif
257
293
258
294
err_PutIPCHandle :
259
295
umf_result = umfPutIPCHandle (IPC_handle );
0 commit comments