• Main Page
  • Classes
  • Files
  • File List
  • File Members

Beesnest/Windows/DcSocket.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright 2007 Erez Bibi (erezbibi@users.sourceforge.net)
00003 This file is part of Beesnest.
00004 
00005 Beesnest is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 (at your option) any later version.
00009 
00010 Beesnest is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with Beesnest; if not, write to the Free Software
00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 
00020 /*
00021  * DcSocket.cpp: Implementation of CdcSocket class.
00022  * for documentation see the header file.
00023  */
00024 
00025 #include "DcSocket.h"
00026 
00027 WSADATA CdcSocket::oData;
00028 
00029 int CdcSocket::nReqMaxLength = 0;
00030 int CdcSocket::nWaitReqTime = 0;
00031 
00032 /* Constructor. */
00033 CdcSocket::CdcSocket () throw (CdcException)
00034 {
00035     /* Reset all the data fields. */
00036     oSocket = INVALID_SOCKET;
00037     oClient.sin_port = 0;
00038     oClient.sin_addr.S_un.S_addr = 0;
00039     StartNewRequest ();
00040     aBuffer = new char [nReqMaxLength];
00041     if (aBuffer == NULL)
00042         throw CdcException ("Cannot allocate memory");
00043 }
00044 
00045 
00046 /* Destructor. */
00047 CdcSocket::~CdcSocket ()
00048 {
00049     /* Close socket (there is no point to handle errors). */
00050     closesocket (oSocket);
00051     /* Delete the buffer. */
00052     if (aBuffer != NULL)
00053         delete[] aBuffer;
00054 }
00055 
00056 
00057 
00058 CdcSocket* CdcSocket::GetNewSocket (SOCKET soc, SOCKADDR_IN from) throw (CdcException)
00059 {
00060     CdcSocket* psock = new CdcSocket;
00061     psock->Init (soc, from);
00062     return psock;
00063 }
00064 
00065 
00066 void CdcSocket::Init (SOCKET soc, SOCKADDR_IN from) throw (CdcException)
00067 {
00068     /* Init socket. */
00069     Init (soc);
00070     /* Wrap the Client object. */
00071     oClient = from;
00072 }
00073 
00074 void CdcSocket::Init (SOCKET soc) throw (CdcException)
00075 {
00076     /* Some error in socket? */
00077     if (soc == INVALID_SOCKET)
00078         throw CdcExceptionSocket (WSAGetLastError());
00079 
00080     /* Just wrap this socket. */
00081     oSocket = soc;
00082 
00083     /* Set receive timeout. */
00084     int ret = setsockopt (oSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&nWaitReqTime,
00085                         sizeof (nWaitReqTime));
00086 
00087     /* Some error? */
00088     if (ret == SOCKET_ERROR)
00089         throw CdcExceptionSocket (WSAGetLastError());
00090 }
00091 
00092 
00093 int CdcSocket::Read (char* buff, int lng) throw (CdcException)
00094 {
00095     /* Read the message from the client. */
00096     int ret = recv (oSocket, buff, lng, 0);
00097     nReqLength += ret;
00098     /* This is "stack overflow" attack protection (?),
00099         even though buff is not on the stack. */
00100     if (nReqLength >= nReqMaxLength)
00101         throw CdcExceptionSocket (WSAEMSGSIZE);
00102     /* Some error? */
00103     if (ret == SOCKET_ERROR)
00104         throw CdcExceptionSocket (WSAGetLastError());
00105     /* Client reset connection? */
00106     if (ret == 0)
00107         throw CdcExceptionSocket (WSAECONNRESET);
00108     return ret;
00109 }
00110 
00111 
00112 int CdcSocket::Read (CdcString& msg) throw (CdcException)
00113 {
00114     int ret = Read (aBuffer, nReqMaxLength - 1);
00115     aBuffer[ret] = '\0';
00116     msg += aBuffer;
00117     return ret;
00118 }
00119 
00120 
00121 int CdcSocket::Write (const char* buff, int lng) throw (CdcException)
00122 {
00123     /* Send the buffer. */
00124     int ret = send (oSocket, buff, lng, 0);
00125 
00126     /* Some error? */
00127     if (ret == SOCKET_ERROR)
00128         throw CdcExceptionSocket (WSAGetLastError());
00129 
00130     return ret;
00131 }
00132 
00133 
00134 const CdcString& CdcSocket::GetClientAddress ()
00135 {
00136     if (sClientIP.IsEmpty () == false) return sClientIP;
00137     if (oClient.sin_addr.S_un.S_addr == 0) return EMPTY_STR;
00138 
00139     IN_ADDR& adr = oClient.sin_addr;
00140     /* Return the IP address of the client. */
00141     sClientIP = CdcString::IntToString (adr.S_un.S_un_b.s_b1) + '.' +
00142                 CdcString::IntToString (adr.S_un.S_un_b.s_b2) + '.' +
00143                 CdcString::IntToString (adr.S_un.S_un_b.s_b3) + '.' +
00144                 CdcString::IntToString (adr.S_un.S_un_b.s_b4);
00145 
00146     return sClientIP;
00147 }
00148 
00149 
00150 int CdcSocket::GetClientPort () const
00151 {
00152     return oClient.sin_port;
00153 }
00154 
00155 
00156 void CdcSocket::Startup (int max_req_len, int wait_req_time)
00157     throw (CdcException)
00158 {
00159     nReqMaxLength = max_req_len;
00160     nWaitReqTime = wait_req_time;
00161 
00162     /* load dll. */
00163     int ret = WSAStartup (0x202, &oData);
00164 
00165     /* Some error? */
00166     if (ret == SOCKET_ERROR)
00167         throw CdcExceptionSocket (WSAGetLastError());
00168 }
00169 
00170 
00171 void CdcSocket::Cleanup () throw (CdcException)
00172 {
00173     /* unregister dll. */
00174     int ret = WSACleanup();
00175 
00176     /* Some error? */
00177     if (ret == SOCKET_ERROR)
00178         throw CdcExceptionSocket (WSAGetLastError());
00179 }

Generated on Mon Oct 11 2010 16:23:25 for Beesnest by  doxygen 1.7.2