Skip to content

Commit a8a7efd

Browse files
authored
Merge pull request #23 from IronsDu/addSomeWrapper
add wrapper for connect, listener
2 parents 1bd9ae1 + d5a5847 commit a8a7efd

6 files changed

Lines changed: 665 additions & 159 deletions

File tree

examples/TestHttp.cpp

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <brynet/net/http/HttpFormat.h>
1010
#include <brynet/net/http/WebSocketFormat.h>
1111
#include <brynet/net/Connector.h>
12+
#include <brynet/net/Wrapper.h>
1213

1314
using namespace brynet;
1415
using namespace brynet::net;
@@ -49,7 +50,6 @@ int main(int argc, char **argv)
4950
auto connector = brynet::net::AsyncConnector::Create();
5051
connector->startWorkerThread();
5152

52-
5353
auto listenThread = ListenThread::Create(false, "0.0.0.0", 8080, [service](TcpSocket::Ptr socket) {
5454
std::string body = "<html>hello world </html>";
5555
auto enterCallback = [body](const TcpConnection::Ptr& session) {
@@ -85,6 +85,19 @@ int main(int argc, char **argv)
8585
});
8686
listenThread->startListen();
8787

88+
wrapper::SocketConnectBuilder sb;
89+
auto s = sb.configureConnector(connector)
90+
.configureConnectOptions({
91+
AsyncConnector::ConnectOptions::WithTimeout(std::chrono::seconds(2)),
92+
AsyncConnector::ConnectOptions::WithAddr("127.0.0.1", 8010)
93+
})
94+
.syncConnect();
95+
s = sb.configureConnector(connector)
96+
.configureConnectOptions({
97+
AsyncConnector::ConnectOptions::WithAddr("127.0.0.1", 8080)
98+
})
99+
.syncConnect();
100+
88101
HttpRequest request;
89102
request.setMethod(HttpRequest::HTTP_METHOD::HTTP_METHOD_GET);
90103
request.setUrl("/ISteamUserAuth/AuthenticateUserTicket/v1/");
@@ -98,12 +111,12 @@ int main(int argc, char **argv)
98111

99112
std::string requestStr = request.getResult();
100113

101-
std::atomic<int> couner{0};
114+
std::atomic<int> couner{ 0 };
102115

103116
for (size_t i = 0; i < 10; i++)
104117
{
105-
reqHttp(service,
106-
connector,
118+
reqHttp(service,
119+
connector,
107120
requestStr,
108121
{
109122
AsyncConnector::ConnectOptions::WithAddr("23.73.140.64", 80),
@@ -115,6 +128,44 @@ int main(int argc, char **argv)
115128
std::cout << ++couner << std::endl;
116129
});
117130
}
131+
132+
wrapper::HttpConnectionBuilder()
133+
.configureConnector(connector)
134+
.configureService(service)
135+
.configureConnectOptions({
136+
AsyncConnector::ConnectOptions::WithAddr("180.97.33.108", 80),
137+
AsyncConnector::ConnectOptions::WithTimeout(std::chrono::seconds(10)),
138+
AsyncConnector::ConnectOptions::WithFailedCallback([]() {
139+
std::cout << "failed" << std::endl;
140+
}),
141+
})
142+
.configureConnectionOptions({
143+
TcpService::AddSocketOption::WithMaxRecvBufferSize(1024),
144+
TcpService::AddSocketOption::AddEnterCallback([](const TcpConnection::Ptr& session) {
145+
// do something for session
146+
std::cout << "success" << std::endl;
147+
})
148+
})
149+
.configureEnterCallback([](HttpSession::Ptr session) {
150+
std::cout << "success" << std::endl;
151+
})
152+
.asyncConnect();
153+
154+
wrapper::ListenerBuilder listenBuilder;
155+
listenBuilder.configureService(service)
156+
.configureSocketOptions({
157+
[](TcpSocket& socket) {
158+
socket.setNodelay();
159+
},
160+
})
161+
.configureConnectionOptions({
162+
TcpService::AddSocketOption::WithMaxRecvBufferSize(1024),
163+
})
164+
.configureListen([](wrapper::BuildListenConfig builder) {
165+
builder.setAddr(false, "0.0.0.0", 80);
166+
})
167+
.asyncRun();
168+
118169
std::cin.get();
119170
return 0;
120171
}

src/brynet/net/ListenThread.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
#include <brynet/net/SocketLibFunction.h>
55
#include <brynet/net/Noexcept.h>
66
#include <brynet/net/Socket.h>
7-
#include <brynet/net/SyncConnector.h>
7+
#include <brynet/net/Wrapper.h>
8+
#include <brynet/net/Connector.h>
89

910
#include <brynet/net/ListenThread.h>
1011

@@ -134,10 +135,18 @@ namespace brynet { namespace net {
134135
{
135136
selfIP = "127.0.0.1";
136137
}
137-
;
138-
brynet::net::SyncConnectSocket({
139-
AsyncConnector::ConnectOptions::WithAddr(selfIP, mPort),
140-
AsyncConnector::ConnectOptions::WithTimeout(std::chrono::seconds(10))});
138+
139+
auto connector = AsyncConnector::Create();
140+
connector->startWorkerThread();
141+
142+
wrapper::SocketConnectBuilder connectBuilder;
143+
connectBuilder
144+
.configureConnector(connector)
145+
.configureConnectOptions({
146+
AsyncConnector::ConnectOptions::WithTimeout(std::chrono::seconds(2)),
147+
AsyncConnector::ConnectOptions::WithAddr(selfIP, mPort)
148+
})
149+
.syncConnect();
141150

142151
try
143152
{

src/brynet/net/SyncConnector.cpp

Lines changed: 0 additions & 80 deletions
This file was deleted.

src/brynet/net/SyncConnector.h

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)