This repository was archived by the owner on Mar 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathoutputmessage.h
More file actions
173 lines (137 loc) · 4.62 KB
/
Copy pathoutputmessage.h
File metadata and controls
173 lines (137 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
//////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//////////////////////////////////////////////////////////////////////
#ifndef __OTSERV_OUTPUTMESSAGE_H__
#define __OTSERV_OUTPUTMESSAGE_H__
#include <cstddef>
#include <list>
#include <stdint.h>
#include <boost/thread/recursive_mutex.hpp>
#include "networkmessage.h"
class Connection;
class Protocol;
typedef boost::shared_ptr<Connection> Connection_ptr;
#define OUTPUT_POOL_SIZE 100
class OutputMessage : public NetworkMessage, boost::noncopyable
{
friend class OutputMessagePool;
OutputMessage();
public:
~OutputMessage();
char* getOutputBuffer();
void writeMessageLength();
void addCryptoHeader(bool addChecksum);
enum OutputMessageState{
STATE_FREE,
STATE_ALLOCATED,
STATE_ALLOCATED_NO_AUTOSEND,
STATE_WAITING
};
Protocol* getProtocol();
Connection_ptr getConnection();
uint64_t getFrame() const;
#ifdef __TRACK_NETWORK__
virtual void Track(std::string file, long line, std::string func)
{
if(last_uses.size() >= 25) {
last_uses.pop_front();
}
std::ostringstream os;
os << /*file << ":"*/ "line " << line << " " << func;
last_uses.push_back(os.str());
}
virtual void clearTrack()
{
last_uses.clear();
}
void PrintTrace()
{
int n = 1;
for(std::list<std::string>::const_reverse_iterator iter = last_uses.rbegin(); iter != last_uses.rend(); ++iter, ++n) {
std::cout << "\t" << n << ".\t" << *iter << std::endl;
}
}
#endif
protected:
#ifdef __TRACK_NETWORK__
std::list<std::string> last_uses;
#endif
template <typename T>
inline void add_header(T add){
if((int32_t)m_outputBufferStart - (int32_t)sizeof(T) < 0){
std::cout << "Error: [OutputMessage::add_header] m_outputBufferStart(" << m_outputBufferStart <<
") < " << sizeof(T) << std::endl;
return;
}
m_outputBufferStart = m_outputBufferStart - sizeof(T);
*(T*)(m_MsgBuf + m_outputBufferStart) = add;
//added header size to the message size
m_MsgSize = m_MsgSize + sizeof(T);
}
void freeMessage();
void setProtocol(Protocol* protocol);
void setConnection(Connection_ptr connection);
void setState(OutputMessageState state);
OutputMessageState getState() const;
void setFrame(uint64_t frame);
Protocol* m_protocol;
Connection_ptr m_connection;
uint32_t m_outputBufferStart;
uint64_t m_frame;
OutputMessageState m_state;
};
typedef boost::shared_ptr<OutputMessage> OutputMessage_ptr;
class OutputMessagePool
{
public:
OutputMessagePool();
~OutputMessagePool();
static OutputMessagePool* getInstance();
#ifdef __ENABLE_SERVER_DIAGNOSTIC__
static uint32_t OutputMessagePoolCount;
#endif
void send(OutputMessage_ptr msg);
void sendAll();
void stop();
OutputMessage_ptr getOutputMessage(Protocol* protocol, bool autosend = true);
void startExecutionFrame();
size_t getTotalMessageCount() const;
size_t getAvailableMessageCount() const;
size_t getAutoMessageCount() const;
void addToAutoSend(OutputMessage_ptr msg);
protected:
void configureOutputMessage(OutputMessage_ptr msg, Protocol* protocol, bool autosend);
void releaseMessage(OutputMessage* msg);
void internalReleaseMessage(OutputMessage* msg);
typedef std::list<OutputMessage*> InternalOutputMessageList;
typedef std::list<OutputMessage_ptr> OutputMessageMessageList;
InternalOutputMessageList m_outputMessages;
InternalOutputMessageList m_allOutputMessages;
OutputMessageMessageList m_autoSendOutputMessages;
OutputMessageMessageList m_toAddQueue;
boost::recursive_mutex m_outputPoolLock;
uint64_t m_frameTime;
bool m_isOpen;
};
#ifdef __TRACK_NETWORK__
#define TRACK_MESSAGE(omsg) (omsg)->Track(__FILE__, __LINE__, __FUNCTION__)
#else
#define TRACK_MESSAGE(omsg)
#endif
#endif