| 32 | } |
| 33 | |
| 34 | bool AFCWebSocketClient::StartClient( |
| 35 | AFHeadLength len, const int target_busid, const std::string& ip, const int port, bool ip_v6) |
| 36 | { |
| 37 | using namespace brynet::net; |
| 38 | using namespace brynet::net::http; |
| 39 | |
| 40 | dst_bus_id_ = target_busid; |
| 41 | |
| 42 | auto shared_this = shared_from_this(); |
| 43 | auto OnEnterCallback = [shared_this, len, ip](const HttpSession::Ptr& httpSession) { |
| 44 | brynet::net::http::HttpRequest request; |
| 45 | request.setMethod(HttpRequest::HTTP_METHOD::HTTP_METHOD_GET); |
| 46 | request.setUrl("/ws"); |
| 47 | request.addHeadValue("Host", ip); |
| 48 | request.addHeadValue("Upgrade", "websocket"); |
| 49 | request.addHeadValue("Connection", "Upgrade"); |
| 50 | request.addHeadValue("Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ=="); |
| 51 | request.addHeadValue("Sec-WebSocket-Version", "13"); |
| 52 | |
| 53 | std::string requestStr = request.getResult(); |
| 54 | httpSession->send(requestStr.c_str(), requestStr.size()); |
| 55 | |
| 56 | httpSession->setWSConnected([shared_this, &len, &ip](const HttpSession::Ptr& httpSession, const HTTPParser&) { |
| 57 | // now session_id |
| 58 | //int64_t cur_session_id = this->trust_session_id_++; |
| 59 | auto cur_session_id = shared_this->uid_generator_->GetUID(shared_this->dst_bus_id_); |
| 60 | // create net event |
| 61 | AFNetEvent* net_connect_event = AFNetEvent::AllocEvent(); |
| 62 | net_connect_event->SetId(cur_session_id); |
| 63 | net_connect_event->SetType(AFNetEventType::CONNECTED); |
| 64 | net_connect_event->SetBusId(shared_this->dst_bus_id_); |
| 65 | net_connect_event->SetIP(ip); |
| 66 | |
| 67 | // set session ud |
| 68 | httpSession->setUD(cur_session_id); |
| 69 | |
| 70 | // create session and net event |
| 71 | { |
| 72 | AFScopeWLock guard(shared_this->rw_lock_); |
| 73 | |
| 74 | AFHttpSessionPtr session_ptr = ARK_NEW AFHttpSession(len, cur_session_id, httpSession); |
| 75 | shared_this->client_session_ptr_.reset(session_ptr); |
| 76 | session_ptr->AddNetEvent(net_connect_event); |
| 77 | } |
| 78 | }); |
| 79 | |
| 80 | httpSession->setWSCallback([shared_this](const HttpSession::Ptr& httpSession, |
| 81 | WebSocketFormat::WebSocketFrameType opcode, const std::string& payload) { |
| 82 | const auto ud = cast<int64_t>(httpSession->getUD()); |
| 83 | int64_t session_id = *ud; |
| 84 | |
| 85 | // Scope lock |
| 86 | { |
| 87 | AFScopeRLock xGuard(shared_this->rw_lock_); |
| 88 | shared_this->client_session_ptr_->AddBuffer(payload.c_str(), payload.size()); |
| 89 | shared_this->client_session_ptr_->ParseBufferToMsg(); |
| 90 | } |
| 91 | }); |
nothing calls this directly
no test coverage detected