-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtcpserver.h
94 lines (61 loc) · 2.1 KB
/
tcpserver.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
86
87
88
89
90
91
92
93
94
//
// Created by mteg on 5/6/18.
//
#ifndef CAPOC2_TCPSERVER_H
#define CAPOC2_TCPSERVER_H
#ifndef DUMMY_NET
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
/* This describes an I/O buffer */
struct tcpConnBuffer
{
unsigned int size; /* Number of bytes kept under mem */
unsigned int alloc; /* Amount of memory allocated for this buffer (size <= alloc) */
char *mem; /* The actual data */
};
struct tcpConn
{
struct tcpConn *next; /* Next connection in the linked list */
struct tcpServer *server;
int fd; /* Socket */
unsigned char state; /* Connection state - see below */
#ifndef DUMMY_NET
struct sockaddr_in peer; /* Peer IP address/port */
#endif
struct tcpConnBuffer i; /* Input buffer */
struct tcpConnBuffer o; /* Output buffer */
void *user_data;
};
#define STATE_OPEN 0
#define STATE_CLOSING 1
class tcpServer {
public:
tcpServer(int maxconn, int buffer_limit);
int listening_socket; /* File descriptor of the listening socket */
int conn_limit; /* Max # of simultaneous connections */
unsigned int buff_limit; /* Max amount of memory allocated for a single I/O buffer (1 connection = 2 buffers) */
int conn_count; /* Current number of connections */
void *user_data;
#define SERVER_PRINTF_BUFFER_SIZE 2048
char *printf_buffer;
struct tcpConn * connections; /* Linked list of connections */
int bind(int port, const char *address);
void assert(void *m);
int doNetwork();
int debug(const char *fmt, ...);
virtual int input(struct tcpConn *c, void *udata, char *buf, int len) = 0 ;
virtual void disconnect(struct tcpConn *c, void *udata) = 0;
virtual void * connect(struct tcpConn *c) = 0;
#ifndef DUMMY_NET
void updateFdSets(fd_set *rd, fd_set *wr, fd_set *ex, int *maxfd);
#endif
void writeTo(struct tcpConn *c, const void *ptr, size_t len);
void printfTo(tcpConn *c, char *fmt, ...);
void writeAll(const void *ptr, size_t len);
void drop(tcpConn *c);
void emergencyShutdown();
};
#define ALLOC_STEP 8192
#define SERVER_ALLOC_START 16384
#endif //CAPOC2_TCPSERVER_H