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

netsockets.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 2013 by Joris Dauphin and cybermind
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 NETSOCKETS_H
30 #define NETSOCKETS_H
31 
32 #include <string>
33 
35 
36 class CHost
37 {
38 public:
39  CHost() : ip(0), port(0) {}
40  CHost(const char *name, int port);
41  CHost(unsigned long ip, int port) : ip(ip), port(port) {}
42  unsigned long getIp() const { return ip; }
43  int getPort() const { return port; }
44  std::string toString() const;
45  bool isValid() const;
46 
47  bool operator == (const CHost &rhs) const { return ip == rhs.ip && port == rhs.port; }
48  bool operator != (const CHost &rhs) const { return !(*this == rhs); }
49 private:
50  unsigned long ip;
51  int port;
52 };
53 
54 class CUDPSocket_Impl;
55 class CTCPSocket_Impl;
56 
58 {
59 public:
60  CUDPSocket();
61  ~CUDPSocket();
62  bool Open(const CHost &host);
63  void Close();
64  void Send(const CHost &host, const void *buf, unsigned int len);
65  int Recv(void *buf, int len, CHost *hostFrom);
66  void SetNonBlocking();
67  //
68  int HasDataToRead(int timeout);
69  bool IsValid() const;
70  int GetSocketAddresses(unsigned long *ips, int maxAddr);
71 
72 #ifdef DEBUG
73 
74  class CStatistic
75  {
76  friend class CUDPSocket;
77  public:
78  CStatistic();
79  void clear();
80  public:
81  unsigned int sentPacketsCount;
82  unsigned int receivedPacketsCount;
83  unsigned int sentBytesCount;
84  unsigned int receivedBytesCount;
85  unsigned int receivedErrorCount;
86  unsigned int receivedBytesExpectedCount;
87  unsigned int biggestSentPacketSize;
88  unsigned int biggestReceivedPacketSize;
89  };
90 
91  void clearStatistic() { m_statistic.clear(); }
92  const CStatistic &getStatistic() const { return m_statistic; }
93 private:
94  CStatistic m_statistic;
95 #endif
96 
97 private:
98  CUDPSocket_Impl *m_impl;
99 };
100 
101 // Class representing TCP socket used in communication
103 {
104 public:
105  CTCPSocket();
106  ~CTCPSocket();
107  bool Open(const CHost &host);
108  void Close();
109  bool Connect(const CHost &host);
110  int Send(const void *buf, unsigned int len);
111  int Recv(void *buf, int len);
112  void SetNonBlocking();
113  //
114  int HasDataToRead(int timeout);
115  bool IsValid() const;
116 private:
117  CTCPSocket_Impl *m_impl;
118 };
119 
121 
122 #endif // !NETSOCKETS_H
CUDPSocket::HasDataToRead
int HasDataToRead(int timeout)
Definition: netsockets.cpp:169
CTCPSocket::~CTCPSocket
~CTCPSocket()
Definition: netsockets.cpp:226
CTCPSocket_Impl
Definition: netsockets.cpp:187
CTCPSocket::Open
bool Open(const CHost &host)
Definition: netsockets.cpp:231
CTCPSocket::CTCPSocket
CTCPSocket()
Definition: netsockets.cpp:221
CHost::CHost
CHost()
Definition: netsockets.h:39
CTCPSocket::IsValid
bool IsValid() const
Definition: netsockets.cpp:268
CUDPSocket::SetNonBlocking
void SetNonBlocking()
Definition: netsockets.cpp:164
CTCPSocket::Recv
int Recv(void *buf, int len)
Definition: netsockets.cpp:252
CHost::getPort
int getPort() const
Definition: netsockets.h:43
CUDPSocket
Definition: netsockets.h:57
CHost::getIp
unsigned long getIp() const
Definition: netsockets.h:42
CHost::toString
std::string toString() const
Definition: netsockets.cpp:53
CTCPSocket::SetNonBlocking
void SetNonBlocking()
Definition: netsockets.cpp:258
CUDPSocket::IsValid
bool IsValid() const
Definition: netsockets.cpp:174
CTCPSocket::Send
int Send(const void *buf, unsigned int len)
Definition: netsockets.cpp:247
CHost::isValid
bool isValid() const
Definition: netsockets.cpp:60
CTCPSocket::HasDataToRead
int HasDataToRead(int timeout)
Definition: netsockets.cpp:263
CHost::CHost
CHost(unsigned long ip, int port)
Definition: netsockets.h:41
CUDPSocket::~CUDPSocket
~CUDPSocket()
Definition: netsockets.cpp:123
CUDPSocket::Send
void Send(const CHost &host, const void *buf, unsigned int len)
Definition: netsockets.cpp:138
CUDPSocket::Open
bool Open(const CHost &host)
Definition: netsockets.cpp:128
CUDPSocket_Impl
Definition: netsockets.cpp:69
CTCPSocket
Definition: netsockets.h:102
CUDPSocket::Recv
int Recv(void *buf, int len, CHost *hostFrom)
Definition: netsockets.cpp:148
CUDPSocket::GetSocketAddresses
int GetSocketAddresses(unsigned long *ips, int maxAddr)
Definition: netsockets.cpp:179
CTCPSocket::Connect
bool Connect(const CHost &host)
Definition: netsockets.cpp:242
CUDPSocket::CUDPSocket
CUDPSocket()
Definition: netsockets.cpp:118
CUDPSocket::Close
void Close()
Definition: netsockets.cpp:133
CHost::operator==
bool operator==(const CHost &rhs) const
Definition: netsockets.h:47
CHost::operator!=
bool operator!=(const CHost &rhs) const
Definition: netsockets.h:48
CTCPSocket::Close
void Close()
Definition: netsockets.cpp:236
CHost
Definition: netsockets.h:36
(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.