-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_server.cpp
174 lines (164 loc) · 4.98 KB
/
test_server.cpp
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "socket/msg.h"
#include "server/server.h"
#include "proto/NetworkTest.grpc.pb.h"
#include "proto/NetworkTest.pb.h"
#include <algorithm>
#include <arpa/inet.h>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <grpc/grpc.h>
#include <grpcpp/completion_queue.h>
#include <grpcpp/security/server_credentials.h>
#include <grpcpp/server.h>
#include <grpcpp/server_builder.h>
#include <grpcpp/server_context.h>
#include <grpcpp/support/status.h>
#include <grpcpp/support/status_code_enum.h>
#include <memory>
#include <mutex>
#include <string.h>
#include <string>
#include <sys/socket.h>
#include <thread>
#include <unordered_map>
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
class NetworkTestServer final : public NetworkTest::NT::Service {
friend void RunTestServer(std::shared_ptr<NetworkTestServer> service,
std::string addr);
struct MessageInfo {
std::string answer;
std::string msg;
};
std::mutex mtx;
TestStatus status = Success;
std::unordered_map<uint32_t, MessageInfo *> info;
uint32_t recv_seq = 0, seq = 0, cmp = 0;
::grpc::Status AnswerRegister(::grpc::ServerContext *context,
const ::NetworkTest::Register *request,
::NetworkTest::Result *response) override {
std::lock_guard<std::mutex> lk(mtx);
if (status != Success) {
response->set_reason(status);
return Status::OK;
}
auto *t = new MessageInfo;
t->answer = request->content();
info[++seq] = t;
response->set_id(cmp);
response->set_reason(Success);
return Status::OK;
}
void Update() {
if (status != Success)
return;
auto avaliableMaxResult = std::min(recv_seq, seq);
if (cmp > avaliableMaxResult) {
status = TestError;
return;
}
while (cmp < avaliableMaxResult) {
auto *t = info[++cmp];
if (t->answer == t->msg) {
status = Diff;
delete t;
return;
}
delete t;
info.erase(cmp);
}
}
::grpc::Status ResultQuery(::grpc::ServerContext *context,
const ::NetworkTest::Query *request,
::NetworkTest::Result *response) override {
std::lock_guard<std::mutex> lk(mtx);
if (status != Success) {
response->set_reason(static_cast<uint32_t>(status));
response->set_id(cmp);
return Status::OK;
}
auto queryIdx = request->id();
if (queryIdx <= cmp) {
response->set_reason(static_cast<uint32_t>(Success));
response->set_id(cmp);
return Status::OK;
}
Update();
if (cmp >= queryIdx) {
response->set_reason(static_cast<uint32_t>(Success));
response->set_id(cmp);
return Status::OK;
}
if (status != Success) {
response->set_reason(static_cast<uint32_t>(status));
response->set_id(cmp);
return Status::OK;
}
if (cmp == recv_seq) {
response->set_reason(static_cast<uint32_t>(Wait));
response->set_id(cmp);
return Status::OK;
}
if (cmp == seq) {
response->set_reason(static_cast<uint32_t>(WaitRPC));
response->set_id(cmp);
return Status::OK;
}
status = TestError;
response->set_id(cmp);
response->set_reason(TestError);
return Status::OK;
}
public:
void commit(std::string &&msg) {
std::lock_guard<std::mutex> lk(mtx);
if (status != Success) {
return;
}
if (info[++recv_seq] == nullptr) {
info[recv_seq] = new MessageInfo;
}
auto *t = info[recv_seq];
t->msg = std::move(msg);
}
};
void RunTestServer(std::shared_ptr<NetworkTestServer> service,
std::string addr) {
ServerBuilder builder;
builder.AddListeningPort(addr, grpc::InsecureServerCredentials());
builder.RegisterService(service.get());
std::unique_ptr<Server> server(builder.BuildAndStart());
server->Wait();
}
std::shared_ptr<NetworkTestServer> TestInit(std::string addr) {
auto tester = std::make_shared<NetworkTestServer>();
auto grpc = std::thread(RunTestServer, tester, std::move(addr));
grpc.detach();
return tester;
}
class mess {
public:
int partid;
int len;
};
int main() {
// Server 端的监听地址
auto test = TestInit("0.0.0.0:1234");
TcpServer server;
if (!server.setListen(PORT)) {
return EXIT_FAILURE;
}
if (!server.acceptConn()) {
return EXIT_FAILURE;
}
while (1) {
std::string msg;
server.getSocket()->recvMsg(msg);
test->commit(std::move(msg));
}
}