Skip to content

Commit 8210ad0

Browse files
Merge pull request #51 from telerik/trifonov/optimize-message-receive
fix: optimize message receive for big messages
2 parents 1ef14bc + 6655a50 commit 8210ad0

File tree

2 files changed

+16
-34
lines changed

2 files changed

+16
-34
lines changed

IOSDeviceLib/SocketHelper.cpp

+14-27
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,22 @@ std::map<std::string, boost::any> receive_message(SOCKET socket, int timeout)
7575
return receive_message_core(socket);
7676
}
7777

78-
Utf16Message* create_utf16_message(unsigned char *message, long long length)
78+
Utf16Message* create_utf16_message(const std::string& message)
7979
{
8080
Utf16Message* result = new Utf16Message();
81-
result->length = length;
8281
result->message = message;
82+
8383
return result;
8484
}
8585

86-
Utf16Message* receive_utf16_message(SOCKET fd, int size = 1000)
86+
Utf16Message* receive_utf16_message(SOCKET fd, int size = 1024 * 1024)
8787
{
88-
long long final_length = 0;
8988
int bytes_read;
9089

9190
// We need to create new unsigned char[] here because if we don't
9291
// the memcpy will fail with EXC_BAD_ACCESS
93-
unsigned char *result = new unsigned char[0];
92+
std::string result;
93+
unsigned char *buffer = new unsigned char[size];
9494

9595
do
9696
{
@@ -99,7 +99,6 @@ Utf16Message* receive_utf16_message(SOCKET fd, int size = 1000)
9999
// the message can have length 2000 and we will try to call recv 3 times.
100100
// We will stay forever at the 3d one because there will be no message in the socket.
101101
int socket_state = get_socket_state(fd, 1);
102-
unsigned char *buffer = new unsigned char[size];
103102

104103
// If the MSG_PEEK flag is set the recv function won't read the data from the socket.
105104
// It will just return the size of the message in the socket.
@@ -111,10 +110,10 @@ Utf16Message* receive_utf16_message(SOCKET fd, int size = 1000)
111110
{
112111
delete[] buffer;
113112
// Socket is closed.
114-
if (final_length > 0)
113+
if (result.length() > 0)
115114
{
116115
// Return the last received message before the socket was closed.
117-
return create_utf16_message(result, final_length);
116+
return create_utf16_message(result);
118117
}
119118
else
120119
{
@@ -126,28 +125,17 @@ Utf16Message* receive_utf16_message(SOCKET fd, int size = 1000)
126125

127126
if (bytes_read > 0)
128127
{
129-
size_t temp_size = final_length + bytes_read;
130-
unsigned char *temp = new unsigned char[temp_size + 1];
131-
132-
memcpy(temp, result, final_length);
133-
memcpy(temp + final_length, buffer, bytes_read);
134-
135-
// Delete the result because we change it's address to temp's address.
136-
delete[] result;
137-
result = temp;
138-
final_length += bytes_read;
128+
result.append(buffer, buffer + bytes_read);
139129
}
140130
else if (bytes_read < 0)
141131
{
142132
delete[] buffer;
143-
delete[] result;
144133
return nullptr;
145134
}
146-
147-
delete[] buffer;
148135
} while (bytes_read == size);
149136

150-
return create_utf16_message(result, final_length);
137+
delete[] buffer;
138+
return create_utf16_message(result);
151139
}
152140

153141
void proxy_socket_messages(SOCKET first, SOCKET second)
@@ -156,15 +144,14 @@ void proxy_socket_messages(SOCKET first, SOCKET second)
156144

157145
while ((message = receive_utf16_message(first)) != nullptr)
158146
{
159-
if (message->length)
147+
if (message->message.length())
160148
{
161149
#ifdef _WIN32
162-
char* message_buffer = (char*)message->message;
150+
char* message_buffer = (char*)message->message.c_str();
163151
#else
164-
unsigned char* message_buffer = message->message;
152+
const char* message_buffer = message->message.c_str();
165153
#endif // _WIN32
166-
167-
send(second, message_buffer, message->length, NULL);
154+
send(second, message_buffer, message->message.length(), NULL);
168155
}
169156

170157
// Delete the message to free the message->message memory.

IOSDeviceLib/SocketHelper.h

+2-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ typedef unsigned long long SOCKET;
1717
#endif
1818

1919
#include <map>
20+
#include <string>
2021
#include <functional>
2122
#include "PlistCpp/include/boost/any.hpp"
2223

@@ -31,13 +32,7 @@ struct LengthEncodedMessage {
3132
};
3233

3334
struct Utf16Message {
34-
unsigned char *message;
35-
long long length;
36-
37-
~Utf16Message()
38-
{
39-
delete[] message;
40-
}
35+
std::string message;
4136
};
4237

4338
LengthEncodedMessage get_message_with_encoded_length(const char* message, long long length = -1);

0 commit comments

Comments
 (0)