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

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 # ifndef __BEOS__
55 # include <net/if.h>
56 # include <arpa/inet.h>
57 # endif
58 # define INVALID_SOCKET -1
59 
60 #endif // !WIN32
61 
62 #ifndef INADDR_NONE
63 # define INADDR_NONE -1
64 #endif
65 
66 /*----------------------------------------------------------------------------
67 -- Defines
68 ----------------------------------------------------------------------------*/
69 
70 #define NIPQUAD(ad) \
71  (int)(((ad) >> 24) & 0xff), (int)(((ad) >> 16) & 0xff), \
72  (int)(((ad) >> 8) & 0xff), (int)((ad) & 0xff)
73 
74 #ifdef USE_WINSOCK
75 typedef SOCKET Socket;
76 #else
77 typedef int Socket;
78 #endif
79 
80 /*----------------------------------------------------------------------------
81 -- Declarations
82 ----------------------------------------------------------------------------*/
83 
84 class SocketSet
85 {
86 public:
87  SocketSet() : MaxSockFD(0) {}
88 
89  void AddSocket(Socket socket);
90  void DelSocket(Socket socket);
91 
93  int Select(int timeout);
95  int HasDataToRead(Socket socket) const;
96 
97 private:
98  std::vector<Socket> Sockets;
99  std::vector<int> SocketReady;
100  Socket MaxSockFD;
101 };
102 
103 /*----------------------------------------------------------------------------
104 -- Functions
105 ----------------------------------------------------------------------------*/
106 
108 extern int NetInit();
110 extern void NetExit();
111 
113 extern unsigned long NetResolveHost(const std::string &host);
115 extern int NetSocketAddr(const Socket sock, unsigned long *ips, int maxAddr);
116 
118 extern Socket NetOpenUDP(unsigned long ip, int port);
120 extern void NetCloseUDP(Socket sockfd);
122 extern int NetSendUDP(Socket sockfd, unsigned long host, int port, const void *buf, int len);
124 extern int NetRecvUDP(Socket sockfd, void *buf, int len, unsigned long *hostFrom, int *portFrom);
125 
126 
128 extern Socket NetOpenTCP(const char *addr, int port);
130 extern void NetCloseTCP(Socket sockfd);
132 extern int NetConnectTCP(Socket sockfd, unsigned long addr, int port);
134 extern int NetSendTCP(Socket sockfd, const void *buf, int len);
136 extern int NetRecvTCP(Socket sockfd, void *buf, int len);
138 extern int NetListenTCP(Socket sockfd);
140 extern Socket NetAcceptTCP(Socket sockfd, unsigned long *clientHost, int *clientPort);
141 
142 
144 extern int NetSetNonBlocking(Socket sockfd);
146 extern int NetSocketReady(Socket sockfd, int timeout);
147 
149 
150 #endif // !__NET_LOWLEVEL_H
Socket NetOpenTCP(const char *addr, int port)
Open a TCP Socket port.
Definition: net_lowlevel.cpp:392
void NetCloseTCP(Socket sockfd)
Close a TCP socket port.
Definition: net_lowlevel.cpp:169
int NetSendTCP(Socket sockfd, const void *buf, int len)
Send through a TCP socket.
Definition: net_lowlevel.cpp:646
int NetRecvTCP(Socket sockfd, void *buf, int len)
Receive from a TCP socket.
Definition: net_lowlevel.cpp:594
int NetListenTCP(Socket sockfd)
Listen for connections on a TCP socket.
Definition: net_lowlevel.cpp:658
Socket NetAcceptTCP(Socket sockfd, unsigned long *clientHost, int *clientPort)
Accept a connection on a TCP socket.
Definition: net_lowlevel.cpp:672
int NetSetNonBlocking(Socket sockfd)
Set socket to non-blocking.
Definition: net_lowlevel.cpp:190
unsigned long NetResolveHost(const std::string &host)
Resolve host in name or or colon dot notation.
Definition: net_lowlevel.cpp:202
void DelSocket(Socket socket)
Definition: net_lowlevel.cpp:700
int NetConnectTCP(Socket sockfd, unsigned long addr, int port)
Open a TCP connection.
Definition: net_lowlevel.cpp:433
int NetSocketAddr(const Socket sock, unsigned long *ips, int maxAddr)
Get local IP from network file descriptor.
Definition: net_lowlevel.cpp:344
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:624
void NetCloseUDP(Socket sockfd)
Close a UDP socket port.
Definition: net_lowlevel.cpp:159
int Select(int timeout)
Wait for socket set ready.
Definition: net_lowlevel.cpp:502
int NetRecvUDP(Socket sockfd, void *buf, int len, unsigned long *hostFrom, int *portFrom)
Receive from a UDP socket.
Definition: net_lowlevel.cpp:564
int HasDataToRead(Socket socket) const
Check if a socket in a socket set is ready.
Definition: net_lowlevel.cpp:542
int NetInit()
Hardware dependend network init.
Definition: net_lowlevel.cpp:142
SocketSet()
Definition: net_lowlevel.h:87
Socket NetOpenUDP(unsigned long ip, int port)
Open a UDP Socket port. (param in network format)
Definition: net_lowlevel.cpp:359
SOCKET Socket
Definition: net_lowlevel.h:75
void NetExit()
Hardware dependend network exit.
Definition: net_lowlevel.cpp:150
Definition: net_lowlevel.h:84
int NetSocketReady(Socket sockfd, int timeout)
Wait for socket ready.
Definition: net_lowlevel.cpp:468
void AddSocket(Socket socket)
Definition: net_lowlevel.cpp:688
(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.