Project

General

Profile

Statistics
| Branch: | Revision:

root / include / platdep / sockets.h @ fbe00e73

History | View | Annotate | Download (2.27 KB)

1
//=========================================================================
2
//  PLATDEP/SOCKETS.H - part of
3
//
4
//                  OMNeT++/OMNEST
5
//           Discrete System Simulation in C++
6
//
7
//  Author: Andras Varga, 2005
8
//          Dept. of Electrical and Computer Systems Engineering,
9
//          Monash University, Melbourne, Australia
10
//
11
//=========================================================================
12

    
13
/*--------------------------------------------------------------*
14
  Copyright (C) 2005-2008 Andras Varga
15
  Copyright (C) 2006-2008 OpenSim Ltd.
16

17
  This file is distributed WITHOUT ANY WARRANTY. See the file
18
  `license' for details on this and other legal matters.
19
*--------------------------------------------------------------*/
20

    
21
#ifndef __PLATDEP_SOCKETS_H
22
#define __PLATDEP_SOCKETS_H
23

    
24
//
25
// With some care, it's possible to write platform-independent socket code
26
// using the macros below.
27
//
28

    
29
#ifdef _WIN32
30
//
31
// Winsock version
32
//
33

    
34
// include <winsock.h> or <winsock2.h> (mutually exclusive) if neither has been included yet
35
#if !defined(_WINSOCKAPI_) && !defined(_WINSOCK2API_)
36
# ifndef WANT_WINSOCK2
37
#  include <winsock.h>
38
# else
39
#  include <winsock2.h>
40
# endif
41
#endif
42
#undef min
43
#undef max
44

    
45
// Winsock prefixes error codes with "WS"
46
#define SOCKETERR(x)  WS#x
47
inline int sock_errno()  {return WSAGetLastError();}
48

    
49
// Shutdown mode constants are named differently (and only in winsock2.h
50
// which we don't want to pull in)
51
#define SHUT_RD   0x00  // as SD_RECEIVE in winsock2.h
52
#define SHUT_WR   0x01  // as SD_SEND in winsock2.h
53
#define SHUT_RDWR 0x02  // as SD_BOTH in winsock2.h
54

    
55
typedef int socklen_t;
56

    
57
inline int initsocketlibonce() {
58
    static bool inited = false;  //FIXME "static" and "inline" conflict!
59
    if (inited) return 0;
60
    inited = true;
61
    WSAData wsaData;
62
#ifdef _WINSOCKAPI_
63
    return WSAStartup(MAKEWORD(1,1), &wsaData);
64
#else
65
    return WSAStartup(MAKEWORD(2,2), &wsaData);
66
#endif
67
}
68

    
69

    
70
#else
71
//
72
// Unix version
73
//
74
#include <errno.h>
75
#include <sys/types.h>
76
#include <sys/socket.h>
77
#include <netinet/in.h>
78
#include <unistd.h>
79
#include <fcntl.h>
80

    
81
#define SOCKET int
82
inline int initsocketlibonce() {return 0;}
83
inline void closesocket(int) {}
84
inline int sock_errno()  {return errno;}
85
#define SOCKETERR(x)  x
86
#define INVALID_SOCKET -1
87
#define SOCKET_ERROR   -1
88

    
89
#endif
90

    
91
#endif