# SocketTCP.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_TCP_HH
    00021 #define CERTI_SOCKET_TCP_HH
    00022
    00023 #include "Socket.hh"
    00024 #include "certi.hh"
    00025
    00026 #ifndef _WIN32
    00027     #include <sys/socket.h>
    00028     #include <netinet/in.h>
    00029 #endif
    00030
    00031 // This is the read buffer of TCP sockets. It must be at least as long
    00032 // as the longest data ever received by a socket.
    00033 // If the next line is commented out, no buffer will be used at all.
    00034 //#define SOCKTCP_BUFFER_LENGTH 4096
    00035
    00036 namespace certi {
    00037
    00048 class CERTI_EXPORT SocketTCP : public Socket
    00049 {
    00050 public :
    00051     SocketTCP();
    00052     virtual ~SocketTCP();
    00053     virtual void close();
    00054
    00055     virtual void createConnection(const char *server_name, unsigned int port)
    00056         throw (NetworkError);
    00057     void createTCPClient(in_port_t port, in_addr_t addr) throw (NetworkError);
    00058     void createTCPServer(in_port_t port = 0, in_addr_t addr = INADDR_ANY) throw (NetworkError);
    00059
    00060     int accept(SocketTCP *serveur) throw (NetworkError);
    00061     virtual void send(const unsigned char *, size_t)        throw (NetworkError, NetworkSignal);
    00062     virtual void receive(void *Buffer, unsigned long Size)  throw (NetworkError, NetworkSignal);
    00063
    00064     virtual bool isDataReady() const ;
    00065
    00066     virtual unsigned long returnAdress() const ;
    00067
    00068     SocketTCP &operator=(SocketTCP &theSocket);
    00069
    00070     #ifdef _WIN32
    00071         SOCKET returnSocket();
    00072         static bool winsockStartup();
    00073         static void winsockShutdown();
    00074         static bool winsockInitialized()    { return (winsockInits > 0);}
    00075     #else
    00076                 virtual int returnSocket() ;
    00077     #endif
    00078
    00079 protected:
    00080     int timeoutTCP(int, int);
    00081
    00082     ByteCount_t SentBytesCount;
    00083     ByteCount_t RcvdBytesCount;
    00084
    00085 private:
    00086     int open();
    00087     int connect(in_port_t port, in_addr_t addr);
    00088     int listen(unsigned long howMuch=5);
    00089     int bind(in_port_t port=0, in_addr_t addr=INADDR_ANY);
    00090     void setPort(in_port_t port);
    00091     in_port_t getPort() const ;
    00092     in_addr_t getAddr() const ;
    00093
    00094     #ifdef _WIN32
    00095       SOCKET _socket_tcp;
    00096       static int winsockInits;
    00097     #else
    00098       long _socket_tcp;
    00099     #endif
    00100 bool    _est_init_tcp;
    00101 struct sockaddr_in _sockIn;
    00102
    00103 #ifdef SOCKTCP_BUFFER_LENGTH
    00104     // This class can use a buffer to reduce the number of systems calls
    00105     // when reading a lot of small amouts of data. Each time a Receive
    00106     // is made, it will try to read SOCKTCP_BUFFER_LENGTH
    00107     char ReadBuffer[SOCKTCP_BUFFER_LENGTH] ;
    00108     unsigned long RBLength ;
    00109 #endif
    00110 };
    00111
    00112 } // namespace certi
    00113
    00114 #endif // CERTI_SOCKET_TCP_HH