-
Notifications
You must be signed in to change notification settings - Fork 4
/
mgsession.c
272 lines (242 loc) · 8.22 KB
/
mgsession.c
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright (c) 2016-2020 Memgraph Ltd. [https://memgraph.com]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "mgsession.h"
#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Somehow stdalign.h is not there (VS Build Tools 2019, Windows 11)
// https://docs.microsoft.com/en-us/cpp/cpp/alignment-cpp-declarations?view=msvc-170
// EVERYWHERE EXCEPT MSVC
#if !defined(_WIN32) || !defined(_MSC_VER)
#include <stdalign.h>
#include <unistd.h>
#endif
#include "mgcommon.h"
#include "mgconstants.h"
#include "mgtransport.h"
int mg_session_status(const mg_session *session) {
if (!session) {
return MG_SESSION_BAD;
}
return session->status;
}
#define MG_DECODER_ALLOCATOR_BLOCK_SIZE 131072
// This seems like a reasonable value --- at most 4 kB per block is wasted
// (around 3% of allocated memory, not including padding), and separate
// allocations should only happen for really big objects (lists with more than
// 500 elements, maps with more than 250 elements, strings with more than 4000
// characters, ...).
#define MG_DECODER_SEP_ALLOC_THRESHOLD 4096
mg_session *mg_session_init(mg_allocator *allocator) {
mg_linear_allocator *decoder_allocator =
mg_linear_allocator_init(allocator, MG_DECODER_ALLOCATOR_BLOCK_SIZE,
MG_DECODER_SEP_ALLOC_THRESHOLD);
if (!decoder_allocator) {
return NULL;
}
mg_session *session = mg_allocator_malloc(allocator, sizeof(mg_session));
if (!session) {
mg_linear_allocator_destroy(decoder_allocator);
return NULL;
}
session->transport = NULL;
session->allocator = allocator;
session->decoder_allocator = (mg_allocator *)decoder_allocator;
session->out_buffer = NULL;
session->in_buffer = NULL;
session->out_capacity = MG_BOLT_CHUNK_HEADER_SIZE + MG_BOLT_MAX_CHUNK_SIZE;
session->out_buffer = mg_allocator_malloc(allocator, session->out_capacity);
if (!session->out_buffer) {
goto cleanup;
}
session->out_begin = MG_BOLT_CHUNK_HEADER_SIZE;
session->out_end = session->out_begin;
session->in_capacity = MG_BOLT_MAX_CHUNK_SIZE;
session->in_buffer = mg_allocator_malloc(allocator, session->in_capacity);
if (!session->in_buffer) {
goto cleanup;
}
session->in_end = 0;
session->in_cursor = 0;
session->result.session = session;
session->result.message = NULL;
session->result.columns = NULL;
session->explicit_transaction = 0;
session->query_number = 0;
session->error_buffer[0] = 0;
return session;
cleanup:
mg_linear_allocator_destroy(decoder_allocator);
mg_allocator_free(allocator, session->in_buffer);
mg_allocator_free(allocator, session->out_buffer);
mg_allocator_free(allocator, session);
return NULL;
}
void mg_session_set_error(mg_session *session, const char *fmt, ...) {
va_list arglist;
va_start(arglist, fmt);
if (vsnprintf(session->error_buffer, MG_MAX_ERROR_SIZE, fmt, arglist) < 0) {
strncpy(session->error_buffer, "couldn't set error message",
MG_MAX_ERROR_SIZE);
}
va_end(arglist);
}
const char *mg_session_error(mg_session *session) {
if (!session) {
return "session is NULL (possibly out of memory)";
}
return session->error_buffer;
}
void mg_session_invalidate(mg_session *session) {
if (session->transport) {
mg_transport_destroy(session->transport);
session->transport = NULL;
}
session->status = MG_SESSION_BAD;
}
void mg_session_destroy(mg_session *session) {
if (!session) {
return;
}
if (session->transport) {
mg_transport_destroy(session->transport);
}
mg_allocator_free(session->allocator, session->in_buffer);
mg_allocator_free(session->allocator, session->out_buffer);
mg_message_destroy_ca(session->result.message, session->decoder_allocator);
session->result.message = NULL;
mg_list_destroy_ca(session->result.columns, session->allocator);
session->result.columns = NULL;
mg_linear_allocator_destroy(
(mg_linear_allocator *)session->decoder_allocator);
mg_allocator_free(session->allocator, session);
}
int mg_session_flush_chunk(mg_session *session) {
size_t chunk_size = session->out_end - session->out_begin;
if (!chunk_size) {
return 0;
}
if (chunk_size > MG_BOLT_MAX_CHUNK_SIZE) {
abort();
}
// Actual chunk data is written with offset of two bytes, leaving 2 bytes for
// chunk size which we write here before sending.
assert(session->out_begin == MG_BOLT_CHUNK_HEADER_SIZE);
assert(MG_BOLT_CHUNK_HEADER_SIZE == sizeof(uint16_t));
*(uint16_t *)session->out_buffer = htobe16((uint16_t)chunk_size);
if (mg_transport_send(session->transport, session->out_buffer,
session->out_end) != 0) {
mg_session_set_error(session, "failed to send chunk data");
return MG_ERROR_SEND_FAILED;
}
session->out_end = session->out_begin;
return 0;
}
int mg_session_flush_message(mg_session *session) {
{
int status = mg_session_flush_chunk(session);
if (status != 0) {
return status;
}
}
const char MESSAGE_END[] = {0x00, 0x00};
{
int status =
mg_transport_send(session->transport, MESSAGE_END, sizeof(MESSAGE_END));
if (status != 0) {
mg_session_set_error(session, "failed to send message end marker");
return MG_ERROR_SEND_FAILED;
}
}
return 0;
}
int mg_session_write_raw(mg_session *session, const char *data, size_t len) {
size_t sent = 0;
while (sent < len) {
size_t buffer_free = session->out_capacity - session->out_end;
if (len - sent >= buffer_free) {
memcpy(session->out_buffer + session->out_end, data + sent, buffer_free);
session->out_end = session->out_capacity;
sent += buffer_free;
{
int status = mg_session_flush_chunk(session);
if (status != 0) {
return status;
}
}
} else {
memcpy(session->out_buffer + session->out_end, data + sent, len - sent);
session->out_end += len - sent;
sent = len;
}
}
return 0;
}
int mg_session_ensure_space_for_chunk(mg_session *session, size_t chunk_size) {
while (session->in_capacity - session->in_end < chunk_size) {
char *new_in_buffer = mg_allocator_realloc(
session->allocator, session->in_buffer, 2 * session->in_capacity);
if (!new_in_buffer) {
mg_session_set_error(session,
"failed to enlarge incoming message buffer");
return MG_ERROR_OOM;
}
session->in_capacity = 2 * session->in_capacity;
session->in_buffer = new_in_buffer;
}
return 0;
}
int mg_session_read_chunk(mg_session *session) {
uint16_t chunk_size;
mg_transport_suspend_until_ready_to_read(session->transport);
if (mg_transport_recv(session->transport, (char *)&chunk_size, 2) != 0) {
mg_session_set_error(session, "failed to receive chunk size");
return MG_ERROR_RECV_FAILED;
}
chunk_size = be16toh(chunk_size);
if (chunk_size == 0) {
return 0;
}
{
int status = mg_session_ensure_space_for_chunk(session, chunk_size);
if (status != 0) {
return status;
}
}
mg_transport_suspend_until_ready_to_read(session->transport);
if (mg_transport_recv(session->transport,
session->in_buffer + session->in_end,
chunk_size) != 0) {
mg_session_set_error(session, "failed to receive chunk data");
return MG_ERROR_RECV_FAILED;
}
session->in_end += chunk_size;
return 1;
}
int mg_session_receive_message(mg_session *session) {
// At this point, we reset the session decoder allocator and all objects from
// the previous message are lost.
mg_linear_allocator_reset((mg_linear_allocator *)session->decoder_allocator);
session->in_end = 0;
session->in_cursor = 0;
int status;
do {
status = mg_session_read_chunk(session);
} while (status == 1);
return status;
}