-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.h
101 lines (79 loc) · 1.94 KB
/
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#ifndef connection_h
#define connection_h 1
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <cstddef>
#include <map>
#include "pollmgr.h"
class connection;
class chanmgr {
public:
virtual bool got_pdu(connection *c, char *b, int sz) = 0;
virtual ~chanmgr() {}
};
class connection : public aio_callback {
public:
struct charbuf {
charbuf(): buf(NULL), sz(0), solong(0) {}
charbuf (char *b, int s) : buf(b), sz(s), solong(0){}
char *buf;
int sz;
int solong; //amount of bytes written or read so far
};
connection(chanmgr *m1, int f1, int lossytest=0);
~connection();
int channo() { return fd_; }
bool isdead();
void closeconn();
bool send(char *b, int sz);
void write_cb(int s);
void read_cb(int s);
void incref();
void decref();
int ref();
int compare(connection *another);
private:
bool readpdu();
bool writepdu();
chanmgr *mgr_;
const int fd_;
bool dead_;
charbuf wpdu_;
charbuf rpdu_;
struct timeval create_time_;
int waiters_;
int refno_;
const int lossy_;
pthread_mutex_t m_;
pthread_mutex_t ref_m_;
pthread_cond_t send_complete_;
pthread_cond_t send_wait_;
};
class tcpsconn {
public:
tcpsconn(chanmgr *m1, int port, int lossytest=0);
~tcpsconn();
inline int port() { return port_; }
void accept_conn();
private:
int port_;
pthread_mutex_t m_;
pthread_t th_;
int pipe_[2];
int tcp_; //file desciptor for accepting connection
chanmgr *mgr_;
int lossy_;
std::map<int, connection *> conns_;
void process_accept();
};
struct bundle {
bundle(chanmgr *m, int s, int l):mgr(m),tcp(s),lossy(l) {}
chanmgr *mgr;
int tcp;
int lossy;
};
void start_accept_thread(chanmgr *mgr, int port, pthread_t *th, int *fd = NULL, int lossy=0);
connection *connect_to_dst(const sockaddr_in &dst, chanmgr *mgr, int lossy=0);
#endif