log4cplus
2.0.0
|
00001 // -*- C++ -*- 00002 // Module: Log4CPLUS 00003 // File: socket.h 00004 // Created: 4/2003 00005 // Author: Tad E. Smith 00006 // 00007 // 00008 // Copyright 2003-2015 Tad E. Smith 00009 // 00010 // Licensed under the Apache License, Version 2.0 (the "License"); 00011 // you may not use this file except in compliance with the License. 00012 // You may obtain a copy of the License at 00013 // 00014 // http://www.apache.org/licenses/LICENSE-2.0 00015 // 00016 // Unless required by applicable law or agreed to in writing, software 00017 // distributed under the License is distributed on an "AS IS" BASIS, 00018 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00019 // See the License for the specific language governing permissions and 00020 // limitations under the License. 00021 00024 #ifndef LOG4CPLUS_HELPERS_SOCKET_HEADER_ 00025 #define LOG4CPLUS_HELPERS_SOCKET_HEADER_ 00026 00027 #include <log4cplus/config.hxx> 00028 00029 #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE) 00030 #pragma once 00031 #endif 00032 00033 #include <array> 00034 00035 #include <log4cplus/tstring.h> 00036 #include <log4cplus/helpers/socketbuffer.h> 00037 00038 00039 namespace log4cplus { 00040 namespace helpers { 00041 00042 enum SocketState { ok, 00043 not_opened, 00044 bad_address, 00045 connection_failed, 00046 broken_pipe, 00047 invalid_access_mode, 00048 message_truncated, 00049 accept_interrupted 00050 }; 00051 00052 typedef std::ptrdiff_t SOCKET_TYPE; 00053 00054 extern LOG4CPLUS_EXPORT SOCKET_TYPE const INVALID_SOCKET_VALUE; 00055 00056 class LOG4CPLUS_EXPORT AbstractSocket { 00057 public: 00058 AbstractSocket(); 00059 AbstractSocket(SOCKET_TYPE sock, SocketState state, int err); 00060 AbstractSocket(AbstractSocket const &) = delete; 00061 AbstractSocket(AbstractSocket &&); 00062 virtual ~AbstractSocket() = 0; 00063 00065 virtual void close(); 00066 virtual bool isOpen() const; 00067 virtual void shutdown(); 00068 AbstractSocket & operator = (AbstractSocket && rhs); 00069 00070 void swap (AbstractSocket &); 00071 00072 protected: 00073 SOCKET_TYPE sock; 00074 SocketState state; 00075 int err; 00076 }; 00077 00078 00079 00084 class LOG4CPLUS_EXPORT Socket : public AbstractSocket { 00085 public: 00086 // ctor and dtor 00087 Socket(); 00088 Socket(SOCKET_TYPE sock, SocketState state, int err); 00089 Socket(const tstring& address, unsigned short port, 00090 bool udp = false, bool ipv6 = false); 00091 Socket(Socket &&); 00092 virtual ~Socket(); 00093 00094 Socket & operator = (Socket &&); 00095 00096 // methods 00097 virtual bool read(SocketBuffer& buffer); 00098 virtual bool write(const SocketBuffer& buffer); 00099 virtual bool write(const std::string & buffer); 00100 }; 00101 00102 00103 00110 class LOG4CPLUS_EXPORT ServerSocket : public AbstractSocket { 00111 public: 00112 ServerSocket(unsigned short port, bool udp = false, bool ipv6 = false); 00113 ServerSocket(ServerSocket &&); 00114 virtual ~ServerSocket(); 00115 00116 ServerSocket & operator = (ServerSocket &&); 00117 00118 Socket accept(); 00119 void interruptAccept (); 00120 void swap (ServerSocket &); 00121 00122 protected: 00123 std::array<std::ptrdiff_t, 2> interruptHandles; 00124 }; 00125 00126 00127 LOG4CPLUS_EXPORT SOCKET_TYPE openSocket(unsigned short port, bool udp, 00128 bool ipv6, SocketState& state); 00129 LOG4CPLUS_EXPORT SOCKET_TYPE connectSocket(const log4cplus::tstring& hostn, 00130 unsigned short port, bool udp, bool ipv6, SocketState& state); 00131 LOG4CPLUS_EXPORT SOCKET_TYPE acceptSocket(SOCKET_TYPE sock, SocketState& state); 00132 LOG4CPLUS_EXPORT int closeSocket(SOCKET_TYPE sock); 00133 LOG4CPLUS_EXPORT int shutdownSocket(SOCKET_TYPE sock); 00134 00135 LOG4CPLUS_EXPORT long read(SOCKET_TYPE sock, SocketBuffer& buffer); 00136 LOG4CPLUS_EXPORT long write(SOCKET_TYPE sock, 00137 const SocketBuffer& buffer); 00138 LOG4CPLUS_EXPORT long write(SOCKET_TYPE sock, 00139 const std::string & buffer); 00140 00141 LOG4CPLUS_EXPORT tstring getHostname (bool fqdn); 00142 LOG4CPLUS_EXPORT int setTCPNoDelay (SOCKET_TYPE, bool); 00143 00144 } // end namespace helpers 00145 } // end namespace log4cplus 00146 00147 #endif // LOG4CPLUS_HELPERS_SOCKET_HEADER_