MCPcopy Create free account
hub / github.com/OpenArkStudio/ARK / StartClient

Method StartClient

src/plugin/net/src/AFCWebSocketClient.cpp:34–126  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

32}
33
34bool 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 });

Callers

nothing calls this directly

Calls 13

getResultMethod · 0.80
SetIdMethod · 0.80
SetBusIdMethod · 0.80
AddNetEventMethod · 0.80
ParseBufferToMsgMethod · 0.80
SetNeedRemoveMethod · 0.80
c_strMethod · 0.45
sizeMethod · 0.45
GetUIDMethod · 0.45
SetTypeMethod · 0.45
SetIPMethod · 0.45
resetMethod · 0.45

Tested by

no test coverage detected