@@ -75,22 +75,22 @@ std::map<std::string, boost::any> receive_message(SOCKET socket, int timeout)
75
75
return receive_message_core (socket);
76
76
}
77
77
78
- Utf16Message* create_utf16_message (unsigned char * message, long long length )
78
+ Utf16Message* create_utf16_message (const std::string& message)
79
79
{
80
80
Utf16Message* result = new Utf16Message ();
81
- result->length = length;
82
81
result->message = message;
82
+
83
83
return result;
84
84
}
85
85
86
- Utf16Message* receive_utf16_message (SOCKET fd, int size = 1000 )
86
+ Utf16Message* receive_utf16_message (SOCKET fd, int size = 1024 * 1024 )
87
87
{
88
- long long final_length = 0 ;
89
88
int bytes_read;
90
89
91
90
// We need to create new unsigned char[] here because if we don't
92
91
// 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];
94
94
95
95
do
96
96
{
@@ -99,7 +99,6 @@ Utf16Message* receive_utf16_message(SOCKET fd, int size = 1000)
99
99
// the message can have length 2000 and we will try to call recv 3 times.
100
100
// We will stay forever at the 3d one because there will be no message in the socket.
101
101
int socket_state = get_socket_state (fd, 1 );
102
- unsigned char *buffer = new unsigned char [size];
103
102
104
103
// If the MSG_PEEK flag is set the recv function won't read the data from the socket.
105
104
// 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)
111
110
{
112
111
delete[] buffer;
113
112
// Socket is closed.
114
- if (final_length > 0 )
113
+ if (result. length () > 0 )
115
114
{
116
115
// Return the last received message before the socket was closed.
117
- return create_utf16_message (result, final_length );
116
+ return create_utf16_message (result);
118
117
}
119
118
else
120
119
{
@@ -126,28 +125,17 @@ Utf16Message* receive_utf16_message(SOCKET fd, int size = 1000)
126
125
127
126
if (bytes_read > 0 )
128
127
{
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);
139
129
}
140
130
else if (bytes_read < 0 )
141
131
{
142
132
delete[] buffer;
143
- delete[] result;
144
133
return nullptr ;
145
134
}
146
-
147
- delete[] buffer;
148
135
} while (bytes_read == size);
149
136
150
- return create_utf16_message (result, final_length);
137
+ delete[] buffer;
138
+ return create_utf16_message (result);
151
139
}
152
140
153
141
void proxy_socket_messages (SOCKET first, SOCKET second)
@@ -156,15 +144,14 @@ void proxy_socket_messages(SOCKET first, SOCKET second)
156
144
157
145
while ((message = receive_utf16_message (first)) != nullptr )
158
146
{
159
- if (message->length )
147
+ if (message->message . length () )
160
148
{
161
149
#ifdef _WIN32
162
- char * message_buffer = (char *)message->message ;
150
+ char * message_buffer = (char *)message->message . c_str () ;
163
151
#else
164
- unsigned char * message_buffer = message->message ;
152
+ const char * message_buffer = message->message . c_str () ;
165
153
#endif // _WIN32
166
-
167
- send (second, message_buffer, message->length , NULL );
154
+ send (second, message_buffer, message->message .length (), NULL );
168
155
}
169
156
170
157
// Delete the message to free the message->message memory.
0 commit comments