-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.cc
136 lines (131 loc) · 3.06 KB
/
connection.cc
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
130
131
132
133
134
135
136
#include "connection.h"
#define BUFFER_SIZE 65536
#define CHUNK_SIZE 1024
Connection::Connection(int fd_) : fd(fd_) {
in = new BipBuffer(BUFFER_SIZE);
out = new BipBuffer(BUFFER_SIZE);
}
void Connection::handle(RingBuffer<Order> *ring, Train *train) {
char *index_in, *index_out;
int n;
Order *order;
long sequence = -1;
int start, end;
index_in = in->reserve(CHUNK_SIZE);
if (index_in == nullptr) {
perror("in reserve error");
exit(1);
}
if ((n = recv(fd, index_in, CHUNK_SIZE, 0)) == -1) {
perror("connection recv");
exit(1);
}
in->commit(n);
index_in = in->get(n);
// test-start
/* Order::check(index_in, n, start, end); */
/* sequence = ring->next(1); */
/* order = ring->get(sequence); */
/* order->parse(index_in + start, end - start + 1); */
/* if (send(fd, index_in, n, 0) == -1) { */
/* perror("connection send"); */
/* exit(1); */
/* } */
/* in->release(n); */
/* return; */
/* // test-end */
int count = 0;
while (n >= 12) {
if (Order::check(index_in, n, start, end)) {
sequence = ring->next(1);
order = ring->get(sequence);
if (order->parse(index_in + start, end - start + 1) == -1) {
cout << "order parse fail" << endl;
exit(1);
}
order->connection = this;
/* // test-start */
/* switch (order->operation) { */
/* case 0x01: */
/* train->query(order); */
/* break; */
/* case 0x02: */
/* train->book(order); */
/* break; */
/* case 0x03: */
/* train->check(order); */
/* break; */
/* case 0x04: */
/* train->refund(order); */
/* break; */
/* default: */
/* cout << "unknown operation " << hex << order->operation << endl; */
/* exit(1); */
/* } */
/* if (order->dump() == -1) { */
/* cout << "order dump fail" << endl; */
/* exit(1); */
/* } */
/* index_out = out->reserve(order->rsize); */
/* if (index_out == nullptr) { */
/* perror("in reserve error"); */
/* exit(1); */
/* } */
/* memcpy(index_out, order->raw, order->rsize); */
/* out->commit(order->rsize); */
/* // test-end */
in->release(end);
count++;
} else {
if (end != 0) {
cout << dec << start << "-" << end << " bytes unknown" << endl;
Util::print_array(index_in, end);
}
in->release(end);
if (end == 0) {
// not enough data, wait
break;
}
}
n -= end;
index_in += end;
}
/* // test-start */
/* /\* cout << dec << count << " orders got" << endl; *\/ */
/* index_out = out->get(n); */
/* while (n > 0) { */
/* if (send(fd, index_out, n, 0) == -1) { */
/* perror("connection send"); */
/* exit(1); */
/* } */
/* out->release(n); */
/* index_out = out->get(n); */
/* } */
/* // test-end */
if (sequence != -1) {
ring->publish(sequence);
}
}
void Connection::flush() {
char *index;
int n;
while (true) {
index = out->get(n);
if (n > 0) {
// TODO: should handle EAGAIN
if (send(fd, index, n, 0) == -1) {
perror("connection send");
exit(1);
}
out->release(n);
} else {
break;
}
}
}
void Connection::close_connection() {
if (close(fd) == -1) {
perror("close connection");
exit(1);
}
}