-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.cc
57 lines (48 loc) · 1.44 KB
/
client.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
#include "client.h"
#include <spdlog/spdlog.h>
#include <zmq.h>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include <vector>
#include "ib.h"
#include "log.h"
#include "zmq_helper.h"
ClientContext::ClientContext(IbContext &ib_ctx)
: ib_ctx_(ib_ctx), buf_(ib_ctx.buf_), buf_size_(ib_ctx.buf_size_) {
zmq_ctx_ = zmq_ctx_new();
RequireNotNull(zmq_ctx_, "zmq_ctx_new failed");
sock_ = zmq_socket(zmq_ctx_, ZMQ_REQ);
RequireNotNull(sock_, "zmq_socket failed");
}
ClientContext::~ClientContext() {
zmq_close(sock_);
zmq_ctx_destroy(zmq_ctx_);
}
void ClientContext::Init(unsigned int server_threads,
unsigned int client_threads, std::string ip,
unsigned int port) {
char url[80];
BuildZmqUrl(url, 80, ip, port);
RequireZero(zmq_connect(sock_, url), "zmq_connect failed");
for (unsigned int i = 0; i < client_threads; i++) {
auto qp_ctx = std::make_shared<QpContext>(ib_ctx_);
SendLocalInfo(sock_, *qp_ctx);
RecvRemoteInfo(sock_, *qp_ctx);
qp_ctx->Activate();
req_qps_.push_back(qp_ctx);
}
for (unsigned int i = 0; i < server_threads; i++) {
auto qp_ctx = std::make_shared<QpContext>(ib_ctx_);
SendLocalInfo(sock_, *qp_ctx);
RecvRemoteInfo(sock_, *qp_ctx);
qp_ctx->Activate();
rep_qps_.push_back(qp_ctx);
}
spdlog::info("Client is ready.");
}