-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCPInterface.h
70 lines (57 loc) · 1.91 KB
/
TCPInterface.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
/**
* @file TCPInterface.h
* @brief Functions to manage and control the TCP Interface (the server component).
*/
#ifndef TCPINTERFACE_H
#define TCPINTERFACE_H
/*-------------------- INCLUDES --------------------*/
#include "Common.h"
// networking
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
/*-------------------- DEFINES --------------------*/
#define STREAMING_PORT_D 1401
#define STREAMING_PORT_C 1402
enum TCPInterfaceMode
{
TCPCLIENT = 1,
TCPSERVER = 2
};
/*-------------------- MACROS --------------------*/
// Check standard function return values (error if < 0) and throw an error if necessary
#define CHECK_TCPMODE(requiredMode) \
if (mMode != requiredMode) \
{ \
printf("ERROR: TCPInterface is not configured to perform this action.\n"); \
exit(EXIT_FAILURE); \
}
/*---------------- CLASS DEFINITION ----------------*/
class TCPInterface
{
public:
TCPInterface (TCPInterfaceMode mode, int port);
~TCPInterface (void);
void waitForServerConnection (void);
bool checkForClients (void);
int writeBytes (const void* bs, size_t n);
void readBytes (void* bs, size_t n);
private:
void setupServer (void);
void setupClient (const char* serverAddress);
void setBlocking (bool_t blocking);
void connectToServer (void);
void connectToClient (void);
void shutdownServer (void);
void shutdownClient (void);
TCPInterfaceMode mMode;
int mServerSocket;
int mClientSocket;
struct sockaddr_in mServerAddress;
struct sockaddr_in mClientAddress;
int mServerPort;
uint8_t mClientConnected;
};
#endif