-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
130 lines (113 loc) · 4.44 KB
/
common.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
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
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <cstdlib>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <fcntl.h>
#include <map>
#include "connection.h"
#define CHECK_EQUAL(verb, act_val, exp_val, cmd) if((exp_val) != (act_val)){ \
printf("Error in %s, expected value %d, actual value %d\n",(verb), (exp_val), (act_val)); \
if (errno != 0) printf("\"%s\"\n", strerror(errno));\
cmd; \
}
#define CHECK_NOT_EQUAL(verb, act_val, exp_val, cmd) if((exp_val) == (act_val)){ \
printf("Error in %s (errno = %s)\n", (verb), strerror(errno)); \
if (errno != 0) printf("\"%s\"\n", strerror(errno));\
cmd; \
}
#define WARM_UP_PERIOD 2 // Warm Up period in seconds
#define NUM_EXTRA_CONNECTION 40 // Number of connection to use with extra option.
#define SND_RCV_BUFFER_SIZE 4000 // Send and receive buffer size
enum extra_t {OPEN, CLOSE, NON};
struct config_t {
int server; /* Is server */
char server_ip[20]; /* Server's IP */
int server_port; /* Server's port */
int message_size; /* Message Size */
int mps; /* Message Rate Per Second */
int period; /* period to run test */
int number_connection; /* number of connection */
int warm; /* warm up period */
int tcp_nodelay; /* TCP_NODELAY option */
enum extra_t extra_op; /* Use extra connection for testing close, open or non. */
};
extern struct config_t config; // Test configuration
extern bool is_finished; // flag to indicate that test finished.
class runner_t {
public:
runner_t() : writable_socket_list(config.mps * (config.number_connection + NUM_EXTRA_CONNECTION)),
readable_socket_list(config.mps * (config.number_connection + NUM_EXTRA_CONNECTION)),
readable_connection_list(config.mps * (config.number_connection + NUM_EXTRA_CONNECTION))
{
msg_count = 0;
efd = -1;
};
virtual ~runner_t(){};
virtual void run() = 0; // The scenario for running (server - client) side.
virtual void print_result() = 0; // Print result of Test.
virtual void init_socket() = 0; // Initialize socket depending on its side (server -client).
virtual void handle_socket_write() = 0; // Handle available socket write (send) request that is filled in queue(FIFO).
virtual void handle_socket_read() = 0; // Handle available read (receive) request that is filled in queue(FIFO).
virtual void handle_connection_read() = 0; // Handle previously received messages that is filled in queue(FIFO).
void init_epoll();
int set_nonblocking_socket(int fd);
void add_new_connection(int fd);
void remove_connection(int fd);
void set_socket_option(int fd);
protected:
unsigned int msg_count;
int efd;
queue_t<connection_t*> writable_socket_list; // List of socket file descriptor that need to be write on.
queue_t<connection_t*> readable_socket_list; // List of socket file descriptor that need to be read from.
queue_t<connection_t*> readable_connection_list; // List of read socket file descriptor that need to be handled.
std::map<int, connection_t*> connection_list; // Map for each connection file descriptor and it buffers.
//Following are variable that used for temporary storage.
int fd, rc, data_size;
uint8_t* start_address;
connection_t* current_connection;
connection_buffer_t *read_buffer, *write_buffer;
};
class server_t : public runner_t{
public:
server_t();
virtual ~server_t();
void run();
void print_result();
void init_socket();
void handle_socket_write();
void handle_socket_read();
void handle_connection_read();
protected:
int lfd; // listen socket FD.
//Following are variable that used for temporary storage.
struct sockaddr_in cli_addr;
socklen_t cli_len;
unsigned int* msg_id;
};
class client_t : public runner_t {
public:
client_t();
virtual ~client_t();
void run();
void print_result();
void init_socket();
void handle_socket_write();
void handle_socket_read();
void handle_connection_read();
void open_new_socket();
void connect_new_socket(int sock_fd);
protected:
queue_t<int> socket_list; // List of open socket file descriptor.
unsigned int tick_counter;
bool connection_is_established;
unsigned int sent_message;
TicksDuration send_period;
};