| 34 | } |
| 35 | |
| 36 | bool AFCWebSocketServer::StartServer(AFHeadLength head_len, const int busid, const std::string& ip, const int port, |
| 37 | const int thread_num, const unsigned int max_client, bool ip_v6 /* = false*/) |
| 38 | { |
| 39 | using namespace brynet::net; |
| 40 | using namespace brynet::net::http; |
| 41 | |
| 42 | this->bus_id_ = busid; |
| 43 | |
| 44 | tcp_service_ = TcpService::Create(); |
| 45 | tcp_service_->startWorkerThread(thread_num); |
| 46 | |
| 47 | auto shared_this = shared_from_this(); |
| 48 | auto OnEnterCallback = [shared_this, &head_len](const TcpConnectionPtr& session) { |
| 49 | const std::string& session_ip = session->getIP(); |
| 50 | HttpService::setup(session, [shared_this, &head_len, &session_ip](const HttpSession::Ptr& httpSession) { |
| 51 | //int64_t cur_session_id = shared_this->trusted_session_id_++; |
| 52 | int64_t cur_session_id = shared_this->uid_generator_->GetUID(shared_this->bus_id_); |
| 53 | AFNetEvent* net_connect_event = AFNetEvent::AllocEvent(); |
| 54 | net_connect_event->SetId(cur_session_id); |
| 55 | net_connect_event->SetType(AFNetEventType::CONNECTED); |
| 56 | net_connect_event->SetBusId(shared_this->bus_id_); |
| 57 | net_connect_event->SetIP(session_ip); |
| 58 | |
| 59 | // Scope lock |
| 60 | { |
| 61 | AFScopeWLock guard(shared_this->rw_lock_); |
| 62 | AFHttpSessionPtr session_ptr = ARK_NEW AFHttpSession(head_len, cur_session_id, httpSession); |
| 63 | if (shared_this->AddNetSession(session_ptr)) |
| 64 | { |
| 65 | session_ptr->AddNetEvent(net_connect_event); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | httpSession->setWSCallback([shared_this](const HttpSession::Ptr& httpSession, |
| 70 | WebSocketFormat::WebSocketFrameType opcode, const std::string& payload) { |
| 71 | switch (opcode) |
| 72 | { |
| 73 | case WebSocketFormat::WebSocketFrameType::ERROR_FRAME: |
| 74 | case WebSocketFormat::WebSocketFrameType::CONTINUATION_FRAME: |
| 75 | case WebSocketFormat::WebSocketFrameType::TEXT_FRAME: |
| 76 | case WebSocketFormat::WebSocketFrameType::BINARY_FRAME: |
| 77 | case WebSocketFormat::WebSocketFrameType::CLOSE_FRAME: |
| 78 | case WebSocketFormat::WebSocketFrameType::PING_FRAME: |
| 79 | { |
| 80 | auto frame = std::make_shared<std::string>(); |
| 81 | WebSocketFormat::wsFrameBuild(payload.c_str(), payload.size(), *frame, |
| 82 | WebSocketFormat::WebSocketFrameType::PONG_FRAME, true, false); |
| 83 | return httpSession->send(frame); |
| 84 | } |
| 85 | case WebSocketFormat::WebSocketFrameType::PONG_FRAME: |
| 86 | break; |
| 87 | default: |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | const auto ud = cast<int64_t>(httpSession->getUD()); |
| 92 | int64_t session_id = *ud; |
| 93 |
nothing calls this directly
no test coverage detected