-
Notifications
You must be signed in to change notification settings - Fork 250
Expand file tree
/
Copy pathunit_test_tcp_server.cpp
More file actions
160 lines (132 loc) · 4.06 KB
/
unit_test_tcp_server.cpp
File metadata and controls
160 lines (132 loc) · 4.06 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
/*
* Copyright (c) 2014-2016, Freescale Semiconductor, Inc.
* Copyright 2016-2020 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "erpc_basic_codec.hpp"
#include "erpc_simple_server.hpp"
#include "erpc_tcp_transport.hpp"
#include "Logging.hpp"
#include "c_test_unit_test_common_server.h"
#include "myAlloc.hpp"
#include "test_unit_test_common_server.hpp"
#include "unit_test.h"
////////////////////////////////////////////////////////////////////////////////
// DEFINITIONS
////////////////////////////////////////////////////////////////////////////////
#ifndef UNIT_TEST_TCP_HOST
#define UNIT_TEST_TCP_HOST "localhost"
#endif
#ifndef UNIT_TEST_TCP_PORT
#define UNIT_TEST_TCP_PORT 12345
#endif
////////////////////////////////////////////////////////////////////////////////
// CODE
////////////////////////////////////////////////////////////////////////////////
using namespace erpc;
using namespace erpcShim;
class MyMessageBufferFactory : public MessageBufferFactory
{
public:
virtual MessageBuffer create()
{
uint8_t *buf = new uint8_t[1024];
return MessageBuffer(buf, 1024);
}
virtual void dispose(MessageBuffer *buf)
{
erpc_assert(buf);
if (*buf)
{
delete[] buf->get();
}
}
};
TCPTransport g_transport(UNIT_TEST_TCP_HOST, UNIT_TEST_TCP_PORT, true);
MyMessageBufferFactory g_msgFactory;
BasicCodecFactory g_basicCodecFactory;
SimpleServer g_server;
Crc16 g_crc16;
int ::MyAlloc::allocated_ = 0;
Common_service *svc_common;
////////////////////////////////////////////////////////////////////////////////
// Code
////////////////////////////////////////////////////////////////////////////////
int main(int argc, const char *argv[])
{
// create logger instance
StdoutLogger *m_logger = new StdoutLogger();
m_logger->setFilterLevel(Logger::log_level_t::kInfo);
Log::setLogger(m_logger);
Log::info("Starting ERPC server on port %d...\n", UNIT_TEST_TCP_PORT);
erpc_status_t result = g_transport.open();
if (result)
{
Log::error("Failed to open connection\n");
return 1;
}
g_transport.setCrc16(&g_crc16);
g_server.setMessageBufferFactory(&g_msgFactory);
g_server.setTransport(&g_transport);
g_server.setCodecFactory(&g_basicCodecFactory);
add_services(&g_server);
add_common_service(&g_server);
// run server infinitely
erpc_status_t err = g_server.run();
if (err && err != kErpcStatus_ServerIsDown)
{
Log::error("Error occurred: %d\n", err);
return err;
}
// Thread::sleep(10000000);
free(m_logger);
g_transport.close();
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// Common service implementations here
////////////////////////////////////////////////////////////////////////////////
void quit()
{
remove_common_service(&g_server);
remove_services(&g_server);
g_server.stop();
}
int32_t getServerAllocated()
{
int result = ::MyAlloc::allocated();
::MyAlloc::allocated(0);
return result;
}
class Common_server : public Common_interface
{
public:
void quit(void) { ::quit(); }
int32_t getServerAllocated(void)
{
int32_t result;
result = ::getServerAllocated();
return result;
}
};
////////////////////////////////////////////////////////////////////////////////
// Server helper functions
////////////////////////////////////////////////////////////////////////////////
void add_common_service(SimpleServer *server)
{
svc_common = new Common_service(new Common_server());
server->addService(svc_common);
}
void remove_common_service(SimpleServer *server)
{
server->removeService(svc_common);
delete svc_common->getHandler();
delete svc_common;
}
extern "C" void erpc_add_service_to_server(void *service) {}
extern "C" void erpc_remove_service_from_server(void *service) {}
////////////////////////////////////////////////////////////////////////////////
// EOF
////////////////////////////////////////////////////////////////////////////////