EUDAQ
 All Classes Namespaces Files Functions Variables Pages
TransportTCP_WIN32.h
1 #define MSG_NOSIGNAL 0
2 #ifndef EINPROGRESS
3 #define EINPROGRESS WSAEINPROGRESS
4 #endif
5 #ifndef EWOULDBLOCK
6 #define EWOULDBLOCK WSAEWOULDBLOCK
7 #endif
8 
9 #ifndef __CINT__
10 // using winsock2.h here would cause conflicts when including Windows4Root.h
11 // required e.g. by the ROOT online monitor
12 #include <winsock.h>
13 #endif
14 
15 #include <map>
16 
17 namespace eudaq {
18 
19  namespace {
20 
21  // List of Winsock error constants mapped to an interpretation string.
22  // Note that this list must remain sorted by the error constants'
23  // values, because we do a binary search on the list when looking up
24  // items.
25  std::pair<int, std::string> g_ErrorList[] = {
26  std::make_pair(0, "No error"),
27  std::make_pair(WSAEINTR, "Interrupted system call"),
28  std::make_pair(WSAEBADF, "Bad file number"),
29  std::make_pair(WSAEACCES, "Permission denied"),
30  std::make_pair(WSAEFAULT, "Bad address"),
31  std::make_pair(WSAEINVAL, "Invalid argument"),
32  std::make_pair(WSAEMFILE, "Too many open sockets"),
33  std::make_pair(WSAEWOULDBLOCK, "Operation would block"),
34  std::make_pair(WSAEINPROGRESS, "Operation now in progress"),
35  std::make_pair(WSAEALREADY, "Operation already in progress"),
36  std::make_pair(WSAENOTSOCK, "Socket operation on non-socket"),
37  std::make_pair(WSAEDESTADDRREQ, "Destination address required"),
38  std::make_pair(WSAEMSGSIZE, "Message too long"),
39  std::make_pair(WSAEPROTOTYPE, "Protocol wrong type for socket"),
40  std::make_pair(WSAENOPROTOOPT, "Bad protocol option"),
41  std::make_pair(WSAEPROTONOSUPPORT, "Protocol not supported"),
42  std::make_pair(WSAESOCKTNOSUPPORT, "Socket type not supported"),
43  std::make_pair(WSAEOPNOTSUPP, "Operation not supported on socket"),
44  std::make_pair(WSAEPFNOSUPPORT, "Protocol family not supported"),
45  std::make_pair(WSAEAFNOSUPPORT, "Address family not supported"),
46  std::make_pair(WSAEADDRINUSE, "Address already in use"),
47  std::make_pair(WSAEADDRNOTAVAIL, "Can't assign requested address"),
48  std::make_pair(WSAENETDOWN, "Network is down"),
49  std::make_pair(WSAENETUNREACH, "Network is unreachable"),
50  std::make_pair(WSAENETRESET, "Net connection reset"),
51  std::make_pair(WSAECONNABORTED, "Software caused connection abort"),
52  std::make_pair(WSAECONNRESET, "Connection reset by peer"),
53  std::make_pair(WSAENOBUFS, "No buffer space available"),
54  std::make_pair(WSAEISCONN, "Socket is already connected"),
55  std::make_pair(WSAENOTCONN, "Socket is not connected"),
56  std::make_pair(WSAESHUTDOWN, "Can't send after socket shutdown"),
57  std::make_pair(WSAETOOMANYREFS, "Too many references, can't splice"),
58  std::make_pair(WSAETIMEDOUT, "Connection timed out"),
59  std::make_pair(WSAECONNREFUSED, "Connection refused"),
60  std::make_pair(WSAELOOP, "Too many levels of symbolic links"),
61  std::make_pair(WSAENAMETOOLONG, "File name too long"),
62  std::make_pair(WSAEHOSTDOWN, "Host is down"),
63  std::make_pair(WSAEHOSTUNREACH, "No route to host"),
64  std::make_pair(WSAENOTEMPTY, "Directory not empty"),
65  std::make_pair(WSAEPROCLIM, "Too many processes"),
66  std::make_pair(WSAEUSERS, "Too many users"),
67  std::make_pair(WSAEDQUOT, "Disc quota exceeded"),
68  std::make_pair(WSAESTALE, "Stale NFS file handle"),
69  std::make_pair(WSAEREMOTE, "Too many levels of remote in path"),
70  std::make_pair(WSASYSNOTREADY, "Network system is unavailable"),
71  std::make_pair(WSAVERNOTSUPPORTED, "Winsock version out of range"),
72  std::make_pair(WSANOTINITIALISED, "WSAStartup not yet called"),
73  std::make_pair(WSAEDISCON, "Graceful shutdown in progress"),
74  std::make_pair(WSAHOST_NOT_FOUND, "Host not found"),
75  std::make_pair(WSANO_DATA, "No host data of that type was found")};
76  static const int k_NumMessages = sizeof g_ErrorList / sizeof *g_ErrorList;
77  std::map<int, std::string> g_ErrorCodes(g_ErrorList,
78  g_ErrorList + k_NumMessages);
79 
80  typedef int socklen_t;
81 
82  static int LastSockError() { return WSAGetLastError(); }
83 
84  static inline std::string LastSockErrorString(const std::string &msg) {
85  std::map<int, std::string>::const_iterator it =
86  g_ErrorCodes.find(WSAGetLastError());
87  std::string err =
88  (it == g_ErrorCodes.end() ? "Unknown Error" : it->second);
89  return msg + ": " + err;
90  }
91 
92  static void setup_socket(SOCKET sock) {
93  unsigned long one = 1;
94  ioctlsocket(sock, FIONBIO, &one);
95  }
96 
97  class WSAHelper {
98  public:
99  WSAHelper() {
100  WSAData wsadata;
101  WSAStartup(MAKEWORD(2, 2), &wsadata);
102  }
103  ~WSAHelper() { WSACleanup(); }
104  };
105  static WSAHelper theHelper;
106  }
107 }