Exemplo n.º 1
0
	bool message_procedure::dispatch(connection::ptr spConn, message::ptr msg)
	{
		do 
		{
			message::msgHeader header;
			int sizeHeader = sizeof(header);

			if (false == msg->readable((char*)&header, sizeHeader)) break;
			if (false == msg->readable(header.size)) break;

			msg->read(sizeHeader);

			auto it = _instanceElements.find(header.protocol);
			if(_instanceElements.end() != it) {
				it->second(spConn, header.size, *msg.get());
			}
			else{
				auto it = _staticElements.find(header.protocol);
				if(_staticElements.end() != it){
					it->second(spConn, header.size, *msg.get());
				}
				else{
					_receiver.noHandler(spConn, *msg.get());
					msg->readAll();
					break;
				}
			}
		}while(true);

		return true;
	}
Exemplo n.º 2
0
bool message_queue::send(message::ptr msg, bool waitforanswer)
{
	msg->needsanswer = waitforanswer;
	msg->result = false;
	message* msg_addr = msg.get();
	mutex_locker oml(mymutex);
	bool e = myqueue.empty();
	myqueue.push_back(msg.release());
	msginqueue = true;
	if (e) {
		emptycondvar.signal();
	}
	if (waitforanswer) {
		while (true) {
			ackcondvar.wait(mymutex);
			// check if this message has been acknowledged
			for (std::list<message*>::iterator it = ackqueue.begin(); it != ackqueue.end(); ) {
				if (*it == msg_addr) {
					// found it, return result, delete and unqueue message
					bool result = (*it)->result;
					delete *it;
					ackqueue.erase(it);
					return result;
				}
			}
		}
	}
	return true;
}
Exemplo n.º 3
0
    void client_impl::on_handshake(message::ptr const& message)
    {
        if(message && message->get_flag() == message::flag_object)
        {
            const object_message* obj_ptr =static_cast<object_message*>(message.get());
            const map<string,message::ptr>* values = &(obj_ptr->get_map());
            auto it = values->find("sid");
            if (it!= values->end()) {
                m_sid = static_pointer_cast<string_message>(it->second)->get_string();
            }
            else
            {
                goto failed;
            }
            it = values->find("pingInterval");
            if (it!= values->end()&&it->second->get_flag() == message::flag_integer) {
                m_ping_interval = (unsigned)static_pointer_cast<int_message>(it->second)->get_int();
            }
            else
            {
                m_ping_interval = 25000;
            }
            it = values->find("pingTimeout");

            if (it!=values->end()&&it->second->get_flag() == message::flag_integer) {
                m_ping_timeout = (unsigned) static_pointer_cast<int_message>(it->second)->get_int();
            }
            else
            {
                m_ping_timeout = 60000;
            }

            m_ping_timer.reset(new boost::asio::deadline_timer(m_client.get_io_service()));
            boost::system::error_code ec;
            m_ping_timer->expires_from_now(milliseconds(m_ping_interval), ec);
            if(ec)LOG("ec:"<<ec.message()<<endl);
            m_ping_timer->async_wait(lib::bind(&client_impl::ping,this,lib::placeholders::_1));
            LOG("On handshake,sid:"<<m_sid<<",ping interval:"<<m_ping_interval<<",ping timeout"<<m_ping_timeout<<endl);
            return;
        }
failed:
        //just close it.
        m_client.get_io_service().dispatch(lib::bind(&client_impl::close_impl, this,close::status::policy_violation,"Handshake error"));
    }