55 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66 */
77
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
814#include <arpa/inet.h>
9- #include <stdio.h>
10- #include <stdlib.h>
11- #include <string.h>
1215#include <sys/prctl.h>
1316#include <sys/socket.h>
1417#include <unistd.h>
18+ #endif
19+
20+ #include <stdio.h>
21+ #include <stdlib.h>
22+ #include <string.h>
1523
1624#include <umf/ipc.h>
1725#include <umf/memory_pool.h>
2331#define SIZE_SHM 1024
2432
2533int producer_connect_to_consumer (int port ) {
26- struct sockaddr_in consumer_addr ;
34+ #ifdef _WIN32
35+ WSADATA wsaData ;
36+ SOCKET producer_socket ;
37+ #else
2738 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
2850
2951 // create a producer socket
3052 producer_socket = socket (AF_INET , SOCK_STREAM , 0 );
3153 if (producer_socket < 0 ) {
3254 fprintf (stderr , "[producer] ERROR: Unable to create socket\n" );
33- return -1 ;
55+ goto err_WSA_cleanup ;
3456 }
3557
3658 fprintf (stderr , "[producer] Socket created\n" );
3759
3860 // set IP address and port the same as for the consumer
3961 consumer_addr .sin_family = AF_INET ;
40- consumer_addr .sin_port = htons (port );
62+ consumer_addr .sin_port = htons (( u_short ) port );
4163 consumer_addr .sin_addr .s_addr = inet_addr (INET_ADDR );
4264
4365 // send connection request to the consumer
@@ -50,10 +72,19 @@ int producer_connect_to_consumer(int port) {
5072
5173 fprintf (stderr , "[producer] Connected to the consumer\n" );
5274
53- return producer_socket ; // success
75+ return ( int ) producer_socket ; // success
5476
5577err_close_producer_socket_connect :
78+ #ifdef _WIN32
79+ closesocket (producer_socket );
80+ #else
5681 close (producer_socket );
82+ #endif
83+
84+ err_WSA_cleanup :
85+ #ifdef _WIN32
86+ WSACleanup ();
87+ #endif
5788
5889 return -1 ;
5990}
@@ -163,8 +194,8 @@ int main(int argc, char *argv[]) {
163194 }
164195
165196 // 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 );
168199 if (len < 0 ) {
169200 fprintf (stderr , "[producer] ERROR: unable to send the message\n" );
170201 goto err_close_producer_socket ;
@@ -179,7 +210,7 @@ int main(int argc, char *argv[]) {
179210 memset (recv_buffer , 0 , sizeof (recv_buffer ));
180211
181212 // 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 );
183214 if (len < 0 ) {
184215 fprintf (stderr , "[producer] ERROR: error while receiving the "
185216 "confirmation from the consumer\n" );
@@ -201,7 +232,8 @@ int main(int argc, char *argv[]) {
201232 }
202233
203234 // 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 );
205237 if (len < 0 ) {
206238 fprintf (stderr , "[producer] ERROR: unable to send the message\n" );
207239 goto err_close_producer_socket ;
@@ -253,7 +285,11 @@ int main(int argc, char *argv[]) {
253285 }
254286
255287err_close_producer_socket :
288+ #ifdef _WIN32
289+ closesocket (producer_socket );
290+ #else
256291 close (producer_socket );
292+ #endif
257293
258294err_PutIPCHandle :
259295 umf_result = umfPutIPCHandle (IPC_handle );
0 commit comments