-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathSocketHandler.cpp
More file actions
252 lines (208 loc) · 5.38 KB
/
Copy pathSocketHandler.cpp
File metadata and controls
252 lines (208 loc) · 5.38 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
Copyright (C) 2012 MoSync AB
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License,
version 2, as published by the Free Software Foundation.
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., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
/**
* @file SocketHandler.cpp
*
* Helpers for managing socket communication
* with the the Reload server.
*
* Author: Mikael Kindborg
*/
#include <maheap.h>
#include "SocketHandler.h"
#include "Convert.h"
#include "Log.h"
using namespace MAUtil;
SocketHandler::SocketHandler() :
mSocket(this)
{
// 17 = 9 (Magic header) + 8 (message size hex data).
mHeaderBufferSize = 17;
mMagicHeader = "RELOADMSG";
mReadState = 0;
mReadBuffer = NULL;
}
SocketHandler::~SocketHandler()
{
deallocateReadBuffer();
}
// ========== High-level public protocol ==========
void SocketHandler::setListener(SocketHandlerListener* listener)
{
mListener = listener;
}
int SocketHandler::connectToServer(
const MAUtil::String& serverAddress,
const MAUtil::String& port)
{
char buffer[512];
mSocket.close();
sprintf(buffer, "socket://%s:%s",
serverAddress.c_str(),
port.c_str());
LOG("@@@ RELOAD: Connect string: %s", buffer);
return mSocket.connect(buffer);
}
void SocketHandler::closeConnection()
{
mSocket.close();
// TODO: Should we call this? No, I think.
mListener->socketHandlerDisconnected(0);
}
void SocketHandler::sendMessage(const char* message)
{
// Get message size.
String messageLength = Convert::intToHex(strlen(message));
// Do we have a valid size string?
if (messageLength.length() != 8)
{
maPanic(0, "ReloadClient::sendTCPMessage: messageLength.length != 8");
}
// Create message data, starting with a magic header.
String data = mMagicHeader + messageLength + message;
// Queue the data.
mSendQueue.add(data);
// Send it directly if this is the only item to be sent.
// If there are elements waiting in the queue, they will
// be sent in connWriteFinished.
if (mSendQueue.size() == 1)
{
// Send the data.
mSocket.write(data.c_str(), data.length());
}
}
void SocketHandler::readMessageHeader()
{
mReadState = 1;
// Read header of next message sent from server.
readRawData(mHeaderBufferIn, mHeaderBufferSize);
}
/**
* Read next message sent from server.
* @param size Message size
*/
void SocketHandler::readMessage(int size)
{
mReadState = 2;
allocateReadBuffer(size);
// Read message sent from server.
readRawData(mReadBuffer, size);
}
// ========== ConnectionListener protocol ==========
/**
* The socket->connect() operation has completed.
*/
void SocketHandler::connectFinished(Connection* conn, int result)
{
if (result > 0)
{
// Call connected callback.
mListener->socketHandlerConnected(result);
// Read header of first message.
readMessageHeader();
}
else
{
// Call with error code.
mListener->socketHandlerConnected(result);
}
}
void SocketHandler::connReadFinished(Connection* conn, int result)
{
if (result > 0)
{
// Did we get the message header?
if (1 == mReadState)
{
// We have the header, get the message size.
mHeaderBufferIn[mHeaderBufferSize] = 0;
const char* sizeHeader = mHeaderBufferIn + mMagicHeader.length();
int size = Convert::hexToInt(sizeHeader);
LOG("@@@ SocketHandler::connReadFinished header size: %s %i",
sizeHeader, size);
// Read the actual message data.
readMessage(size);
}
// Did we get the message data?
else if (2 == mReadState)
{
// NULL-terminate message and call listener callback.
mReadBuffer[mReadSize] = 0;
// Read next message. The message can be Disconnect and
// close the socket. That's why read is called before
// handling the Message
readMessageHeader();
mListener->socketHandlerMessageReceived(mReadBuffer);
}
else
{
maPanic(0, "SocketHandler::connReadFinished: invalid read state");
}
}
else
{
// TODO: Should we call this?
mListener->socketHandlerDisconnected(result);
}
}
void SocketHandler::connRecvFinished(Connection* conn, int result)
{
// Not used.
}
void SocketHandler::connWriteFinished(Connection* conn, int result)
{
if (result > 0)
{
// Write has finished, remove written element (first in queue).
mSendQueue.remove(0);
// Is there more data waiting to be sent?
if (mSendQueue.size() > 0)
{
// Send first in queue.
mSocket.write((mSendQueue[0]).c_str(), (mSendQueue[0]).length());
}
}
else
{
// Empty the queue.
mSendQueue.clear();
// TODO: Should we call this?
mListener->socketHandlerDisconnected(result);
}
}
// ========== Low-level internal protocol ==========
void SocketHandler::deallocateReadBuffer()
{
if (mReadBuffer)
{
free(mReadBuffer);
mReadBuffer = NULL;
}
}
void SocketHandler::allocateReadBuffer(int size)
{
deallocateReadBuffer();
mReadSize = size;
// Make room for ending NULL terminator.
mReadBuffer = (char*) malloc(size + 1);
}
void SocketHandler::readRawData(char* buffer, int numBytes)
{
mSocket.read(buffer, numBytes);
}
void SocketHandler::sendRawData(const char* buffer, int numBytes)
{
mSocket.write(buffer, numBytes);
}