a43e1d8a167a3aa61c5c165c0cd2d3f09ead7bd0
[simavr] / simavr / sim / sim_network.h
1 /*
2         sim_network.h
3
4         Copyright 2012 Stephan Veigl <veigl@gmx.net>
5
6         This file is part of simavr.
7
8         simavr is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 3 of the License, or
11         (at your option) any later version.
12
13         simavr is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #ifndef __SIM_NETWORK_H__
23 #define __SIM_NETWORK_H__
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 #ifdef __MINGW32__
30
31 // Windows with MinGW
32
33 #include <windows.h>
34 #include <winsock2.h>
35 #include <ws2tcpip.h>
36
37 #define send(sockfd, buf, len, flags) \
38         (ssize_t)send( (sockfd), (const char *)(buf), (len), (flags))
39 #define setsockopt(sockfd, level, optname, optval, optlen) \
40         setsockopt( (sockfd), (level), (optname), (void *)(optval), (optlen))
41 #define recv(sockfd, buf, len, flags) \
42         (ssize_t)recv( (sockfd), (char *)(buf), (len), (flags))
43 #define sleep(x) Sleep((x)*1000)
44
45 static inline int network_init()
46 {
47         // Windows requires WinSock to be init before use
48         WSADATA wsaData;
49         if ( WSAStartup( MAKEWORD( 2, 2 ), &wsaData ) )
50                 return -1;
51
52         return 0;
53 }
54
55 static inline void network_release()
56 {
57         // close WinSock
58         WSACleanup();
59 }
60
61 #else
62
63 // native Linux
64
65 #include <netinet/in.h>
66 #include <netinet/tcp.h>
67 #include <arpa/inet.h>
68 #include <sys/socket.h>
69 #include <sys/time.h>
70 #include <poll.h>
71
72 static inline int network_init()
73 {
74         // nothing to do
75         return 0;
76 }
77
78 static inline void network_release()
79 {
80         // nothing to do
81 }
82
83 #endif
84
85 #ifdef __cplusplus
86 };
87 #endif
88
89 #endif /*__SIM_NETWORK_H__*/