forked from EmbeddedRPC/erpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
erpc_tcp_transport.hpp
156 lines (136 loc) · 4.69 KB
/
erpc_tcp_transport.hpp
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
/*
* Copyright (c) 2015, Freescale Semiconductor, Inc.
* Copyright 2016 NXP
* All rights reserved.
*
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef _EMBEDDED_RPC__TCP_TRANSPORT_H_
#define _EMBEDDED_RPC__TCP_TRANSPORT_H_
#if defined(__MINGW32__)
#include <winsock2.h>
#endif
#include "erpc_nocrc_framed_transport.hpp"
#include "erpc_threading.h"
/*!
* @addtogroup tcp_transport
* @{
* @file
*/
////////////////////////////////////////////////////////////////////////////////
// Classes
////////////////////////////////////////////////////////////////////////////////
namespace erpc {
/*!
* @brief Client side of TCP/IP transport.
*
* @ingroup tcp_transport
*/
class TCPTransport : public NoCRCFramedTransport
{
public:
/*!
* @brief Constructor.
*
* This function initializes object attributes.
*
* @param[in] isServer True when this transport is used for server side application.
*/
explicit TCPTransport(bool isServer);
/*!
* @brief Constructor.
*
* This function initializes object attributes.
*
* @param[in] host Specify the host name or IP address of the computer.
* @param[in] port Specify the listening port number.
* @param[in] isServer True when this transport is used for server side application.
*/
TCPTransport(const char *host, uint16_t port, bool isServer);
/*!
* @brief TCPTransport destructor
*/
virtual ~TCPTransport(void);
/*!
* @brief This function set host and port of this transport layer.
*
* @param[in] host Specify the host name or IP address of the computer.
* @param[in] port Specify the listening port number.
*/
void configure(const char *host, uint16_t port);
/*!
* @brief This function will create host on server side, or connect client to the server.
*
* @retval #kErpcStatus_Success When creating host was successful or client connected successfully.
* @retval #kErpcStatus_UnknownName Host name resolution failed.
* @retval #kErpcStatus_ConnectionFailure Connecting to the specified host failed.
*/
virtual erpc_status_t open(void);
/*!
* @brief This function disconnects client or stop server host.
*
* @param[in] stopServer Specify is server shall be closed as well (stop listen())
* @retval #kErpcStatus_Success Always return this.
*/
virtual erpc_status_t close(bool stopServer = true);
protected:
bool m_isServer; /*!< If true then server is using transport, else client. */
const char *m_host; /*!< Specify the host name or IP address of the computer. */
uint16_t m_port; /*!< Specify the listening port number. */
#if defined(__MINGW32__)
SOCKET m_socket; /*!< Socket number. */
#else
int m_socket; /*!< Socket number. */
#endif
Thread m_serverThread; /*!< Pointer to server thread. */
bool m_runServer; /*!< Thread is executed while this is true. */
using NoCRCFramedTransport::underlyingReceive;
using NoCRCFramedTransport::underlyingSend;
/*!
* @brief This function connect client to the server.
*
* @retval kErpcStatus_Success When client connected successfully.
* @retval kErpcStatus_Fail When client doesn't connected successfully.
*/
virtual erpc_status_t connectClient(void);
/*!
* @brief This function read data.
*
* @param[inout] data Preallocated buffer for receiving data.
* @param[in] size Size of data to read.
*
* @retval #kErpcStatus_Success When data was read successfully.
* @retval #kErpcStatus_ReceiveFailed When reading data ends with error.
* @retval #kErpcStatus_ConnectionClosed Peer closed the connection.
*/
virtual erpc_status_t underlyingReceive(uint8_t *data, uint32_t size) override;
/*!
* @brief This function writes data.
*
* @param[in] data Buffer to send.
* @param[in] size Size of data to send.
*
* @retval #kErpcStatus_Success When data was written successfully.
* @retval #kErpcStatus_SendFailed When writing data ends with error.
* @retval #kErpcStatus_ConnectionClosed Peer closed the connection.
*/
virtual erpc_status_t underlyingSend(const uint8_t *data, uint32_t size) override;
/*!
* @brief Server thread function.
*/
void serverThread(void);
/*!
* @brief Thread entry point.
*
* Control is passed to the serverThread() method of the TCPTransport instance pointed to
* by the @c arg parameter.
*
* @param arg Thread argument. The pointer to the TCPTransport instance is passed through
* this argument.
*/
static void serverThreadStub(void *arg);
};
} // namespace erpc
/*! @} */
#endif // _EMBEDDED_RPC__TCP_TRANSPORT_H_