pairConnection.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "pairConnection.h"
00019
00020 using namespace std;
00021
00022 namespace eq
00023 {
00024 namespace net
00025 {
00026 PairConnection::PairConnection( ConnectionPtr readConnection,
00027 ConnectionPtr writeConnection )
00028 : _readConnection( readConnection )
00029 , _writeConnection( writeConnection )
00030 {
00031 _sibling = new PairConnection( this );
00032 EQASSERT( readConnection->isClosed( ));
00033 EQASSERT( writeConnection->isClosed( ));
00034 EQINFO << "New PairConnection @" << (void*)this << endl;
00035 }
00036
00037 PairConnection::PairConnection( PairConnection* sibling )
00038 : _readConnection( sibling->_writeConnection ),
00039 _writeConnection( sibling->_readConnection ),
00040 _sibling( sibling )
00041 {
00042 EQASSERT( _readConnection->isClosed( ));
00043 EQASSERT( _writeConnection->isClosed( ));
00044 EQINFO << "New PairConnection Sibling @" << (void*)this << endl;
00045 }
00046
00047 PairConnection::~PairConnection()
00048 {
00049 _readConnection = 0;
00050 _writeConnection = 0;
00051 _sibling = 0;
00052 }
00053
00054 ConnectionPtr PairConnection::getSibling()
00055 {
00056 return _sibling.get();
00057 }
00058
00059 bool PairConnection::connect()
00060 {
00061 if( _state != STATE_CLOSED )
00062 return false;
00063
00064 _state = STATE_CONNECTING;
00065 _sibling->_state = STATE_CONNECTING;
00066
00067 if( !_readConnection->connect( ))
00068 {
00069 _state = STATE_CLOSED;
00070 _sibling->_state = STATE_CLOSED;
00071 return false;
00072 }
00073
00074 if( !_writeConnection->connect( ))
00075 {
00076 _readConnection->close();
00077 _state = STATE_CLOSED;
00078 _sibling->_state = STATE_CLOSED;
00079 return false;
00080 }
00081
00082 _state = STATE_CONNECTED;
00083 _sibling->_state = STATE_CONNECTED;
00084 return true;
00085 }
00086
00087 void PairConnection::close()
00088 {
00089 if( _state != STATE_CONNECTED )
00090 return;
00091
00092 _state = STATE_CLOSED;
00093 _sibling->_state = STATE_CLOSED;
00094
00095 _readConnection->close();
00096 _writeConnection->close();
00097
00098
00099
00100 _sibling->_sibling = 0;
00101
00102 _readConnection = 0;
00103 _writeConnection = 0;
00104 _sibling = 0;
00105 }
00106
00107 }
00108 }