_________ __                 __
        /   _____//  |_____________ _/  |______     ____  __ __  ______
        \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
        /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ \
       /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
               \/                  \/          \//_____/            \/
    ______________________                           ______________________
                          T H E   W A R   B E G I N S
                   Stratagus - A free fantasy real time strategy game engine

net_lowlevel.h
Go to the documentation of this file.
1 // _________ __ __
2 // / _____// |_____________ _/ |______ ____ __ __ ______
3 // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
4 // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
5 // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
6 // \/ \/ \//_____/ \/
7 // ______________________ ______________________
8 // T H E W A R B E G I N S
9 // Stratagus - A free fantasy real time strategy game engine
10 //
12 //
13 // (c) Copyright 1998-2007 by Lutz Sammer and Jimmy Salmon
14 //
15 // This program is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation; only version 2 of the License.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 // 02111-1307, USA.
28 
29 #ifndef __NET_LOWLEVEL_H
30 #define __NET_LOWLEVEL_H
31 
33 
34 /*----------------------------------------------------------------------------
35 -- Includes
36 ----------------------------------------------------------------------------*/
37 
38 #include <vector>
39 
40 // Include system network headers
41 #ifdef USE_WIN32
42 
43 # define USE_WINSOCK
44 
45 # include <winsock2.h>
46 
47 #else // UNIX
48 # include <sys/time.h>
49 # include <unistd.h>
50 # include <netinet/in.h>
51 # include <netdb.h>
52 # include <sys/socket.h>
53 # include <sys/ioctl.h>
54 # include <ifaddrs.h>
55 # ifndef __BEOS__
56 # include <net/if.h>
57 # include <arpa/inet.h>
58 # endif
59 # define INVALID_SOCKET -1
60 
61 #endif // !WIN32
62 
63 #ifndef INADDR_NONE
64 # define INADDR_NONE -1
65 #endif
66 
67 /*----------------------------------------------------------------------------
68 -- Defines
69 ----------------------------------------------------------------------------*/
70 
71 #define NIPQUAD(ad) \
72  (int)(((ad) >> 24) & 0xff), (int)(((ad) >> 16) & 0xff), \
73  (int)(((ad) >> 8) & 0xff), (int)((ad) & 0xff)
74 
75 #ifdef USE_WINSOCK
76 typedef SOCKET Socket;
77 #else
78 typedef int Socket;
79 #endif
80 
81 /*----------------------------------------------------------------------------
82 -- Declarations
83 ----------------------------------------------------------------------------*/
84 
85 class SocketSet
86 {
87 public:
88  SocketSet() : MaxSockFD(0) {}
89 
90  void AddSocket(Socket socket);
91  void DelSocket(Socket socket);
92 
94  int Select(int timeout);
96  int HasDataToRead(Socket socket) const;
97 
98 private:
99  std::vector<Socket> Sockets;
100  std::vector<int> SocketReady;
101  Socket MaxSockFD;
102 };
103 
104 /*----------------------------------------------------------------------------
105 -- Functions
106 ----------------------------------------------------------------------------*/
107 
109 extern int NetInit();
111 extern void NetExit();
112 
114 extern unsigned long NetResolveHost(const std::string &host);
116 extern int NetSocketAddr(unsigned long *ips, int maxAddr);
118 extern std::string NetGetHostname();
119 
121 extern Socket NetOpenUDP(unsigned long ip, int port);
123 extern void NetCloseUDP(Socket sockfd);
125 extern int NetSendUDP(Socket sockfd, unsigned long host, int port, const void *buf, int len);
127 extern int NetRecvUDP(Socket sockfd, void *buf, int len, unsigned long *hostFrom, int *portFrom);
128 
129 
131 extern Socket NetOpenTCP(const char *addr, int port);
133 extern void NetCloseTCP(Socket sockfd);
135 extern int NetConnectTCP(Socket sockfd, unsigned long addr, int port);
137 extern int NetSendTCP(Socket sockfd, const void *buf, int len);
139 extern int NetRecvTCP(Socket sockfd, void *buf, int len);
141 extern int NetListenTCP(Socket sockfd);
143 extern Socket NetAcceptTCP(Socket sockfd, unsigned long *clientHost, int *clientPort);
144 
145 
147 extern int NetSetNonBlocking(Socket sockfd);
149 extern int NetSocketReady(Socket sockfd, int timeout);
150 
152 
153 #endif // !__NET_LOWLEVEL_H
SocketSet::HasDataToRead
int HasDataToRead(Socket socket) const
Check if a socket in a socket set is ready.
Definition: net_lowlevel.cpp:495
NetGetHostname
std::string NetGetHostname()
Get local hostname.
Definition: net_lowlevel.cpp:212
NetOpenTCP
Socket NetOpenTCP(const char *addr, int port)
Open a TCP Socket port.
Definition: net_lowlevel.cpp:341
NetSendTCP
int NetSendTCP(Socket sockfd, const void *buf, int len)
Send through a TCP socket.
Definition: net_lowlevel.cpp:599
NetCloseTCP
void NetCloseTCP(Socket sockfd)
Close a TCP socket port.
Definition: net_lowlevel.cpp:159
SocketSet::Select
int Select(int timeout)
Wait for socket set ready.
Definition: net_lowlevel.cpp:455
NetListenTCP
int NetListenTCP(Socket sockfd)
Listen for connections on a TCP socket.
Definition: net_lowlevel.cpp:611
NetAcceptTCP
Socket NetAcceptTCP(Socket sockfd, unsigned long *clientHost, int *clientPort)
Accept a connection on a TCP socket.
Definition: net_lowlevel.cpp:625
NetRecvTCP
int NetRecvTCP(Socket sockfd, void *buf, int len)
Receive from a TCP socket.
Definition: net_lowlevel.cpp:547
NetResolveHost
unsigned long NetResolveHost(const std::string &host)
Resolve host in name or or colon dot notation.
Definition: net_lowlevel.cpp:192
SocketSet::SocketSet
SocketSet()
Definition: net_lowlevel.h:88
NetSetNonBlocking
int NetSetNonBlocking(Socket sockfd)
Set socket to non-blocking.
Definition: net_lowlevel.cpp:180
SocketSet::AddSocket
void AddSocket(Socket socket)
Definition: net_lowlevel.cpp:641
SocketSet::DelSocket
void DelSocket(Socket socket)
Definition: net_lowlevel.cpp:653
NetConnectTCP
int NetConnectTCP(Socket sockfd, unsigned long addr, int port)
Open a TCP connection.
Definition: net_lowlevel.cpp:386
NetCloseUDP
void NetCloseUDP(Socket sockfd)
Close a UDP socket port.
Definition: net_lowlevel.cpp:149
NetSendUDP
int NetSendUDP(Socket sockfd, unsigned long host, int port, const void *buf, int len)
Send through a UPD socket to a host:port.
Definition: net_lowlevel.cpp:577
NetRecvUDP
int NetRecvUDP(Socket sockfd, void *buf, int len, unsigned long *hostFrom, int *portFrom)
Receive from a UDP socket.
Definition: net_lowlevel.cpp:517
NetExit
void NetExit()
Hardware dependend network exit.
Definition: net_lowlevel.cpp:140
NetInit
int NetInit()
Hardware dependend network init.
Definition: net_lowlevel.cpp:132
NetSocketAddr
int NetSocketAddr(unsigned long *ips, int maxAddr)
Get local IPs.
Definition: net_lowlevel.cpp:270
NetOpenUDP
Socket NetOpenUDP(unsigned long ip, int port)
Open a UDP Socket port. (param in network format)
Definition: net_lowlevel.cpp:308
Socket
SOCKET Socket
Definition: net_lowlevel.h:76
SocketSet
Definition: net_lowlevel.h:85
NetSocketReady
int NetSocketReady(Socket sockfd, int timeout)
Wait for socket ready.
Definition: net_lowlevel.cpp:421
(C) Copyright 1998-2012 by The Stratagus Project under the GNU General Public License.
All trademarks and copyrights on this page are owned by their respective owners.