0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 09:06:21 +01:00
mongodb/util/message_server_asio.cpp

160 lines
5.0 KiB
C++
Raw Normal View History

2009-03-02 15:13:20 +01:00
// message_server_asio.cpp
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <iostream>
#include <vector>
#include "message.h"
#include "message_server.h"
using namespace boost;
using namespace boost::asio;
using namespace boost::asio::ip;
using namespace std;
namespace mongo {
2009-03-02 15:57:43 +01:00
class MessageServerSession : public enable_shared_from_this<MessageServerSession> , public AbstractMessagingPort {
2009-03-02 15:13:20 +01:00
public:
2009-03-02 15:57:43 +01:00
MessageServerSession( MessageHandler * handler , io_service& ioservice ) : _handler( handler ) , _socket( ioservice ){
2009-03-02 15:13:20 +01:00
}
tcp::socket& socket(){
return _socket;
}
void start(){
cout << "MessageServerSession start from:" << _socket.remote_endpoint() << endl;
async_read( _socket ,
buffer( &_inHeader , sizeof( _inHeader ) ) ,
bind( &MessageServerSession::handleReadHeader , shared_from_this() , placeholders::error ) );
}
void handleReadHeader( const boost::system::error_code& error ){
cout << "got header\n"
<< " len: " << _inHeader.len << "\n"
<< " id : " << _inHeader.id << "\n"
<< " op : " << _inHeader._operation << "\n";
2009-03-02 15:57:43 +01:00
if ( ! _inHeader.valid() ){
cerr << " got invalid header from: " << _socket.remote_endpoint() << " closing connected" << endl;
return;
}
2009-03-02 15:13:20 +01:00
char * raw = (char*)malloc( _inHeader.len );
2009-03-02 15:57:43 +01:00
2009-03-02 15:13:20 +01:00
MsgData * data = (MsgData*)raw;
memcpy( data , &_inHeader , sizeof( _inHeader ) );
assert( data->len == _inHeader.len );
2009-03-02 15:57:43 +01:00
uassert( "_cur not empty!" , ! _cur.data );
2009-03-02 15:13:20 +01:00
_cur.setData( data , true );
async_read( _socket ,
buffer( raw + sizeof( _inHeader ) , _inHeader.len - sizeof( _inHeader ) ) ,
bind( &MessageServerSession::handleReadBody , shared_from_this() , placeholders::error ) );
}
void handleReadBody( const boost::system::error_code& error ){
2009-03-02 15:57:43 +01:00
_handler->process( _cur , this );
}
void handleWriteDone( const boost::system::error_code& error ){
_cur.reset();
2009-03-02 15:13:20 +01:00
}
2009-03-02 15:57:43 +01:00
virtual void reply( Message& received, Message& response ){
reply( received , response , received.data->id );
}
virtual void reply( Message& query , Message& toSend, MSGID responseTo ){
toSend.data->id = nextMessageId();
toSend.data->responseTo = responseTo;
uassert( "pipelining requests doesn't work yet" , query.data->id == _cur.data->id );
async_write( _socket ,
buffer( (char*)toSend.data , toSend.data->len ) ,
bind( &MessageServerSession::handleWriteDone , shared_from_this() , placeholders::error ) );
}
private:
MessageHandler * _handler;
2009-03-02 15:13:20 +01:00
tcp::socket _socket;
MsgData _inHeader;
Message _cur;
};
2009-03-02 15:57:43 +01:00
2009-03-02 15:13:20 +01:00
class AsyncMessageServer : public MessageServer {
public:
2009-03-02 15:57:43 +01:00
AsyncMessageServer( int port , MessageHandler * handler ) :
MessageServer( port , handler ) ,
_endpoint( tcp::v4() , port ) ,
_acceptor( _ioservice , _endpoint ){
2009-03-02 15:13:20 +01:00
_accept();
}
virtual ~AsyncMessageServer(){
}
void run(){
cout << "AsyncMessageServer starting to listen on: " << _port << endl;
_ioservice.run();
cout << "AsyncMessageServer done listening on: " << _port << endl;
}
void handleAccept( shared_ptr<MessageServerSession> session ,
const boost::system::error_code& error ){
if ( error ){
cerr << "handleAccept error!" << endl;
return;
}
session->start();
_accept();
}
void _accept(){
2009-03-02 15:57:43 +01:00
shared_ptr<MessageServerSession> session( new MessageServerSession( _handler , _ioservice ) );
2009-03-02 15:13:20 +01:00
_acceptor.async_accept( session->socket() ,
bind( &AsyncMessageServer::handleAccept,
this,
session,
boost::asio::placeholders::error )
);
}
2009-03-02 15:57:43 +01:00
private:
2009-03-02 15:13:20 +01:00
io_service _ioservice;
tcp::endpoint _endpoint;
tcp::acceptor _acceptor;
};
2009-03-02 15:57:43 +01:00
2009-03-02 15:13:20 +01:00
// --temp hacks--
void dbexit( int rc , const char * why ){
cerr << "dbserver.cpp::dbexit" << endl;
::exit(rc);
}
const char * curNs = "";
string getDbContext(){
return "getDbContext bad";
}
}
using namespace mongo;
int main(){
2009-03-02 15:57:43 +01:00
mongo::AsyncMessageServer s(9999,0);
2009-03-02 15:13:20 +01:00
s.run();
return 0;
}