-
Notifications
You must be signed in to change notification settings - Fork 17
/
msg.h
91 lines (79 loc) · 1.53 KB
/
msg.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
#ifndef MSG_H
#define MSG_H
#include <queue>
#include "unp.h"
#include "unpthread.h"
#include "netheader.h"
#define MAXSIZE 10000
#define MB (1024*1024)
enum STATUS
{
CLOSE = 0,
ON = 1,
};
struct MSG
{
char *ptr;
int len;
int targetfd;
MSG_TYPE msgType;
uint32_t ip;
Image_Format format;
MSG()
{
}
MSG(MSG_TYPE msg_type, char *msg, int length, int fd)
{
msgType = msg_type;
ptr = msg;
len = length;
targetfd = fd;
}
};
struct SEND_QUEUE
{
private:
pthread_mutex_t lock;
pthread_cond_t cond;
std::queue<MSG> send_queue;
public:
SEND_QUEUE()
{
lock = PTHREAD_MUTEX_INITIALIZER;
cond = PTHREAD_COND_INITIALIZER;
}
void push_msg(MSG msg)
{
Pthread_mutex_lock(&lock);
while(send_queue.size() >= MAXSIZE)
{
Pthread_cond_wait(&cond, &lock);
}
send_queue.push(msg);
Pthread_mutex_unlock(&lock);
Pthread_cond_signal(&cond);
}
MSG pop_msg()
{
Pthread_mutex_lock(&lock);
while(send_queue.empty())
{
Pthread_cond_wait(&cond, &lock);
}
MSG msg = send_queue.front();
send_queue.pop();
Pthread_mutex_unlock(&lock);
Pthread_cond_signal(&cond);
return msg;
}
void clear()
{
Pthread_mutex_lock(&lock);
while(!send_queue.empty())
{
send_queue.pop();
}
Pthread_mutex_unlock(&lock);
}
};
#endif // MSG_H