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