void OnMessage(const linear::Socket& socket, const linear::Message& msg) { const linear::Addrinfo& info = socket.GetPeerInfo(); switch(msg.type) { case linear::REQUEST: { linear::Request request = msg.as<linear::Request>(); std::cout << "recv Request: msgid = " << request.msgid << ", method = \"" << request.method << "\"" << ", params = " << request.params.stringify() << " from " << info.addr << ":" << info.port << std::endl; linear::Response response(request.msgid, linear::type::nil(), std::string("This client does not handle request")); response.Send(socket); } break; case linear::RESPONSE: { linear::Response response = msg.as<linear::Response>(); std::cout << "recv Response(Handler): msgid = " << response.msgid << ", result = " << response.result.stringify() << ", error = " << response.error.stringify() << " from " << info.addr << ":" << info.port << std::endl; std::cout << "origin request: msgid = " << response.request.msgid << ", method = \"" << response.request.method << "\"" << ", params = " << response.request.params.stringify() << std::endl; } break; case linear::NOTIFY: { linear::Notify notify = msg.as<linear::Notify>(); std::cout << "recv Notify: " << "method = \"" << notify.method << "\"" << ", params = " << notify.params.stringify() << " from " << info.addr << ":" << info.port << std::endl; try { Derived data = notify.params.as<Derived>(); std::cout << "parameters detail" << std::endl; std::cout << "Base::" << "int: " << data.int_val << ", double: " << data.double_val << ", string: " << data.string_val << ", vector: " << data.vector_val[0] << ", map: {\"key\": " << data.map_val["key"] << "}" << std::endl; std::cout << "Derived::int: " << data.derived_val << std::endl; } catch(const std::bad_cast&) { std::cout << "invalid type cast" << std::endl; } } break; default: { std::cout << "BUG: plz inform to linear-developpers" << std::endl; } break; } }
void OnConnect(const linear::Socket& socket) { const linear::Addrinfo& info = socket.GetPeerInfo(); std::cout << "OnConnect: " << info.addr << ":" << info.port << std::endl; // WSSSocket specific linear::WSResponseContext context = socket.as<linear::WSSSocket>().GetWSResponseContext(); std::cout << "--- Response Headers ---" << std::endl; for (std::map<std::string, std::string>::const_iterator it = context.headers.begin(); it != context.headers.end(); it++) { std::cout << it->first << ": " << it->second << std::endl; } std::cout << "--- Headers End ---" << std::endl; // WSSSocket specific end }
void OnConnect(const linear::Socket& socket) { const linear::Addrinfo& info = socket.GetPeerInfo(); std::cout << "OnConnect: " << info.addr << ":" << info.port << std::endl; // WSSocket specific linear::WSRequestContext request_context = socket.as<linear::WSSocket>().GetWSRequestContext(); std::cout << "--- Request Path ---" << std::endl; std::cout << request_context.path << std::endl; std::cout << "--- Request Query ---" << std::endl; std::cout << request_context.query << std::endl; std::cout << "--- Authorization Info ---" << std::endl; std::cout << "type: " << request_context.authorization.type << std::endl; std::cout << "username: "******"realm: " << request_context.authorization.realm << std::endl; // Digest Auth Validation (username = "******", password = "******") linear::AuthorizationContext::Result r = request_context.authorization.Validate("password"); std::cout << "Validate: " << ((r != linear::AuthorizationContext::INVALID) ? "VALID" : "INVALID") << std::endl; std::cout << "--- Request Headers ---" << std::endl; for (std::map<std::string, std::string>::const_iterator it = request_context.headers.begin(); it != request_context.headers.end(); it++) { std::cout << it->first << ": " << it->second << std::endl; } std::cout << "--- Request Headers End ---" << std::endl; linear::WSResponseContext response_context; // set the result of Digest Auth Validation if (r != linear::AuthorizationContext::INVALID) { response_context.code = LNR_WS_OK; } else { response_context.code = LNR_WS_UNAUTHORIZED; } // If you want to respond with custom header, do like below response_context.headers["X-Custom-Header"] = "Add Any Header for Response"; socket.as<linear::WSSocket>().SetWSResponseContext(response_context); // WSSocket specific end }
void OnDisconnect(const linear::Socket& socket, const linear::Error&) { // WSSSocket specific linear::WSResponseContext response_context = socket.as<linear::WSSSocket>().GetWSResponseContext(); std::cout << "Response Code: " << response_context.code << std::endl; std::cout << "--- Response Headers ---" << std::endl; for (std::map<std::string, std::string>::const_iterator it = response_context.headers.begin(); it != response_context.headers.end(); it++) { std::cout << it->first << ": " << it->second << std::endl; } std::cout << "--- Headers End ---" << std::endl; static int num_of_try = 0; if (num_of_try < num_of_retry_) { // retry to connect after 1 sec timer_.Start(ApplicationHandler::Reconnect, 1000, new linear::Socket(socket)); num_of_try++; } else { // give up to connect const linear::Addrinfo& info = socket.GetPeerInfo(); std::cout << "OnDisconnect: " << info.addr << ":" << info.port << std::endl; num_of_try = 0; } }
void OnMessage(const linear::Socket& socket, const linear::Message& msg) { const linear::Addrinfo& info = socket.GetPeerInfo(); switch(msg.type) { case linear::REQUEST: { linear::Request request = msg.as<linear::Request>(); std::cout << "recv Request: msgid = " << request.msgid << ", method = \"" << request.method << "\"" << ", params = " << request.params.stringify() << " from " << info.addr << ":" << info.port << std::endl; if (request.method == "echo") { std::cout << "do echo back: " << request.params.stringify() << std::endl; linear::Response response(request.msgid, request.params); response.Send(socket); } else { linear::Response response(request.msgid, linear::type::nil(), std::string("method not found")); response.Send(socket); } } break; case linear::RESPONSE: { linear::Response response = msg.as<linear::Response>(); std::cout << "recv Response: msgid = " << response.msgid << ", result = " << response.result.stringify() << ", error = " << response.error.stringify() << " from " << info.addr << ":" << info.port << std::endl; std::cout << "origin request: msgid = " << response.request.msgid << ", method = \"" << response.request.method << "\"" << ", params = " << response.request.params.stringify() << std::endl; } break; case linear::NOTIFY: { linear::Notify notify = msg.as<linear::Notify>(); std::cout << "recv Notify: " << "method = \"" << notify.method << "\"" << ", params = " << notify.params.stringify() << " from " << info.addr << ":" << info.port << std::endl; try { Derived data = notify.params.as<Derived>(); std::cout << "parameters detail" << std::endl; std::cout << "Base::" << "int: " << data.base_val.int_val << ", double: " << data.base_val.double_val << ", string: " << data.base_val.string_val << ", vector: " << data.base_val.vector_val[0] << ", map: {\"key\": " << data.base_val.map_val["key"] << "}" << std::endl; std::cout << "Derived::int: " << data.derived_val << std::endl; } catch(const std::bad_cast&) { std::cout << "invalid type cast" << std::endl; } linear::Notify notify_from_server("from server", Derived()); notify_from_server.Send(LINEAR_BROADCAST_GROUP); // send notify to all connected clients } break; default: { std::cout << "BUG: plz inform to linear-developpers" << std::endl; } break; } }
void OnDisconnect(const linear::Socket& socket, const linear::Error& err) { const linear::Addrinfo& info = socket.GetPeerInfo(); std::cout << "OnDisconnect: " << info.addr << ":" << info.port << std::endl; }