MCPcopy Create free account
hub / github.com/axmolengine/axmol / handleNetworkEvent

Method handleNetworkEvent

core/network/WebSocket.cpp:520–670  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

518}
519
520void WebSocket::handleNetworkEvent(yasio::io_event* event)
521{
522 int channelIndex = event->cindex();
523 auto channel = _service->channel_at(event->cindex());
524 switch (event->kind())
525 {
526 case YEK_ON_PACKET:
527 if (_state == State::CONNECTING)
528 {
529 auto&& pkt = event->packet_view();
530 this->do_handshake(pkt.data(), pkt.size());
531 if (_handshakeFinished)
532 {
533 ErrorCode error = ErrorCode::OK;
534 if (_responseCode == 101)
535 {
536 auto it = _responseHeaders.find("sec-websocket-accept");
537 if (it != _responseHeaders.end())
538 {
539 auto& secAccept = it->second;
540 if (_verifySecKey != secAccept)
541 error = ErrorCode::VERIFY_SEC_ACCEPT_FAILURE;
542 }
543 else
544 error = ErrorCode::NO_SEC_ACCEPT;
545 }
546 else
547 error = ErrorCode::UPGRADE_FAILURE;
548
549 if (error == ErrorCode::OK)
550 {
551 _state = State::OPEN;
552 auto& timerForRead = channel->get_user_timer();
553 timerForRead.cancel();
554 _transport = event->transport();
555 _eventQueue.emplace_back(new WsOpenEvent());
556
557 // WebSocketProtocol::sendPing(*this);
558
559 // starts websocket heartbeat timer
560 //_heartbeatTimer->expires_from_now(std::chrono::seconds(30));
561 //_heartbeatTimer->async_wait([this](io_service& service) {
562 // WebSocketProtocol::sendFrame(*this, "WSWS", 4, ws::detail::opcode::ping);
563 // return false;
564 //});
565 }
566 else
567 {
568 if (!_responseData.empty())
569 AXLOGE("WebSocket: handshake fail, detail: {}", _responseData);
570
571 //_state = State::CLOSING;
572 _service->close(channelIndex);
573 _eventQueue.emplace_back(new ErrorEvent(error));
574 }
575 }
576 }
577 else if (_state == State::OPEN)

Callers

nothing calls this directly

Calls 15

websocket_parser_executeFunction · 0.85
channel_atMethod · 0.80
packet_viewMethod · 0.80
transportMethod · 0.80
cindexMethod · 0.45
kindMethod · 0.45
dataMethod · 0.45
sizeMethod · 0.45
findMethod · 0.45
endMethod · 0.45
cancelMethod · 0.45
emplace_backMethod · 0.45

Tested by

no test coverage detected