Main Page   Namespace List   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

socket.cpp

Go to the documentation of this file.
00001 
00014 #include "sysconfig.h"
00015 #include "socket.h"
00016 
00024 Socket::Socket(SOCKET sock, int size) : mSock(sock), mSize(size) {
00025   mpInBuffer = new char[mSize];
00026   mpEod = mpRead = mpInBuffer;
00027   mpOutBuffer = new char[mSize];
00028   mpWrite = mpStartWrite = mpOutBuffer;
00029 }
00030 
00036 Socket::Socket(const Socket& r_sock)
00037   : mSock(r_sock.mSock), mSize(r_sock.mSize) {
00038   // Set up new input buffer and pointers exactly
00039   mpInBuffer = new char[mSize];
00040   memcpy(mpInBuffer, r_sock.mpInBuffer, mSize);
00041   mpEod = mpInBuffer + (r_sock.mpEod - r_sock.mpInBuffer);
00042   mpRead = mpInBuffer + (r_sock.mpRead - r_sock.mpInBuffer);
00043   // Set up new output buffer and pointers exactly
00044   mpOutBuffer = new char[mSize];
00045   memcpy(mpOutBuffer, r_sock.mpOutBuffer, mSize);
00046   mpWrite = mpOutBuffer + (r_sock.mpWrite - r_sock.mpOutBuffer);
00047   mpStartWrite = mpOutBuffer + (r_sock.mpStartWrite - r_sock.mpOutBuffer);
00048 }
00049 
00053 Socket::~Socket() {
00054   delete[] mpInBuffer;
00055   delete[] mpOutBuffer;
00056 }
00057 
00069 char Socket::Read() {
00070   if (mpRead == mpEod) {              // Nothing in bufffer. Fetch more data.
00071     mpEod = mpRead = mpInBuffer;      // reset buffer for a full load.
00072     int bytes = recv (mSock, mpInBuffer, mSize, 0);
00073     if (bytes == 0) {
00074       throw ShutdownException();      // Connection was shutdown.
00075     } else if (bytes == SOCKET_ERROR) {
00076       int err = WSAGetLastError();
00077       if (err == WSAEWOULDBLOCK) {
00078         throw BlockingException(err); // No more input available.
00079       } else {
00080         throw IOErrorException(err);  // We have a serious error.
00081       }
00082     }
00083     mpEod += bytes;
00084   }
00085   char c = *mpRead;
00086   mpRead++;
00087   return c;
00088 }
00089 
00106 void Socket::Write(const char c) {
00107   if (mpWrite == mpOutBuffer + mSize) {
00108     // There is no room in buffer - attempt to flush it.
00109     Flush();
00110   }
00111   *mpWrite = c;
00112   mpWrite++;
00113   return;
00114 }
00115 
00133 void Socket::Write(const string& r_str) {
00134   string::const_iterator si = r_str.begin();
00135   while(si != r_str.end()) {
00136     Write(*si);
00137     si++;
00138   }
00139 }
00140 
00160 void Socket::Write(const char* p_char) {
00161   while(p_char) {
00162     Write(*p_char);
00163     p_char++;
00164   }
00165 }
00166 
00185 void Socket::Write(const char* p_char, int len) {
00186   while(p_char && len) {
00187     Write(*p_char);
00188     p_char++;
00189     len--;
00190   }
00191 }
00192 
00203 void Socket::Flush() {
00204   int wlen = mpWrite - mpStartWrite;
00205   if (wlen == 0) {  // Nothing to write
00206     if (mpStartWrite == mpOutBuffer + mSize) { // Oh we're at end of buffer
00207       mpWrite = mpStartWrite = mpOutBuffer; // reinitialize buffer
00208     }
00209     return;
00210   }
00211   int bytes = send (mSock, mpStartWrite, wlen, 0);
00212   if (bytes == SOCKET_ERROR) {
00213     int err = WSAGetLastError ();
00214     if (err == WSAEWOULDBLOCK) {
00215       throw BlockingException(); // will block
00216     } else {
00217       throw IOErrorException(err); // severe error
00218     }
00219   } else if (bytes < wlen) {  // We haven't written it all
00220     mpStartWrite += bytes;
00221     return;
00222   }
00223   // Okay everything was written - reinitalize buffer
00224   mpWrite = mpStartWrite = mpOutBuffer; // reinitialize buffer
00225   return;
00226 }
00227 
00233 SOCKET Socket::GetSocket() {
00234   return mSock;
00235 }
00236 
00240 void Socket::Close() {
00241   shutdown(mSock, SD_SEND); // Inform the client so they don't hang
00242   try {
00243     // Attempt to read all the data left in the socket's buffer.  This
00244     // should throw an exception which we will catch gracefully.
00245     while (true) Read();
00246   }
00247   catch (exception &ex) {
00248     closesocket(mSock);
00249   }
00250 }
00251 

Generated on Mon Mar 29 23:12:53 2004 for Paradigm by doxygen1.2.17