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>
15
+ #include <sys/socket.h>
16
+ #include <unistd.h>
17
+ #endif
18
+
9
19
#include <stdio.h>
10
20
#include <stdlib.h>
11
21
#include <string.h>
12
- #include <sys/socket.h>
13
- #include <unistd.h>
14
22
15
23
#include <umf/ipc.h>
16
24
#include <umf/memory_pool.h>
22
30
#define SIZE_SHM 1024
23
31
24
32
int producer_connect_to_consumer (int port ) {
25
- struct sockaddr_in consumer_addr ;
33
+ #ifdef _WIN32
34
+ WSADATA wsaData ;
35
+ SOCKET producer_socket ;
36
+ #else
26
37
int producer_socket = -1 ;
38
+ #endif
39
+
40
+ struct sockaddr_in consumer_addr ;
41
+
42
+ #ifdef _WIN32
43
+ // initialize Winsock
44
+ if (WSAStartup (MAKEWORD (2 , 2 ), & wsaData ) != 0 ) {
45
+ fprintf (stderr , "Failed. Error Code : %d" , WSAGetLastError ());
46
+ return -1 ;
47
+ }
48
+ #endif
27
49
28
50
// create a producer socket
29
51
producer_socket = socket (AF_INET , SOCK_STREAM , 0 );
30
52
if (producer_socket < 0 ) {
31
53
fprintf (stderr , "[producer] ERROR: Unable to create socket\n" );
32
- return -1 ;
54
+ goto err_WSA_cleanup ;
33
55
}
34
56
35
57
fprintf (stderr , "[producer] Socket created\n" );
36
58
37
59
// set IP address and port the same as for the consumer
38
60
consumer_addr .sin_family = AF_INET ;
39
- consumer_addr .sin_port = htons (port );
61
+ consumer_addr .sin_port = htons (( u_short ) port );
40
62
consumer_addr .sin_addr .s_addr = inet_addr (INET_ADDR );
41
63
42
64
// send connection request to the consumer
@@ -49,10 +71,19 @@ int producer_connect_to_consumer(int port) {
49
71
50
72
fprintf (stderr , "[producer] Connected to the consumer\n" );
51
73
52
- return producer_socket ; // success
74
+ return ( int ) producer_socket ; // success
53
75
54
76
err_close_producer_socket_connect :
77
+ #ifdef _WIN32
78
+ closesocket (producer_socket );
79
+ #else
55
80
close (producer_socket );
81
+ #endif
82
+
83
+ err_WSA_cleanup :
84
+ #ifdef _WIN32
85
+ WSACleanup ();
86
+ #endif
56
87
57
88
return -1 ;
58
89
}
@@ -131,8 +162,8 @@ int main(int argc, char *argv[]) {
131
162
}
132
163
133
164
// send a size of the IPC_handle to the consumer
134
- ssize_t len =
135
- send ( producer_socket , & IPC_handle_size , sizeof (IPC_handle_size ), 0 );
165
+ ssize_t len = ( ssize_t ) send ( producer_socket , ( const char * ) & IPC_handle_size ,
166
+ sizeof (IPC_handle_size ), 0 );
136
167
if (len < 0 ) {
137
168
fprintf (stderr , "[producer] ERROR: unable to send the message\n" );
138
169
goto err_close_producer_socket ;
@@ -147,7 +178,7 @@ int main(int argc, char *argv[]) {
147
178
memset (recv_buffer , 0 , sizeof (recv_buffer ));
148
179
149
180
// receive the consumer's confirmation
150
- len = recv (producer_socket , recv_buffer , sizeof (recv_buffer ), 0 );
181
+ len = ( ssize_t ) recv (producer_socket , recv_buffer , sizeof (recv_buffer ), 0 );
151
182
if (len < 0 ) {
152
183
fprintf (stderr , "[producer] ERROR: error while receiving the "
153
184
"confirmation from the consumer\n" );
@@ -169,7 +200,8 @@ int main(int argc, char *argv[]) {
169
200
}
170
201
171
202
// send the IPC_handle of IPC_handle_size to the consumer
172
- len = send (producer_socket , IPC_handle , IPC_handle_size , 0 );
203
+ len = (ssize_t )send (producer_socket , (const char * )IPC_handle ,
204
+ (int )IPC_handle_size , 0 );
173
205
if (len < 0 ) {
174
206
fprintf (stderr , "[producer] ERROR: unable to send the message\n" );
175
207
goto err_close_producer_socket ;
@@ -221,7 +253,11 @@ int main(int argc, char *argv[]) {
221
253
}
222
254
223
255
err_close_producer_socket :
256
+ #ifdef _WIN32
257
+ closesocket (producer_socket );
258
+ #else
224
259
close (producer_socket );
260
+ #endif
225
261
226
262
err_PutIPCHandle :
227
263
umf_result = umfPutIPCHandle (IPC_handle );
0 commit comments