library of assembled shared sources

http://lass.cocamware.com

socket_libc.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 "../../util/impl/lass_errno.h"
00046 #include <sys/socket.h>
00047 #include <sys/types.h>
00048 #include <netinet/in.h>
00049 #include <netdb.h>
00050 #include <unistd.h>
00051 
00052 namespace lass
00053 {
00054 namespace io
00055 {
00056 namespace impl
00057 {
00058 
00059 class SocketImpl: public util::NonCopyable
00060 {
00061 public:
00062 
00063     SocketImpl():
00064         socket_(invalidSocket)
00065     {
00066     }
00067 
00068     ~SocketImpl()
00069     {
00070         try
00071         {
00072             closeSocket();
00073         }
00074         catch (std::exception& error)
00075         {
00076             std::cerr << "[LASS RUN MSG] WARNING: closeSocket() failed: " << error.what();
00077         }
00078     }
00079 
00080     void bind(unsigned short portNumber)
00081     {
00082         openSocket();
00083 
00084         ::sockaddr_in addr;
00085         addr.sin_addr.s_addr = htonl(INADDR_ANY);
00086         addr.sin_family = AF_INET;
00087         addr.sin_port = htons(portNumber);
00088 
00089         if (::bind(socket_, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) != 0)
00090         {
00091             const int err = util::impl::lass_errno();
00092             LASS_THROW_EX(SocketError, "Failed to bind socket to port " << portNumber 
00093                 << ": " << util::impl::lass_strerror(err));
00094         }
00095     }
00096 
00097     void listen()
00098     {
00099         LASS_ASSERT(socket_ != invalidSocket);
00100         if (::listen(socket_, SOMAXCONN) != 0)
00101         {
00102             const int err = util::impl::lass_errno();
00103             LASS_THROW_EX(SocketError, "Failed to listen: " << util::impl::lass_strerror(err));
00104         }
00105     }
00106 
00107     void accept(SocketImpl* connection)
00108     {
00109         LASS_ASSERT(socket_ != invalidSocket);
00110         int socket = ::accept(socket_, 0, 0);
00111         if (socket == invalidSocket)
00112         {
00113             const int err = util::impl::lass_errno();
00114             LASS_THROW_EX(SocketError, "Failed to accept connection: " << util::impl::lass_strerror(err));
00115         }
00116         LASS_ASSERT(connection);
00117         connection->socket_ = socket;
00118     }
00119 
00120     void connect(const std::string& ipAddress, unsigned short portNumber)
00121     {
00122         openSocket();
00123         
00124         ::hostent* other = gethostbyname(ipAddress.c_str());
00125         if (!other)
00126         {
00127             LASS_THROW_EX(SocketError, "could not connect " << ipAddress << ":" << portNumber
00128                 << " : failed to lookup hostname.");
00129         }       
00130 
00131         sockaddr_in dest;
00132         dest.sin_addr = *(in_addr*) other->h_addr;
00133         dest.sin_family = PF_INET;
00134         dest.sin_port = htons(portNumber);
00135         const int ret = ::connect(socket_, reinterpret_cast<sockaddr*>(&dest), sizeof(dest));
00136         if (ret != 0)
00137         {
00138             const int err = util::impl::lass_errno();
00139             LASS_THROW_EX(SocketError, "Could not connect " << ipAddress << ":" << portNumber 
00140                 << " : " << util::impl::lass_strerror(err));
00141         }
00142     }
00143 
00144     const int send(const void* begin, int length)
00145     {
00146         LASS_ASSERT(socket_ != invalidSocket);
00147         const int ret = ::send(socket_, static_cast<const char*>(begin), length, 0);
00148         if (ret == -1)
00149         {
00150             const int err = util::impl::lass_errno();
00151             LASS_THROW_EX(SocketError, "Failed to send data: " << util::impl::lass_strerror(err));
00152         }
00153         LASS_ASSERT(ret >= 0);
00154         return ret;
00155     }
00156 
00157     const int receive(void* begin, int length)
00158     {
00159         const int ret = ::recv(socket_, static_cast<char*>(begin), length, 0);
00160         if (ret == -1)
00161         {
00162             const int err = util::impl::lass_errno();
00163             LASS_THROW_EX(SocketError, "Failed to receive data: " << util::impl::lass_strerror(err));
00164         }
00165         LASS_ASSERT(ret >= 0);
00166         return ret;
00167     }
00168 
00169 private:
00170 
00171     enum { invalidSocket = -1 };
00172 
00173     void openSocket()
00174     {
00175         if (socket_ == invalidSocket)
00176         {
00177             socket_ = socket(AF_INET, SOCK_STREAM, 0);
00178             if (socket_ == invalidSocket)
00179             {
00180                 const int err = util::impl::lass_errno();
00181                 LASS_THROW_EX(SocketError, "Failed to create socket: " << util::impl::lass_strerror(err));
00182             }
00183         }
00184     }
00185     
00186     void closeSocket()
00187     {
00188         if (socket_ != invalidSocket)
00189         {
00190             const int ret = close(socket_);
00191             socket_ = invalidSocket;
00192             if (ret != 0)
00193             {
00194                 const int err = util::impl::lass_errno();
00195                 LASS_THROW_EX(SocketError, "Failed to close socket: " << util::impl::lass_strerror(err));
00196             } 
00197         }
00198     }
00199 
00200     int socket_;
00201 };
00202 
00203 }
00204 }
00205 }
00206 
00207 // 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