void bind_events(socket::ptr &socket) { current_socket->on("new message", sio::socket::event_listener_aux([&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp) { _lock.lock(); string user = data->get_map()["username"]->get_string(); string message = data->get_map()["message"]->get_string(); EM(user<<":"<<message); _lock.unlock(); })); current_socket->on("user joined",sio::socket::event_listener_aux([&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp) { _lock.lock(); string user = data->get_map()["username"]->get_string(); participants = data->get_map()["numUsers"]->get_int(); bool plural = participants !=1; // abc " HIGHLIGHT(user<<" joined"<<"\nthere"<<(plural?" are ":"'s ")<< participants<<(plural?" participants":" participant")); _lock.unlock(); })); current_socket->on("user left", sio::socket::event_listener_aux([&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp) { _lock.lock(); string user = data->get_map()["username"]->get_string(); participants = data->get_map()["numUsers"]->get_int(); bool plural = participants !=1; HIGHLIGHT(user<<" left"<<"\nthere"<<(plural?" are ":"'s ")<< participants<<(plural?" participants":" participant")); _lock.unlock(); })); }
void do_ping(Socket::ptr sock, string ip, int port){ debug("Pinging %s:%d\n", ip.c_str(), port); sock->connect(ip.c_str(), port); sock->send(PING + NEWLINE + NEWLINE); string response = sock->recv(); if(response.substr(0, PONG.length()) != PONG){ throw(gollum2411::socket_error("No PONG received")); } }
void startListen(Socket::ptr listen_socket, boost::barrier* barrier){ listen_socket->listen(); if(barrier) barrier->wait(); while(conn_num--) { //accept will switch the contex to epoll fiber Socket::ptr socket = listen_socket->accept(); Scheduler::getThis()->schedule(boost::bind(&TCPServer::handleConnect, this, socket)); } listen_socket->shutdown(); }
void handleConnect(Socket::ptr socket) { char buf[10]; while(1) { //receive will switch the contex to epoll fiber size_t len = socket->receive(buf, sizeof(buf)); if(len == 0) { printf("receive 0, closed by remote\n"); break; } socket->send(buf, len); } socket->shutdown(); }
static void wol(Socket::ptr socket, const std::string &macAddress) { MORDOR_ASSERT(macAddress.size() == 6u); std::string message; message.append(6u, (char)0xff); for(size_t i = 0; i < 16; ++i) message.append(macAddress); socket->send(message.c_str(), message.size()); }
proxy::proxy(threadpool& pool, socket::ptr socket, uint32_t magic) : stopped_(true), magic_(magic), authority_(socket->get_authority()), socket_(socket), message_subscriber_(pool), ////send_subscriber_(std::make_shared<send_subscriber>(pool, NAME "_send")), stop_subscriber_(std::make_shared<stop_subscriber>(pool, NAME "_stop")) { }
// private: void connector::handle_timer(const code& ec, socket::ptr socket, connect_handler handler) { // Cancel any current operations on the socket. socket->stop(); // This is the end of the timer sequence. if (ec) handler(ec, nullptr); else handler(error::channel_timeout, nullptr); }
// payload_buffer_ sizing assumes monotonically increasing size by version. proxy::proxy(threadpool& pool, socket::ptr socket, uint32_t protocol_magic, uint32_t protocol_maximum) : protocol_magic_(protocol_magic), authority_(socket->get_authority()), heading_buffer_(heading::maximum_size()), payload_buffer_(heading::maximum_payload_size(protocol_maximum)), socket_(socket), stopped_(true), version_(protocol_maximum), message_subscriber_(pool), stop_subscriber_(std::make_shared<stop_subscriber>(pool, NAME)) { }