reSIProcate/repro  9694
Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes | Friends
repro::XmlRpcConnection Class Reference

#include <XmlRpcConnection.hxx>

Collaboration diagram for repro::XmlRpcConnection:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 XmlRpcConnection (XmlRpcServerBase &server, resip::Socket sock)
 ~XmlRpcConnection ()
unsigned int getConnectionId () const
void buildFdSet (resip::FdSet &fdset)
bool process (resip::FdSet &fdset)
virtual bool sendResponse (unsigned int requestId, const resip::Data &responseData, bool isFinal)
virtual void sendEvent (const resip::Data &eventData)

Private Types

typedef std::map< unsigned int,
resip::Data
RequestMap

Private Member Functions

bool processSomeReads ()
bool processSomeWrites ()
bool tryParse ()

Private Attributes

XmlRpcServerBasemXmlRcpServer
const unsigned int mConnectionId
unsigned int mNextRequestId
RequestMap mRequests
resip::Socket mSock
resip::Data mRxBuffer
resip::Data mTxBuffer

Static Private Attributes

static unsigned int NextConnectionId = 1

Friends

class XmlRpcServerBase

Detailed Description

Definition at line 15 of file XmlRpcConnection.hxx.


Member Typedef Documentation

typedef std::map<unsigned int, resip::Data> repro::XmlRpcConnection::RequestMap [private]

Definition at line 40 of file XmlRpcConnection.hxx.


Constructor & Destructor Documentation

XmlRpcConnection::XmlRpcConnection ( XmlRpcServerBase server,
resip::Socket  sock 
)

Definition at line 24 of file XmlRpcConnection.cxx.

                                                                            :
   mXmlRcpServer(server),
   mConnectionId(NextConnectionId++),
   mNextRequestId(1),
   mSock(sock)
{
        assert(mSock > 0);
}
XmlRpcConnection::~XmlRpcConnection ( )

Definition at line 34 of file XmlRpcConnection.cxx.

{
   assert(mSock > 0);
#ifdef WIN32
   closesocket(mSock); 
#else
   close(mSock);
#endif
   mSock=0;
}

Member Function Documentation

void XmlRpcConnection::buildFdSet ( resip::FdSet fdset)

Definition at line 47 of file XmlRpcConnection.cxx.

{
   if (!mTxBuffer.empty())
   {
      fdset.setWrite(mSock);
   }
   fdset.setRead(mSock);
}
unsigned int repro::XmlRpcConnection::getConnectionId ( ) const [inline]

Definition at line 23 of file XmlRpcConnection.hxx.

{ return mConnectionId; }
bool XmlRpcConnection::process ( resip::FdSet fdset)

Definition at line 58 of file XmlRpcConnection.cxx.

{
   if (fdset.hasException(mSock))
   {
      int errNum = 0;
      int errNumSize = sizeof(errNum);
      getsockopt(mSock,SOL_SOCKET,SO_ERROR,(char *)&errNum,(socklen_t *)&errNumSize);
      InfoLog (<< "XmlRpcConnection::process: Exception reading from socket " 
               << (int)mSock << " code: " << errNum << "; closing connection");
      return false;
   }
   
   if (fdset.readyToRead(mSock))
   {
      bool ok = processSomeReads();
      if (!ok)
      {
         return false;
      }
   }
   if ((!mTxBuffer.empty()) && fdset.readyToWrite(mSock))
   {
      bool ok = processSomeWrites();
      if (!ok)
      {
         return false;
      }
   }

   return true;
}
bool XmlRpcConnection::processSomeReads ( ) [private]

Definition at line 91 of file XmlRpcConnection.cxx.

{
   const int bufSize = 8000;
   char buf[bufSize];
   
 
#if defined(WIN32)
   int bytesRead = ::recv(mSock, buf, bufSize, 0);
#else
   int bytesRead = ::read(mSock, buf, bufSize);
#endif

   if (bytesRead == INVALID_SOCKET)
   {
      int e = getErrno();
      XmlRpcServerBase::logSocketError(e);
      InfoLog (<< "XmlRpcConnection::processSomeReads: Failed read on " << (int)mSock);
      return false;
   }
   else if(bytesRead == 0)
   {
      DebugLog (<< "XmlRpcConnection::processSomeReads: Connection closed by remote");
      return false;
   }

   //DebugLog (<< "XmlRpcConnection::processSomeReads: read=" << bytesRead);            

   mRxBuffer += Data( buf, bytesRead );
   
   while(tryParse());
   
   return true;
}
bool XmlRpcConnection::processSomeWrites ( ) [private]

Definition at line 171 of file XmlRpcConnection.cxx.

{
   if (mTxBuffer.empty())
   {
      return true;
   }
   
   //DebugLog (<< "XmlRpcConnection::processSomeWrites: Writing " << mTxBuffer );

#if defined(WIN32)
   int bytesWritten = ::send(mSock, mTxBuffer.data(), (int)mTxBuffer.size(), 0);
#else
   int bytesWritten = ::write(mSock, mTxBuffer.data(), mTxBuffer.size() );
#endif

   if (bytesWritten == INVALID_SOCKET)
   {
      int e = getErrno();
      XmlRpcServerBase::logSocketError(e);
      InfoLog (<< "XmlRpcConnection::processSomeWrites - failed write on " << mSock << " " << strerror(e));

      return false;
   }
   
   if (bytesWritten == (int)mTxBuffer.size())
   {
      DebugLog (<< "XmlRpcConnection::processSomeWrites - Wrote it all" );
      mTxBuffer = Data::Empty;

      //return false; // return false causes connection to close and clean up
      return true;  // keep connection up
   }
   else
   {
      Data rest = mTxBuffer.substr(bytesWritten);
      mTxBuffer = rest;
      DebugLog( << "XmlRpcConnection::processSomeWrites - Wrote " << bytesWritten << " bytes - still need to do " << mTxBuffer );
   }
   
   return true;
}
void XmlRpcConnection::sendEvent ( const resip::Data eventData) [virtual]

Definition at line 261 of file XmlRpcConnection.cxx.

{
   mTxBuffer += eventData;
}
bool XmlRpcConnection::sendResponse ( unsigned int  requestId,
const resip::Data responseData,
bool  isFinal 
) [virtual]

Definition at line 214 of file XmlRpcConnection.cxx.

{
   RequestMap::iterator it = mRequests.find(requestId);
   if(it != mRequests.end())
   {
      Data& request = it->second;
      Data response(request.size() + responseData.size() + 30, Data::Preallocate);
      ParseBuffer pb(request);

      // A response is formed by starting with the request and inserting the 
      // ResponseData between <Response> tags at the same level as the <Request> tags
      const char* start = pb.position();      
      pb.skipToChars("</Request>");
      if (!pb.eof())
      {
         pb.skipN(10);  // Skip past </Request>
         pb.skipWhitespace();
   
         // Response starts with request message up to end of Request tag
         response = pb.data(start);
   
         // Add in response data
         response += Symbols::CRLF;
         response += "  <Response>" + responseData + "  </Response>";
         response += Symbols::CRLF;

         // Add remainder of request message
         start = pb.position();
         pb.skipToEnd();
         response += pb.data(start);
      }
      else
      {
         // No Request in message - just send bare response
         response = "<Response>" + responseData + "</Response>";
      }
      mTxBuffer += response;
      if(isFinal)
      {
          mRequests.erase(it);
      }
      return true;
   }
   return false;
}
bool XmlRpcConnection::tryParse ( ) [private]

Definition at line 127 of file XmlRpcConnection.cxx.

{
   ParseBuffer pb(mRxBuffer);
   Data initialTag;
   const char* start = pb.position();
   pb.skipWhitespace();
   pb.skipToChar('<');   
   if(!pb.eof())
   {
      pb.skipChar();
      const char* anchor = pb.position();
      pb.skipToChar('>');
      if(!pb.eof())
      {
         initialTag = pb.data(anchor);
         // Find end of initial tag
         pb.skipToChars("</" + initialTag + ">");
         if (!pb.eof())
         {
            pb.skipN((int)initialTag.size() + 3);  // Skip past </InitialTag>            
            mRequests[mNextRequestId] = pb.data(start);
            mXmlRcpServer.handleRequest(mConnectionId, mNextRequestId, mRequests[mNextRequestId]);
            mNextRequestId++;

            // Remove processed data from RxBuffer
            pb.skipWhitespace();
            if(!pb.eof())
            {
               anchor = pb.position();
               pb.skipToEnd();
               mRxBuffer = pb.data(anchor);
               return true;
            }
            else
            {
               mRxBuffer.clear();
            }
         }   
      }
   }
   return false;
}

Friends And Related Function Documentation

friend class XmlRpcServerBase [friend]

Definition at line 17 of file XmlRpcConnection.hxx.


Member Data Documentation

const unsigned int repro::XmlRpcConnection::mConnectionId [private]

Definition at line 36 of file XmlRpcConnection.hxx.

Definition at line 39 of file XmlRpcConnection.hxx.

Definition at line 41 of file XmlRpcConnection.hxx.

Definition at line 44 of file XmlRpcConnection.hxx.

Definition at line 43 of file XmlRpcConnection.hxx.

Definition at line 45 of file XmlRpcConnection.hxx.

Definition at line 35 of file XmlRpcConnection.hxx.

unsigned int XmlRpcConnection::NextConnectionId = 1 [static, private]

Definition at line 37 of file XmlRpcConnection.hxx.


The documentation for this class was generated from the following files: