|
9 | 9 | #include <brynet/net/http/HttpFormat.h> |
10 | 10 | #include <brynet/net/http/WebSocketFormat.h> |
11 | 11 | #include <brynet/utils/packet.h> |
| 12 | +#include <brynet/net/Wrapper.h> |
12 | 13 |
|
13 | 14 | using namespace brynet; |
14 | 15 | using namespace brynet::net; |
@@ -51,49 +52,59 @@ int main(int argc, char **argv) |
51 | 52 | size_t workers = argc > 4 ? std::atoi(argv[4]) : std::thread::hardware_concurrency(); |
52 | 53 |
|
53 | 54 | std::cout << "host: " << host << ':' << port << " | connections: " << connections << " | workers: " << workers << std::endl; |
54 | | - |
55 | | - auto service = TcpService::Create(); |
56 | | - service->startWorkerThread(workers); |
57 | | - |
58 | | - for (int i = 0; i < connections; i++) |
59 | | - { |
60 | | - sock fd = brynet::net::base::Connect(false, host, port); |
61 | | - auto socket = TcpSocket::Create(fd, false); |
62 | | - brynet::net::base::SocketNodelay(fd); |
63 | 55 |
|
64 | | - auto enterCallback = [host](const TcpConnection::Ptr& session) { |
65 | | - HttpService::setup(session, [host](const HttpSession::Ptr& httpSession) { |
66 | | - HttpRequest request; |
67 | | - request.setMethod(HttpRequest::HTTP_METHOD::HTTP_METHOD_GET); |
68 | | - request.setUrl("/ws"); |
69 | | - request.addHeadValue("Host", host); |
70 | | - request.addHeadValue("Upgrade", "websocket"); |
71 | | - request.addHeadValue("Connection", "Upgrade"); |
72 | | - request.addHeadValue("Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ=="); |
73 | | - request.addHeadValue("Sec-WebSocket-Version", "13"); |
| 56 | + auto enterCallback = [host](const HttpSession::Ptr& httpSession) { |
| 57 | + HttpRequest request; |
| 58 | + request.setMethod(HttpRequest::HTTP_METHOD::HTTP_METHOD_GET); |
| 59 | + request.setUrl("/ws"); |
| 60 | + request.addHeadValue("Host", host); |
| 61 | + request.addHeadValue("Upgrade", "websocket"); |
| 62 | + request.addHeadValue("Connection", "Upgrade"); |
| 63 | + request.addHeadValue("Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ=="); |
| 64 | + request.addHeadValue("Sec-WebSocket-Version", "13"); |
74 | 65 |
|
75 | | - std::string requestStr = request.getResult(); |
76 | | - httpSession->send(requestStr.c_str(), requestStr.size()); |
| 66 | + std::string requestStr = request.getResult(); |
| 67 | + httpSession->send(requestStr.c_str(), requestStr.size()); |
77 | 68 |
|
78 | | - httpSession->setWSConnected([](const HttpSession::Ptr& session, const HTTPParser&) { |
79 | | - for (int i = 0; i < 200; i++) |
80 | | - { |
81 | | - sendPacket(session, "hello, world!", 13); |
82 | | - } |
83 | | - }); |
84 | | - |
85 | | - httpSession->setWSCallback([](const HttpSession::Ptr& session, |
86 | | - WebSocketFormat::WebSocketFrameType, const std::string& payload) { |
87 | | - std::cout << payload << std::endl; |
| 69 | + httpSession->setWSConnected([](const HttpSession::Ptr& session, const HTTPParser&) { |
| 70 | + for (int i = 0; i < 200; i++) |
| 71 | + { |
88 | 72 | sendPacket(session, "hello, world!", 13); |
89 | | - count += 1; |
90 | | - }); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + httpSession->setWSCallback([](const HttpSession::Ptr& session, |
| 77 | + WebSocketFormat::WebSocketFrameType, const std::string& payload) { |
| 78 | + std::cout << payload << std::endl; |
| 79 | + sendPacket(session, "hello, world!", 13); |
| 80 | + count += 1; |
91 | 81 | }); |
92 | | - }; |
| 82 | + }; |
| 83 | + |
| 84 | + auto service = TcpService::Create(); |
| 85 | + service->startWorkerThread(workers); |
93 | 86 |
|
94 | | - service->addTcpConnection(std::move(socket), |
95 | | - brynet::net::TcpService::AddSocketOption::AddEnterCallback(enterCallback), |
96 | | - brynet::net::TcpService::AddSocketOption::WithMaxRecvBufferSize(1024*1024)); |
| 87 | + auto connector = AsyncConnector::Create(); |
| 88 | + connector->startWorkerThread(); |
| 89 | + |
| 90 | + wrapper::HttpConnectionBuilder connectionBuilder; |
| 91 | + connectionBuilder.configureService(service) |
| 92 | + .configureConnector(connector) |
| 93 | + .configureConnectionOptions({ |
| 94 | + brynet::net::TcpService::AddSocketOption::WithMaxRecvBufferSize(1024 * 1024) |
| 95 | + }); |
| 96 | + |
| 97 | + for (int i = 0; i < connections; i++) |
| 98 | + { |
| 99 | + connectionBuilder.configureConnectOptions({ |
| 100 | + AsyncConnector::ConnectOptions::WithAddr(host, port), |
| 101 | + AsyncConnector::ConnectOptions::WithTimeout(std::chrono::seconds(10)), |
| 102 | + AsyncConnector::ConnectOptions::AddProcessTcpSocketCallback([](TcpSocket& socket) { |
| 103 | + socket.setNodelay(); |
| 104 | + }) |
| 105 | + }) |
| 106 | + .configureEnterCallback(enterCallback) |
| 107 | + .asyncConnect(); |
97 | 108 | } |
98 | 109 |
|
99 | 110 | brynet::net::EventLoop mainLoop; |
|
0 commit comments