ManaPlus
network.h
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef NET_EA_NETWORK_H
25 #define NET_EA_NETWORK_H
26 
27 #include "net/serverinfo.h"
28 
29 PRAGMACLANG6GCC(GCC diagnostic push)
30 PRAGMACLANG6GCC(GCC diagnostic ignored "-Wold-style-cast")
31 #include "net/sdltcpnet.h"
32 PRAGMACLANG6GCC(GCC diagnostic pop)
33 
34 struct PacketInfo;
35 
36 namespace Ea
37 {
38 
40 {
41  public:
42  Network();
43 
45 
46  virtual ~Network();
47 
48  bool connect(const ServerInfo &server);
49 
50  void disconnect();
51 
53  { return mServer; }
54 
55  int getState() const A_WARN_UNUSED
56  { return mState; }
57 
58  const std::string &getError() const A_WARN_UNUSED
59  { return mError; }
60 
62  { return mState == CONNECTED; }
63 
65  { return mInSize; }
66 
67  void skip(const int len);
68 
69  void flush();
70 
71  void fixSendBuffer();
72 
74  { mPauseDispatch = true; }
75 
76  // ERROR replaced by NET_ERROR because already defined in Windows
77  enum
78  {
79  IDLE = 0,
83  NET_ERROR
84  };
85 
86  protected:
87  friend int networkThread(void *data);
88 
89  void setError(const std::string &error);
90 
91  uint16_t readWord(const int pos) const A_WARN_UNUSED;
92 
93  bool realConnect();
94 
95  void receive();
96 
98 
100 
102 
103  char *mInBuffer;
104  char *mOutBuffer;
105  unsigned int mInSize;
106  unsigned int mOutSize;
107 
108  unsigned int mToSkip;
109 
110  int mState;
111  std::string mError;
112 
113  SDL_Thread *mWorkerThread;
114  SDL_mutex *mMutexIn;
115  SDL_mutex *mMutexOut;
116  int mSleep;
118 };
119 
120 } // namespace Ea
121 
122 #endif // NET_EA_NETWORK_H
void receive()
Definition: network.cpp:244
bool isConnected() const
Definition: network.h:61
unsigned int mToSkip
Definition: network.h:108
SDL_Thread * mWorkerThread
Definition: network.h:113
void flush()
Definition: network.cpp:157
int getInSize() const
Definition: network.h:64
const std::string & getError() const
Definition: network.h:58
SDL_mutex * mMutexIn
Definition: network.h:114
friend int networkThread(void *data)
Definition: network.cpp:52
int mSleep
Definition: network.h:116
SDL_mutex * mMutexOut
Definition: network.h:115
@ NET_ERROR
Definition: network.h:83
@ CONNECTED
Definition: network.h:80
@ CONNECTING
Definition: network.h:81
bool connect(const ServerInfo &server)
Definition: network.cpp:101
virtual ~Network()
Definition: network.cpp:84
bool realConnect()
Definition: network.cpp:205
uint16_t readWord(const int pos) const
Definition: network.cpp:350
ServerInfo mServer
Definition: network.h:99
bool mPauseDispatch
Definition: network.h:117
std::string mError
Definition: network.h:111
void pauseDispatch()
Definition: network.h:73
unsigned int mOutSize
Definition: network.h:106
int mState
Definition: network.h:110
PacketInfo * mPackets
Definition: network.h:101
void fixSendBuffer()
Definition: network.cpp:362
char * mInBuffer
Definition: network.h:103
void setError(const std::string &error)
Definition: network.cpp:343
char * mOutBuffer
Definition: network.h:104
unsigned int mInSize
Definition: network.h:105
void skip(const int len)
Definition: network.cpp:181
TcpNet::Socket mSocket
Definition: network.h:97
int getState() const
Definition: network.h:55
void disconnect()
Definition: network.cpp:139
ServerInfo getServer() const
Definition: network.h:52
#define A_WARN_UNUSED
Definition: localconsts.h:161
#define notfinal
Definition: localconsts.h:261
#define A_DELETE_COPY(func)
Definition: localconsts.h:53
#define PRAGMACLANG6GCC(str)
Definition: localconsts.h:240
uint32_t data
bool error(InputEvent &event) __attribute__((noreturn))
Definition: actions.cpp:82
::TCPsocket Socket
Definition: sdltcpnet.h:38