# Socket.hh

Go to the documentation of this file.

    00001 // ----------------------------------------------------------------------------
    00002 // CERTI - HLA RunTime Infrastructure
    00003 // Copyright (C) 2002-2005  ONERA
    00004 //
    00005 // This program is free software ; you can redistribute it and/or
    00006 // modify it under the terms of the GNU Lesser General Public License
    00007 // as published by the Free Software Foundation ; either version 2 of
    00008 // the License, or (at your option) any later version.
    00009 //
    00010 // This program is distributed in the hope that it will be useful, but
    00011 // WITHOUT ANY WARRANTY ; without even the implied warranty of
    00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    00013 // Lesser General Public License for more details.
    00014 //
    00015 // You should have received a copy of the GNU Lesser General Public
    00016 // License along with this program ; if not, write to the Free Software
    00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    00018 // ----------------------------------------------------------------------------
    00019
    00020 #ifndef CERTI_SOCKET_HH
    00021 #define CERTI_SOCKET_HH
    00022
    00023 #include "certi.hh"
    00024 #include "Exception.hh"
    00025 #ifndef _WIN32
    00026     #include <sys/time.h>
    00027     #include <sys/types.h>
    00028     #include <netinet/in.h>
    00029     #include <netdb.h>
    00030 #endif
    00031
    00032 #include <string>
    00033 #include <sstream>
    00034
    00035 namespace certi {
    00036
    00037 class Socket
    00038 {
    00039 public:
    00040     typedef unsigned long ByteCount_t;
    00041     virtual ~Socket() {};
    00042
    00043     virtual void createConnection(const char *server_name, unsigned int port)
    00044         throw (NetworkError) = 0;
    00045     virtual void send(const unsigned char *, size_t) = 0;
    00046     virtual void receive(void *Buffer, unsigned long Size) = 0 ;
    00047     virtual void close() = 0 ;
    00048
    00049     // This method may be used for implementation using Read Buffers,
    00050     // because in that case 'select' system calls are not trustworthy.
    00051     // See Important Note in SocketTCP.hh
    00052     virtual bool isDataReady() const = 0 ;
    00053
    00054     virtual unsigned long returnAdress() const = 0 ;
    00055
    00056     #ifdef _WIN32
    00057         virtual SOCKET returnSocket() = 0;
    00058     #else
    00059         virtual int returnSocket() = 0;
    00060     #endif
    00061
    00068     static const std::string addr2string(in_addr_t addr) {
    00069         typedef union {
    00070             uint32_t    addr;
    00071             uint8_t     parts[4];
    00072         } addr_union_t;
    00073         std::stringstream msg;
    00074
    00075         addr_union_t uaddr;
    00076         uaddr.addr = (uint32_t)ntohl((uint32_t)(addr));
    00077         msg << ""  << static_cast<int>(uaddr.parts[3])
    00078             << "." << static_cast<int>(uaddr.parts[2])
    00079             << "." << static_cast<int>(uaddr.parts[1])
    00080             << "." << static_cast<int>(uaddr.parts[0]);
    00081         return msg.str();
    00082     }
    00083 };
    00084
    00085 } // namespace certi
    00086
    00087 #endif // CERTI_SOCKET_HH