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 "ipc_common.h"
17
25
@@ -53,25 +61,40 @@ Generally communication between the producer and the consumer looks like:
53
61
*/
54
62
55
63
int consumer_connect (int port ) {
64
+
65
+ #ifdef _WIN32
66
+ WSADATA wsaData ;
67
+ SOCKET producer_socket , consumer_socket ;
68
+ #else
69
+ int producer_socket = -1 ;
70
+ int consumer_socket = -1 ;
71
+ #endif
72
+
56
73
struct sockaddr_in consumer_addr ;
57
74
struct sockaddr_in producer_addr ;
58
75
int producer_addr_len ;
59
- int producer_socket = -1 ;
60
- int consumer_socket = -1 ;
61
76
int ret = -1 ;
62
77
78
+ #ifdef _WIN32
79
+ // initialize Winsock
80
+ if (WSAStartup (MAKEWORD (2 , 2 ), & wsaData ) != 0 ) {
81
+ fprintf (stderr , "Failed. Error Code : %d" , WSAGetLastError ());
82
+ return -1 ;
83
+ }
84
+ #endif
85
+
63
86
// create a socket
64
87
consumer_socket = socket (AF_INET , SOCK_STREAM , 0 );
65
88
if (consumer_socket < 0 ) {
66
89
fprintf (stderr , "[consumer] ERROR: creating socket failed\n" );
67
- return -1 ;
90
+ goto err_WSA_cleanup ;
68
91
}
69
92
70
93
fprintf (stderr , "[consumer] Socket created\n" );
71
94
72
95
// set the IP address and the port
73
96
consumer_addr .sin_family = AF_INET ;
74
- consumer_addr .sin_port = htons (port );
97
+ consumer_addr .sin_port = htons (( uint16_t ) port );
75
98
consumer_addr .sin_addr .s_addr = inet_addr (INET_ADDR );
76
99
77
100
// bind to the IP address and the port
@@ -101,12 +124,22 @@ int consumer_connect(int port) {
101
124
}
102
125
103
126
fprintf (stderr , "[consumer] Producer connected at IP %s and port %i\n" ,
104
- inet_ntoa (producer_addr .sin_addr ), ntohs (producer_addr .sin_port ));
127
+ inet_ntoa (producer_addr .sin_addr ),
128
+ (int )ntohs (producer_addr .sin_port ));
105
129
106
- ret = producer_socket ; // success
130
+ ret = ( int ) producer_socket ; // success
107
131
108
132
err_close_consumer_socket :
133
+ #ifdef _WIN32
134
+ closesocket (consumer_socket );
135
+ #else
109
136
close (consumer_socket );
137
+ #endif
138
+
139
+ err_WSA_cleanup :
140
+ #ifdef _WIN32
141
+ WSACleanup ();
142
+ #endif
110
143
111
144
return ret ;
112
145
}
@@ -117,7 +150,13 @@ int run_consumer(int port, const umf_memory_pool_ops_t *pool_ops,
117
150
void * provider_params , memcopy_callback_t memcopy_callback ,
118
151
void * memcopy_ctx ) {
119
152
char consumer_message [MSG_SIZE ];
153
+
154
+ #ifdef _WIN32
155
+ SOCKET producer_socket ;
156
+ #else
120
157
int producer_socket = -1 ;
158
+ #endif
159
+
121
160
int ret = -1 ;
122
161
umf_memory_provider_handle_t provider = NULL ;
123
162
umf_result_t umf_result = UMF_RESULT_ERROR_UNKNOWN ;
@@ -171,8 +210,8 @@ int run_consumer(int port, const umf_memory_pool_ops_t *pool_ops,
171
210
IPC_handle_size );
172
211
173
212
// send confirmation to the producer (IPC handle size)
174
- recv_len =
175
- send ( producer_socket , & IPC_handle_size , sizeof (IPC_handle_size ), 0 );
213
+ recv_len = send ( producer_socket , ( const char * ) & IPC_handle_size ,
214
+ sizeof (IPC_handle_size ), 0 );
176
215
if (recv_len < 0 ) {
177
216
fprintf (stderr , "[consumer] ERROR: sending confirmation failed\n" );
178
217
goto err_free_recv_buffer ;
@@ -214,8 +253,8 @@ int run_consumer(int port, const umf_memory_pool_ops_t *pool_ops,
214
253
strcpy (consumer_message , "SKIP" );
215
254
216
255
// send the SKIP response to the producer
217
- send (producer_socket , consumer_message , strlen ( consumer_message ) + 1 ,
218
- 0 );
256
+ send (producer_socket , consumer_message ,
257
+ ( int ) strlen ( consumer_message ) + 1 , 0 );
219
258
220
259
goto err_free_recv_buffer ;
221
260
}
@@ -249,8 +288,8 @@ int run_consumer(int port, const umf_memory_pool_ops_t *pool_ops,
249
288
strcpy (consumer_message , CONSUMER_MSG );
250
289
251
290
// send response to the producer
252
- if (send (producer_socket , consumer_message , strlen ( consumer_message ) + 1 ,
253
- 0 ) < 0 ) {
291
+ if (send (producer_socket , consumer_message ,
292
+ ( int ) strlen ( consumer_message ) + 1 , 0 ) < 0 ) {
254
293
fprintf (stderr , "[consumer] ERROR: send() failed\n" );
255
294
goto err_closeIPCHandle ;
256
295
}
@@ -273,7 +312,11 @@ int run_consumer(int port, const umf_memory_pool_ops_t *pool_ops,
273
312
free (recv_buffer );
274
313
275
314
err_close_producer_socket :
315
+ #ifdef _WIN32
316
+ closesocket (producer_socket );
317
+ #else
276
318
close (producer_socket );
319
+ #endif
277
320
278
321
err_umfMemoryPoolDestroy :
279
322
umfPoolDestroy (pool );
@@ -295,20 +338,34 @@ int run_consumer(int port, const umf_memory_pool_ops_t *pool_ops,
295
338
296
339
int producer_connect (int port ) {
297
340
struct sockaddr_in consumer_addr ;
341
+
342
+ #ifdef _WIN32
343
+ WSADATA wsaData ;
344
+ SOCKET producer_socket ;
345
+ #else
298
346
int producer_socket = -1 ;
347
+ #endif
348
+
349
+ #ifdef _WIN32
350
+ // initialize Winsock
351
+ if (WSAStartup (MAKEWORD (2 , 2 ), & wsaData ) != 0 ) {
352
+ fprintf (stderr , "Failed. Error Code : %d" , WSAGetLastError ());
353
+ return -1 ;
354
+ }
355
+ #endif
299
356
300
357
// create a producer socket
301
358
producer_socket = socket (AF_INET , SOCK_STREAM , 0 );
302
359
if (producer_socket < 0 ) {
303
360
fprintf (stderr , "[producer] ERROR: Unable to create socket\n" );
304
- return -1 ;
361
+ goto err_WSA_cleanup ;
305
362
}
306
363
307
364
fprintf (stderr , "[producer] Socket created\n" );
308
365
309
366
// set IP address and port the same as for the consumer
310
367
consumer_addr .sin_family = AF_INET ;
311
- consumer_addr .sin_port = htons (port );
368
+ consumer_addr .sin_port = htons (( uint16_t ) port );
312
369
consumer_addr .sin_addr .s_addr = inet_addr (INET_ADDR );
313
370
314
371
// send connection request to the consumer
@@ -321,10 +378,19 @@ int producer_connect(int port) {
321
378
322
379
fprintf (stderr , "[producer] Connected to the consumer\n" );
323
380
324
- return producer_socket ; // success
381
+ return ( int ) producer_socket ; // success
325
382
326
383
err_close_producer_socket_connect :
384
+ #ifdef _WIN32
385
+ closesocket (producer_socket );
386
+ #else
327
387
close (producer_socket );
388
+ #endif
389
+
390
+ err_WSA_cleanup :
391
+ #ifdef _WIN32
392
+ WSACleanup ();
393
+ #endif
328
394
329
395
return -1 ;
330
396
}
@@ -340,18 +406,20 @@ int run_producer(int port, const umf_memory_pool_ops_t *pool_ops,
340
406
int producer_socket = -1 ;
341
407
char consumer_message [MSG_SIZE ];
342
408
409
+ #if !defined(_WIN32 )
343
410
ret = prctl (PR_SET_PTRACER , getppid ());
344
411
if (ret == -1 ) {
345
412
perror ("PR_SET_PTRACER may be not supported. prctl() call failed" );
346
413
goto err_end ;
347
414
}
415
+ #endif
348
416
349
417
// create OS memory provider
350
418
umf_result =
351
419
umfMemoryProviderCreate (provider_ops , provider_params , & provider );
352
420
if (umf_result != UMF_RESULT_SUCCESS ) {
353
421
fprintf (stderr , "[producer] ERROR: creating memory provider failed\n" );
354
- return -1 ;
422
+ goto err_end ;
355
423
}
356
424
357
425
umf_memory_pool_handle_t pool ;
@@ -421,8 +489,8 @@ int run_producer(int port, const umf_memory_pool_ops_t *pool_ops,
421
489
}
422
490
423
491
// send the IPC_handle_size to the consumer
424
- ssize_t len =
425
- send ( producer_socket , & IPC_handle_size , sizeof (IPC_handle_size ), 0 );
492
+ ssize_t len = send ( producer_socket , ( const char * ) & IPC_handle_size ,
493
+ sizeof (IPC_handle_size ), 0 );
426
494
if (len < 0 ) {
427
495
fprintf (stderr , "[producer] ERROR: unable to send the message\n" );
428
496
goto err_close_producer_socket ;
@@ -459,7 +527,8 @@ int run_producer(int port, const umf_memory_pool_ops_t *pool_ops,
459
527
}
460
528
461
529
// send the IPC_handle of IPC_handle_size to the consumer
462
- if (send (producer_socket , IPC_handle , IPC_handle_size , 0 ) < 0 ) {
530
+ if (send (producer_socket , (const char * )IPC_handle , (int )IPC_handle_size ,
531
+ 0 ) < 0 ) {
463
532
fprintf (stderr , "[producer] ERROR: unable to send the message\n" );
464
533
goto err_close_producer_socket ;
465
534
}
@@ -512,7 +581,11 @@ int run_producer(int port, const umf_memory_pool_ops_t *pool_ops,
512
581
}
513
582
514
583
err_close_producer_socket :
584
+ #ifdef _WIN32
585
+ closesocket (producer_socket );
586
+ #else
515
587
close (producer_socket );
588
+ #endif
516
589
517
590
err_PutIPCHandle :
518
591
umf_result = umfPutIPCHandle (IPC_handle );
0 commit comments