_________ __                 __
        /   _____//  |_____________ _/  |______     ____  __ __  ______
        \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
        /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ \
       /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
               \/                  \/          \//_____/            \/
    ______________________                           ______________________
                          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
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 
71 #ifdef DEBUG
72 
73  class CStatistic
74  {
75  friend class CUDPSocket;
76  public:
77  CStatistic();
78  void clear();
79  public:
80  unsigned int sentPacketsCount;
81  unsigned int receivedPacketsCount;
82  unsigned int sentBytesCount;
83  unsigned int receivedBytesCount;
84  unsigned int receivedErrorCount;
85  unsigned int receivedBytesExpectedCount;
86  unsigned int biggestSentPacketSize;
87  unsigned int biggestReceivedPacketSize;
88  };
89 
90  void clearStatistic() { m_statistic.clear(); }
91  const CStatistic &getStatistic() const { return m_statistic; }
92 private:
93  CStatistic m_statistic;
94 #endif
95 
96 private:
97  CUDPSocket_Impl *m_impl;
98 };
99 
100 // Class representing TCP socket used in communication
102 {
103 public:
104  CTCPSocket();
105  ~CTCPSocket();
106  bool Open(const CHost &host);
107  void Close();
108  bool Connect(const CHost &host);
109  int Send(const void *buf, unsigned int len);
110  int Recv(void *buf, int len);
111  void SetNonBlocking();
112  //
113  int HasDataToRead(int timeout);
114  bool IsValid() const;
115 private:
116  CTCPSocket_Impl *m_impl;
117 };
118 
120 
121 #endif // !NETSOCKETS_H
Definition: netsockets.cpp:69
void Close()
Definition: netsockets.cpp:231
CTCPSocket()
Definition: netsockets.cpp:216
void SetNonBlocking()
Definition: netsockets.cpp:163
void Send(const CHost &host, const void *buf, unsigned int len)
Definition: netsockets.cpp:137
~CUDPSocket()
Definition: netsockets.cpp:122
bool IsValid() const
Definition: netsockets.cpp:263
Definition: netsockets.h:101
unsigned long getIp() const
Definition: netsockets.h:42
~CTCPSocket()
Definition: netsockets.cpp:221
int Recv(void *buf, int len, CHost *hostFrom)
Definition: netsockets.cpp:147
int Recv(void *buf, int len)
Definition: netsockets.cpp:247
Definition: netsockets.cpp:182
Definition: netsockets.h:36
CHost(unsigned long ip, int port)
Definition: netsockets.h:41
CUDPSocket()
Definition: netsockets.cpp:117
int getPort() const
Definition: netsockets.h:43
void SetNonBlocking()
Definition: netsockets.cpp:253
bool operator!=(const CHost &rhs) const
Definition: netsockets.h:48
bool isValid() const
Definition: netsockets.cpp:60
std::string toString() const
Definition: netsockets.cpp:53
bool IsValid() const
Definition: netsockets.cpp:173
bool Open(const CHost &host)
Definition: netsockets.cpp:226
Definition: netsockets.h:57
int HasDataToRead(int timeout)
Definition: netsockets.cpp:258
bool Connect(const CHost &host)
Definition: netsockets.cpp:237
void Close()
Definition: netsockets.cpp:132
bool Open(const CHost &host)
Definition: netsockets.cpp:127
bool operator==(const CHost &rhs) const
Definition: netsockets.h:47
CHost()
Definition: netsockets.h:39
int Send(const void *buf, unsigned int len)
Definition: netsockets.cpp:242
int HasDataToRead(int timeout)
Definition: netsockets.cpp:168
(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.