-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_connection.h
85 lines (52 loc) · 2.13 KB
/
tcp_connection.h
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
//---------------------------------------------------------------------------------------------------------------------
/*!
\file
\brief Non blocking TCP connection class
*/
//---------------------------------------------------------------------------------------------------------------------
#ifndef TCP_CONNECTION_H_INCLUDED
#define TCP_CONNECTION_H_INCLUDED
/* -- Includes ------------------------------------------------------------ */
#include <stdint.h>
#include <netinet/in.h>
/* -- Defines ------------------------------------------------------------- */
/* -- Types --------------------------------------------------------------- */
class NbTcpConnection
{
public:
NbTcpConnection();
NbTcpConnection(int sock, const struct sockaddr_in * address);
bool isOpen();
//returns number of received data bytes; 0 when nothing was received; -1 in case of connection errors
int recv(uint8_t * buffer, size_t bufferLen);
//returns number of sent data bytes; -1 in case of connection errors
int send(const uint8_t * data, size_t dataLen, bool more=false);
void close();
protected:
int sock; //file descriptor of socket
struct sockaddr_in address; //remote connection address
};
class NbTcpServer : public NbTcpConnection
{
public:
NbTcpServer();
//open a non blocking tcp server connection, listening to the given port
//returns positive number on success; -1 in case of errors
int open(const uint16_t port);
//call this function cyclically to accept incomming connections
//returns pointer to accepted connection; NULL otherwise
NbTcpConnection * serve();
private:
};
class NbTcpClient : public NbTcpConnection
{
NbTcpClient();
//open a non blocking tcp client connection
//returns positive number when connection is establised; -1 in case of errors
int open(const char * ip, const uint16_t port);
private:
};
/* -- Global Variables ---------------------------------------------------- */
/* -- Function Prototypes ------------------------------------------------- */
/* -- Implementation ------------------------------------------------------ */
#endif // TCPCONNECT_H_INCLUDED