-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy patherpc_simple_server.cpp
More file actions
241 lines (205 loc) · 5.27 KB
/
erpc_simple_server.cpp
File metadata and controls
241 lines (205 loc) · 5.27 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
/*
* Copyright (c) 2014, Freescale Semiconductor, Inc.
* Copyright 2016-2017 NXP
* Copyright 2019-2021 ACRIOS Systems s.r.o.
* All rights reserved.
*
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "erpc_simple_server.h"
using namespace erpc;
////////////////////////////////////////////////////////////////////////////////
// Code
////////////////////////////////////////////////////////////////////////////////
void SimpleServer::disposeBufferAndCodec(Codec *codec)
{
if (codec != NULL)
{
if (codec->getBuffer() != NULL)
{
m_messageFactory->dispose(codec->getBuffer());
}
m_codecFactory->dispose(codec);
}
}
erpc_status_t SimpleServer::runInternal(void)
{
MessageBuffer buff;
Codec *codec = NULL;
// Handle the request.
message_type_t msgType;
uint32_t serviceId;
Md5Hash methodId;
uint32_t sequence;
erpc_status_t err = runInternalBegin(&codec, buff, msgType, serviceId, methodId, sequence);
if (err == kErpcStatus_Success)
{
err = runInternalEnd(codec, msgType, serviceId, methodId, sequence);
}
return err;
}
erpc_status_t SimpleServer::runInternalBegin(Codec **codec, MessageBuffer &buff, message_type_t &msgType,
uint32_t &serviceId, Md5Hash methodId, uint32_t &sequence)
{
erpc_status_t err = kErpcStatus_Success;
if (m_messageFactory->createServerBuffer() == true)
{
buff = m_messageFactory->create();
if (!buff.get())
{
err = kErpcStatus_MemoryError;
}
}
// Receive the next invocation request.
if (err == kErpcStatus_Success)
{
err = m_transport->receive(&buff);
}
#if ERPC_PRE_POST_ACTION
pre_post_action_cb preCB = this->getPreCB();
if (preCB != NULL)
{
preCB();
}
#endif
#if ERPC_MESSAGE_LOGGING
if (err == kErpcStatus_Success)
{
err = logMessage(&buff);
}
#endif
if (err == kErpcStatus_Success)
{
*codec = m_codecFactory->create();
if (*codec == NULL)
{
err = kErpcStatus_MemoryError;
}
}
if (err != kErpcStatus_Success)
{
// Dispose of buffers.
if (buff.get() != NULL)
{
m_messageFactory->dispose(&buff);
}
}
if (err == kErpcStatus_Success)
{
(*codec)->setBuffer(buff);
err = readHeadOfMessage(*codec, msgType, serviceId, methodId, sequence);
if (err != kErpcStatus_Success)
{
// Dispose of buffers and codecs.
disposeBufferAndCodec(*codec);
}
}
return err;
}
erpc_status_t SimpleServer::runInternalEnd(Codec *codec, message_type_t msgType, uint32_t serviceId, Md5Hash methodId,
uint32_t sequence)
{
erpc_status_t err = processMessage(codec, msgType, serviceId, methodId, sequence);
if (err == kErpcStatus_Success)
{
if (msgType != kOnewayMessage)
{
#if ERPC_MESSAGE_LOGGING
err = logMessage(codec->getBuffer());
if (err == kErpcStatus_Success)
{
#endif
err = m_transport->send(codec->getBuffer());
#if ERPC_MESSAGE_LOGGING
}
#endif
}
#if ERPC_PRE_POST_ACTION
pre_post_action_cb postCB = this->getPostCB();
if (postCB != NULL)
{
postCB();
}
#endif
}
// Dispose of buffers and codecs.
disposeBufferAndCodec(codec);
return err;
}
erpc_status_t SimpleServer::run(void)
{
erpc_status_t err = kErpcStatus_Success;
while (!err && m_isServerOn)
{
err = runInternal();
}
return err;
}
#if ERPC_NESTED_CALLS
erpc_status_t SimpleServer::run(RequestContext &request)
{
erpc_status_t err = kErpcStatus_Success;
message_type_t msgType;
uint32_t serviceId;
uint32_t methodId;
uint32_t sequence;
while (!err && m_isServerOn)
{
MessageBuffer buff;
Codec *codec = NULL;
// Handle the request.
err = runInternalBegin(&codec, buff, msgType, serviceId, methodId, sequence);
if (err != kErpcStatus_Success)
{
break;
}
if (msgType == kReplyMessage)
{
if (sequence == request.getSequence())
{
// Swap the received message buffer with the client's message buffer.
request.getCodec()->getBuffer()->swap(&buff);
codec->setBuffer(buff);
}
// Dispose of buffers and codecs.
disposeBufferAndCodec(codec);
if (sequence != request.getSequence())
{
// Ignore message
continue;
}
break;
}
else
{
err = runInternalEnd(codec, msgType, serviceId, methodId, sequence);
}
}
return err;
}
#endif
erpc_status_t SimpleServer::poll(void)
{
erpc_status_t err;
if (m_isServerOn)
{
if (m_transport->hasMessage() == true)
{
err = runInternal();
}
else
{
err = kErpcStatus_Success;
}
}
else
{
err = kErpcStatus_ServerIsDown;
}
return err;
}
void SimpleServer::stop(void)
{
m_isServerOn = false;
}