| 12 | using namespace brynet::net; |
| 13 | |
| 14 | int main(int argc, char** argv) |
| 15 | { |
| 16 | if (argc != 4) |
| 17 | { |
| 18 | fprintf(stderr, "Usage: <listen port> <backend ip> <backend port>"); |
| 19 | exit(-1); |
| 20 | } |
| 21 | |
| 22 | int bindPort = atoi(argv[1]); |
| 23 | string backendIP = argv[2]; |
| 24 | int backendPort = atoi(argv[3]); |
| 25 | |
| 26 | auto tcpService = IOThreadTcpService::Create(); |
| 27 | tcpService->startWorkerThread(std::thread::hardware_concurrency()); |
| 28 | |
| 29 | auto asyncConnector = AsyncConnector::Create(); |
| 30 | asyncConnector->startWorkerThread(); |
| 31 | |
| 32 | auto listenThread = ListenThread::Create(false, |
| 33 | "0.0.0.0", |
| 34 | bindPort, |
| 35 | [tcpService, asyncConnector, backendIP, backendPort](TcpSocket::Ptr socket) { |
| 36 | auto status = std::make_shared<bool>(true); |
| 37 | auto enterCallback = [tcpService, asyncConnector, backendIP, backendPort, status](const TcpConnection::Ptr& session) { |
| 38 | std::shared_ptr<TcpConnection::Ptr> shareBackendSession = std::make_shared<TcpConnection::Ptr>(nullptr); |
| 39 | std::shared_ptr<std::vector<string>> cachePacket = std::make_shared<std::vector<std::string>>(); |
| 40 | |
| 41 | auto enterCallback = [tcpService, session, shareBackendSession, cachePacket, status](TcpSocket::Ptr socket) { |
| 42 | auto enterCallback = [=](const TcpConnection::Ptr& backendSession) { |
| 43 | if (!*status) /*if http client already close*/ |
| 44 | { |
| 45 | backendSession->postDisConnect(); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | *shareBackendSession = backendSession; |
| 50 | |
| 51 | for (auto& p : *cachePacket) |
| 52 | { |
| 53 | backendSession->send(p); |
| 54 | } |
| 55 | cachePacket->clear(); |
| 56 | |
| 57 | backendSession->setDisConnectCallback([=](const TcpConnection::Ptr& backendSession) { |
| 58 | (void) backendSession; |
| 59 | *shareBackendSession = nullptr; |
| 60 | if (*status) |
| 61 | { |
| 62 | session->postDisConnect(); |
| 63 | } |
| 64 | }); |
| 65 | |
| 66 | backendSession->setDataCallback([=](brynet::base::BasePacketReader& reader) { |
| 67 | /* receive data from backend server, then send to http client */ |
| 68 | session->send(reader.begin(), reader.size()); |
| 69 | reader.consumeAll(); |
| 70 | }); |
| 71 | }; |
nothing calls this directly
no test coverage detected