log4cplus
1.1.0
|
00001 // -*- C++ -*- 00002 // Module: Log4CPLUS 00003 // File: socket.h 00004 // Created: 4/2003 00005 // Author: Tad E. Smith 00006 // 00007 // 00008 // Copyright 2003-2010 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 <log4cplus/tstring.h> 00034 #include <log4cplus/helpers/socketbuffer.h> 00035 00036 00037 namespace log4cplus { 00038 namespace helpers { 00039 00040 enum SocketState { ok, 00041 not_opened, 00042 bad_address, 00043 connection_failed, 00044 broken_pipe, 00045 invalid_access_mode, 00046 message_truncated 00047 }; 00048 00049 typedef std::ptrdiff_t SOCKET_TYPE; 00050 00051 extern LOG4CPLUS_EXPORT SOCKET_TYPE const INVALID_SOCKET_VALUE; 00052 00053 class LOG4CPLUS_EXPORT AbstractSocket { 00054 public: 00055 // ctor and dtor 00056 AbstractSocket(); 00057 AbstractSocket(SOCKET_TYPE sock, SocketState state, int err); 00058 AbstractSocket(const AbstractSocket&); 00059 virtual ~AbstractSocket() = 0; 00060 00061 // methods 00063 virtual void close(); 00064 virtual bool isOpen() const; 00065 00066 AbstractSocket& operator=(const AbstractSocket& rhs); 00067 00068 protected: 00069 // Methods 00070 virtual void copy(const AbstractSocket& rhs); 00071 00072 // Data 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, bool udp = false); 00090 virtual ~Socket(); 00091 00092 // methods 00093 virtual bool read(SocketBuffer& buffer); 00094 virtual bool write(const SocketBuffer& buffer); 00095 virtual bool write(const std::string & buffer); 00096 }; 00097 00098 00099 00106 class LOG4CPLUS_EXPORT ServerSocket : public AbstractSocket { 00107 public: 00108 // ctor and dtor 00109 ServerSocket(unsigned short port); 00110 virtual ~ServerSocket(); 00111 00112 Socket accept(); 00113 }; 00114 00115 00116 LOG4CPLUS_EXPORT SOCKET_TYPE openSocket(unsigned short port, SocketState& state); 00117 LOG4CPLUS_EXPORT SOCKET_TYPE connectSocket(const log4cplus::tstring& hostn, 00118 unsigned short port, bool udp, 00119 SocketState& state); 00120 LOG4CPLUS_EXPORT SOCKET_TYPE acceptSocket(SOCKET_TYPE sock, SocketState& state); 00121 LOG4CPLUS_EXPORT int closeSocket(SOCKET_TYPE sock); 00122 00123 LOG4CPLUS_EXPORT long read(SOCKET_TYPE sock, SocketBuffer& buffer); 00124 LOG4CPLUS_EXPORT long write(SOCKET_TYPE sock, 00125 const SocketBuffer& buffer); 00126 LOG4CPLUS_EXPORT long write(SOCKET_TYPE sock, 00127 const std::string & buffer); 00128 00129 LOG4CPLUS_EXPORT tstring getHostname (bool fqdn); 00130 LOG4CPLUS_EXPORT int setTCPNoDelay (SOCKET_TYPE, bool); 00131 00132 } // end namespace helpers 00133 } // end namespace log4cplus 00134 00135 #endif // LOG4CPLUS_HELPERS_SOCKET_HEADER_