示例#1
0
void
send_data( const SocketPtr &in_socket, const std::string &in_msg ) {
    if( in_socket->get_is_connected() )
    {
        MemInfo info( in_msg.c_str(), in_msg.length() + 1 );
        in_socket->send_data( info );
    }
}
示例#2
0
void
DataServer::on_data_received(
            const SocketPtr &in_socket, const MemInfo &in_msg ) {
    const DataServiceMessage *pMsg
            = reinterpret_cast<const DataServiceMessage *>(
                                    in_msg.get_data());
    DataServiceMessage reply( DATA_SERVICE_GENERAL_REPLY );
    bool status = false;
    switch( pMsg->m_header.m_type ) {
        case DataMessageType::DATA_SERVICE_REGISTER:
        {
            std::string buf = pMsg->m_data;
            std::istringstream ifs( buf );
            boost::archive::text_iarchive ia( ifs );
            ServiceRequestData data;
            ia & data;
            status = on_service_register_request(
                                        data.m_service_name,
                                        data.m_hostname, data.m_port );
            break;
        }
        case DataMessageType::DATA_SERVICE_UNREGISTER:
        {
            std::string buf = pMsg->m_data;
            std::istringstream ifs( buf );
            boost::archive::text_iarchive ia( ifs );
            ServiceRequestData data;
            ia & data;
            status = on_service_unregister_request(
                                        data.m_service_name,
                                        data.m_hostname, data.m_port );
            break;
        }
        case DataMessageType::DATA_SERVICE_INFO_REQUEST:
        {
            std::string buf = pMsg->m_data;
            std::istringstream ifs( buf );
            boost::archive::text_iarchive ia( ifs );
            ServiceRequestData data;
            ia & data;
            ServiceInfoPtr info;
            status = on_service_info_request( data.m_service_name, info );
            if( info ) {
                std::ostringstream sout;
                boost::archive::text_oarchive oa(sout);
                oa & info;
                std::strncpy( reply.m_data,
                                sout.str().c_str(), sout.str().size());
            }
            reply.m_header.m_type =  DATA_SERVICE_INFO_REPLY;
            break;
        }
        default:
        {
        }
    }
    reply.m_status = status;
    MemInfo replyMsg( reinterpret_cast<char *>( &reply ),
                        sizeof( DataServiceMessage ) );
    in_socket->send_data( replyMsg );
}