library of assembled shared sources

http://lass.cocamware.com

socket_winsock.inl

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 #include "../io_common.h"
00044 #include "../socket.h"
00045 #include <winsock.h>
00046 
00047 #if LASS_PLATFORM_TYPE == LASS_PLATFORM_TYPE_WIN32
00048 #   pragma comment(lib, "ws2_32.lib")
00049 #endif
00050 
00051 namespace lass
00052 {
00053 namespace io
00054 {
00055 namespace impl
00056 {
00057 
00058 class SocketImpl: public util::NonCopyable
00059 {
00060 public:
00061 
00062     SocketImpl():
00063         socket_(INVALID_SOCKET)
00064     {
00065         WSADATA wsaData;
00066         if (WSAStartup(0x0202, &wsaData) != 0)
00067         {
00068             LASS_THROW_EX(SocketError, "Failed to startup Windows Socket API.");
00069         }
00070         if (wsaData.wVersion != 0x0202)
00071         {
00072             WSACleanup();
00073             LASS_THROW_EX(SocketError, "Windows Socket API is not version 2.2.");
00074         }
00075     }
00076 
00077     ~SocketImpl()
00078     {
00079         if (socket_ != INVALID_SOCKET)
00080         {
00081             const int err = closesocket(socket_);
00082             LASS_ASSERT(err == 0);
00083         }
00084 
00085         const int err = WSACleanup();
00086         LASS_ASSERT(err == 0);
00087     }
00088 
00089     void bind(unsigned short iPortNumber)
00090     {
00091         openSocket();
00092 
00093         SOCKADDR_IN addr;
00094         addr.sin_addr.s_addr = htonl(INADDR_ANY);
00095         addr.sin_family = AF_INET;
00096         addr.sin_port = htons(iPortNumber);
00097 
00098         if (::bind(socket_, (LPSOCKADDR)&addr, sizeof(addr)) != 0)
00099         {
00100             LASS_THROW_EX(SocketError, "Failed to bind socket to port " << iPortNumber);
00101         }
00102     }
00103 
00104     void listen()
00105     {
00106         LASS_ASSERT(socket_ != INVALID_SOCKET);
00107 		::listen(socket_, SOMAXCONN);
00108     }
00109 
00110     void accept(SocketImpl* oConnection)
00111     {
00112         LASS_ASSERT(socket_ != INVALID_SOCKET);
00113         SOCKET connnection = ::accept(socket_, 0, 0);
00114         if (connnection == INVALID_SOCKET)
00115         {
00116             LASS_THROW_EX(SocketError, "Failed to accept connection");
00117         }
00118         LASS_ASSERT(oConnection);
00119         oConnection->socket_ = connnection;
00120     }
00121 
00122     void connect(const std::string& iIpAddress, unsigned short iPortNumber)
00123     {
00124         openSocket();
00125 
00126         SOCKADDR_IN dest;
00127         dest.sin_addr.s_addr = inet_addr(iIpAddress.c_str());
00128         dest.sin_family = PF_INET;
00129         dest.sin_port = htons(iPortNumber);
00130         const int ret = ::connect(socket_, reinterpret_cast<SOCKADDR*>(&dest), sizeof(dest));
00131         if (ret == SOCKET_ERROR)
00132         {
00133             LASS_THROW_EX(SocketError, "could not connect " << iIpAddress << ":" << iPortNumber);
00134         }
00135     }
00136 
00137     const int send(const void* iBegin, int iLength)
00138     {
00139         LASS_ASSERT(socket_ != INVALID_SOCKET);
00140         const int ret = ::send(socket_, static_cast<const char*>(iBegin), iLength, 0);
00141         if (ret == SOCKET_ERROR)
00142         {
00143             LASS_THROW_EX(SocketError, "Failure to send data: " << WSAGetLastError());
00144         }
00145         LASS_ASSERT(ret >= 0);
00146         return ret;
00147     }
00148 
00149     const int receive(void* iBegin, int iLength)
00150     {
00151         const int ret = ::recv(socket_, static_cast<char*>(iBegin), iLength, 0);
00152         if (ret == SOCKET_ERROR)
00153         {
00154             LASS_THROW_EX(SocketError, "Failure to receive data: " << WSAGetLastError());
00155         }
00156         LASS_ASSERT(ret >= 0);
00157         return ret;
00158     }
00159 
00160 private:
00161 
00162     void openSocket()
00163     {
00164         if (socket_ == INVALID_SOCKET)
00165         {
00166             socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
00167             if (socket_ == INVALID_SOCKET)
00168             {
00169                 LASS_THROW_EX(SocketError, "Failed to create socket.");
00170             }
00171         }
00172     }
00173 
00174     SOCKET socket_;
00175 };
00176 
00177 }
00178 }
00179 }
00180 
00181 // EOF

Generated on Mon Nov 10 14:21:34 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo