# socket.h

Go to the documentation of this file.

    00001 // Module:  Log4CPLUS
    00002 // File:    socket.h
    00003 // Created: 4/2003
    00004 // Author:  Tad E. Smith
    00005 //
    00006 //
    00007 // Copyright (C) Tad E. Smith  All rights reserved.
    00008 //
    00009 // This software is published under the terms of the Apache Software
    00010 // License version 1.1, a copy of which has been included with this
    00011 // distribution in the LICENSE.APL file.
    00012 //
    00013
    00016 #ifndef LOG4CPLUS_HELPERS_SOCKET_HEADER_
    00017 #define LOG4CPLUS_HELPERS_SOCKET_HEADER_
    00018
    00019 #include <log4cplus/config.h>
    00020 #include <log4cplus/tstring.h>
    00021 #include <log4cplus/helpers/socketbuffer.h>
    00022 #if defined(_WIN32)
    00023 #include <winsock.h>
    00024 #endif
    00025
    00026 namespace log4cplus {
    00027     namespace helpers {
    00028
    00029         enum SocketState { ok,
    00030                            not_opened,
    00031                            bad_address,
    00032                            connection_failed,
    00033                            broken_pipe,
    00034                            invalid_access_mode,
    00035                            message_truncated
    00036                          };
    00037
    00038 #if !defined(_WIN32)
    00039         typedef int SOCKET_TYPE;
    00040 #define INVALID_SOCKET -1
    00041 #else
    00042         typedef SOCKET SOCKET_TYPE;
    00043 #endif
    00044
    00045         class LOG4CPLUS_EXPORT AbstractSocket {
    00046         public:
    00047           // ctor and dtor
    00048             AbstractSocket();
    00049             AbstractSocket(SOCKET_TYPE sock, SocketState state, int err);
    00050             AbstractSocket(const AbstractSocket&);
    00051             virtual ~AbstractSocket() = 0;
    00052
    00053           // methods
    00055             virtual void close();
    00056             virtual bool isOpen() const;
    00057
    00058             AbstractSocket& operator=(const AbstractSocket& rhs);
    00059
    00060         protected:
    00061           // Methods
    00062             virtual void copy(const AbstractSocket& rhs);
    00063
    00064           // Data
    00065             SOCKET_TYPE sock;
    00066             SocketState state;
    00067             int err;
    00068         };
    00069
    00070
    00071
    00076         class LOG4CPLUS_EXPORT Socket : public AbstractSocket {
    00077         public:
    00078           // ctor and dtor
    00079             Socket();
    00080             Socket(SOCKET_TYPE sock, SocketState state, int err);
    00081             Socket(const tstring& address, int port);
    00082             virtual ~Socket();
    00083
    00084           // methods
    00085             virtual bool read(SocketBuffer& buffer);
    00086             virtual bool write(const SocketBuffer& buffer);
    00087         };
    00088
    00089
    00090
    00097         class LOG4CPLUS_EXPORT ServerSocket : public AbstractSocket {
    00098         public:
    00099           // ctor and dtor
    00100             ServerSocket(int port);
    00101             virtual ~ServerSocket();
    00102
    00103             Socket accept();
    00104         };
    00105
    00106
    00107         LOG4CPLUS_EXPORT SOCKET_TYPE openSocket(unsigned short port, SocketState& state);
    00108         LOG4CPLUS_EXPORT SOCKET_TYPE connectSocket(const log4cplus::tstring& hostn,
    00109                                                    unsigned short port, SocketState& state);
    00110         LOG4CPLUS_EXPORT SOCKET_TYPE acceptSocket(SOCKET_TYPE sock, SocketState& state);
    00111         LOG4CPLUS_EXPORT int closeSocket(SOCKET_TYPE sock);
    00112
    00113         LOG4CPLUS_EXPORT size_t read(SOCKET_TYPE sock, SocketBuffer& buffer);
    00114         LOG4CPLUS_EXPORT size_t write(SOCKET_TYPE sock, const SocketBuffer& buffer);
    00115
    00116     } // end namespace helpers
    00117 } // end namespace log4cplus
    00118
    00119 #endif // LOG4CPLUS_HELPERS_SOCKET_HEADER_