Mana
Loading...
Searching...
No Matches
network.h
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include "utils/mutex.h"
25
26#include "net/serverinfo.h"
27
29#include "net/tmwa/messageout.h"
30
31#include <SDL_net.h>
32#include <SDL_thread.h>
33
34#include <map>
35#include <string>
36#include <unordered_map>
37
42#define CLIENT_PROTOCOL_VERSION 1
43// 10 -> 11: SMSG_MAP_MASK DONE
44
45namespace TmwAthena {
46
47struct PacketInfo;
48
50{
51 public:
52 Network();
53
54 ~Network();
55
56 bool connect(const ServerInfo &server);
57
58 void disconnect();
59
60 const ServerInfo &getServer() const
61 { return mServer; }
62
63 void registerHandler(MessageHandler *handler);
64
65 void unregisterHandler(MessageHandler *handler);
66
67 void clearHandlers();
68
69 const char *messageName(uint16_t id) const;
70
71 int getState() const { return mState; }
72
73 const std::string &getError() const { return mError; }
74
75 bool isConnected() const { return mState == CONNECTED; }
76
77 int getInSize() const { return mInSize; }
78
79 void skip(int len);
80
81 void dispatchMessages();
82
83 void flush();
84
85 // ERROR replaced by NET_ERROR because already defined in Windows
86 enum {
92 };
93
94 private:
95 friend int networkThread(void *data);
96 friend class MessageOut;
97
98 void setError(const std::string &error);
99
100 uint16_t readWord(int pos);
101
102 bool realConnect();
103
104 void receive();
105
106 TCPsocket mSocket = nullptr;
107
109
111 unsigned int mInSize = 0;
112 unsigned int mOutSize = 0;
113
114 unsigned int mToSkip = 0;
115
117 std::string mError;
118
119 SDL_Thread *mWorkerThread = nullptr;
121
122 std::unordered_map<uint16_t, const PacketInfo*> mPacketInfo;
123 std::map<uint16_t, MessageHandler *> mMessageHandlers;
124
126};
127
128} // namespace TmwAthena
A mutex provides mutual exclusion of access to certain data that is accessed by multiple threads.
Definition mutex.h:33
Used for building an outgoing message to eAthena.
Definition messageout.h:35
void setError(const std::string &error)
Definition network.cpp:585
bool isConnected() const
Definition network.h:75
unsigned int mOutSize
Definition network.h:112
bool connect(const ServerInfo &server)
Definition network.cpp:278
SDL_Thread * mWorkerThread
Definition network.h:119
const ServerInfo & getServer() const
Definition network.h:60
void skip(int len)
Definition network.cpp:445
void unregisterHandler(MessageHandler *handler)
Definition network.cpp:340
std::unordered_map< uint16_t, const PacketInfo * > mPacketInfo
Definition network.h:122
void registerHandler(MessageHandler *handler)
Definition network.cpp:332
unsigned int mInSize
Definition network.h:111
friend int networkThread(void *data)
Definition network.cpp:234
TCPsocket mSocket
Definition network.h:106
uint16_t readWord(int pos)
Definition network.cpp:592
void dispatchMessages()
Definition network.cpp:365
static Network * mInstance
Definition network.h:125
int getInSize() const
Definition network.h:77
std::map< uint16_t, MessageHandler * > mMessageHandlers
Definition network.h:123
const std::string & getError() const
Definition network.h:73
ServerInfo mServer
Definition network.h:108
int getState() const
Definition network.h:71
unsigned int mToSkip
Definition network.h:114
std::string mError
Definition network.h:117
const char * messageName(uint16_t id) const
Definition network.cpp:356
Warning: buffers and other variables are shared, so there can be only one connection active at a time...