00001 // Module: Log4CPLUS 00002 // File: socket.h 00003 // Created: 4/2003 00004 // Author: Tad E. Smith 00005 // 00006 // 00007 // Copyright 2003-2009 Tad E. Smith 00008 // 00009 // Licensed under the Apache License, Version 2.0 (the "License"); 00010 // you may not use this file except in compliance with the License. 00011 // You may obtain a copy of the License at 00012 // 00013 // http://www.apache.org/licenses/LICENSE-2.0 00014 // 00015 // Unless required by applicable law or agreed to in writing, software 00016 // distributed under the License is distributed on an "AS IS" BASIS, 00017 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00018 // See the License for the specific language governing permissions and 00019 // limitations under the License. 00020 00023 #ifndef LOG4CPLUS_HELPERS_SOCKET_HEADER_ 00024 #define LOG4CPLUS_HELPERS_SOCKET_HEADER_ 00025 00026 #include "dcmtk/oflog/config.h" 00027 #include "dcmtk/oflog/tstring.h" 00028 #include "dcmtk/oflog/helpers/sockbuff.h" 00029 #if defined(_WIN32) 00030 #include <winsock.h> 00031 #endif 00032 00033 namespace log4cplus { 00034 namespace helpers { 00035 00036 enum SocketState { ok, 00037 not_opened, 00038 bad_address, 00039 connection_failed, 00040 broken_pipe, 00041 invalid_access_mode, 00042 message_truncated 00043 }; 00044 00045 #if !defined(_WIN32) 00046 typedef int SOCKET_TYPE; 00047 #define INVALID_SOCKET -1 00048 #else 00049 typedef SOCKET SOCKET_TYPE; 00050 #endif 00051 00052 class LOG4CPLUS_EXPORT AbstractSocket { 00053 public: 00054 // ctor and dtor 00055 AbstractSocket(); 00056 AbstractSocket(SOCKET_TYPE sock, SocketState state, int err); 00057 AbstractSocket(const AbstractSocket&); 00058 virtual ~AbstractSocket() = 0; 00059 00060 // methods 00062 virtual void close(); 00063 virtual bool isOpen() const; 00064 00065 AbstractSocket& operator=(const AbstractSocket& rhs); 00066 00067 protected: 00068 // Methods 00069 virtual void copy(const AbstractSocket& rhs); 00070 00071 // Data 00072 SOCKET_TYPE sock; 00073 SocketState state; 00074 int err; 00075 }; 00076 00077 00078 00083 class LOG4CPLUS_EXPORT Socket : public AbstractSocket { 00084 public: 00085 // ctor and dtor 00086 Socket(); 00087 Socket(SOCKET_TYPE sock, SocketState state, int err); 00088 Socket(const tstring& address, int port); 00089 virtual ~Socket(); 00090 00091 // methods 00092 virtual bool read(SocketBuffer& buffer); 00093 virtual bool write(const SocketBuffer& buffer); 00094 }; 00095 00096 00097 00104 class LOG4CPLUS_EXPORT ServerSocket : public AbstractSocket { 00105 public: 00106 // ctor and dtor 00107 ServerSocket(int port); 00108 virtual ~ServerSocket(); 00109 00110 Socket accept(); 00111 }; 00112 00113 00114 LOG4CPLUS_EXPORT SOCKET_TYPE openSocket(unsigned short port, SocketState& state); 00115 LOG4CPLUS_EXPORT SOCKET_TYPE connectSocket(const log4cplus::tstring& hostn, 00116 unsigned short port, SocketState& state); 00117 LOG4CPLUS_EXPORT SOCKET_TYPE acceptSocket(SOCKET_TYPE sock, SocketState& state); 00118 LOG4CPLUS_EXPORT int closeSocket(SOCKET_TYPE sock); 00119 00120 LOG4CPLUS_EXPORT long read(SOCKET_TYPE sock, SocketBuffer& buffer); 00121 LOG4CPLUS_EXPORT long write(SOCKET_TYPE sock, const SocketBuffer& buffer); 00122 00123 LOG4CPLUS_EXPORT tstring getHostname (bool fqdn); 00124 00125 } // end namespace helpers 00126 } // end namespace log4cplus 00127 00128 #endif // LOG4CPLUS_HELPERS_SOCKET_HEADER_