1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 #ifndef _LOG4CXX_HELPERS_DATAGRAM_SOCKET_H 19 #define _LOG4CXX_HELPERS_DATAGRAM_SOCKET_H 20 21 #include <log4cxx/helpers/objectimpl.h> 22 #include <log4cxx/helpers/objectptr.h> 23 #include <log4cxx/helpers/inetaddress.h> 24 #include <log4cxx/helpers/pool.h> 25 #include <log4cxx/helpers/datagrampacket.h> 26 27 extern "C" { 28 struct apr_socket_t; 29 } 30 31 namespace log4cxx 32 { 33 namespace helpers 34 { 35 /** This class represents a socket for sending and receiving 36 datagram packets.*/ 37 class LOG4CXX_EXPORT DatagramSocket : public helpers::ObjectImpl 38 { 39 public: 40 DECLARE_ABSTRACT_LOG4CXX_OBJECT(DatagramSocket) 41 BEGIN_LOG4CXX_CAST_MAP() 42 LOG4CXX_CAST_ENTRY(DatagramSocket) 43 END_LOG4CXX_CAST_MAP() 44 45 /** Constructs a datagram socket and binds it to any available port 46 on the local host machine.*/ 47 DatagramSocket(); 48 49 /** Constructs a datagram socket and binds it to the specified 50 port on the local host machine. */ 51 DatagramSocket(int port); 52 53 /** Creates a datagram socket, bound to the specified local 54 address. */ 55 DatagramSocket(int port, InetAddressPtr laddr); 56 57 /** ensure the socket is closed. */ 58 ~DatagramSocket(); 59 60 /** Binds a datagram socket to a local port and address.*/ 61 void bind(int lport, InetAddressPtr laddress); 62 63 /** Creates a datagram socket.*/ 64 void create(); 65 66 /** Closes this datagram socket */ 67 void close(); 68 69 /** Connects the socket to a remote address for this socket. */ 70 void connect(InetAddressPtr address, int port); 71 72 /** Returns the address to which this socket is connected. */ getInetAddress()73 inline InetAddressPtr getInetAddress() const 74 { 75 return address; 76 } 77 78 /** Gets the local address to which the socket is bound. */ getLocalAddress()79 inline InetAddressPtr getLocalAddress() const 80 { 81 return localAddress; 82 } 83 84 /** Returns the port number on the local host to which this 85 socket is bound. */ getLocalPort()86 inline int getLocalPort() const 87 { 88 return localPort; 89 } 90 91 /** Returns the port for this socket */ getPort()92 inline int getPort() const 93 { 94 return port; 95 } 96 97 /** Returns the binding state of the socket. **/ isBound()98 inline bool isBound() const 99 { 100 return localPort != 0; 101 } 102 103 /** Returns wether the socket is closed or not. */ isClosed()104 inline bool isClosed() const 105 { 106 return socket != 0; 107 } 108 109 /** Returns the connection state of the socket. */ isConnected()110 inline bool isConnected() const 111 { 112 return port != 0; 113 } 114 115 /** Receives a datagram packet from this socket. */ 116 void receive(DatagramPacketPtr& p); 117 118 /** Sends a datagram packet from this socket. */ 119 void send(DatagramPacketPtr& p); 120 121 private: 122 DatagramSocket(const DatagramSocket&); 123 DatagramSocket& operator=(const DatagramSocket&); 124 /** The APR socket */ 125 apr_socket_t* socket; 126 127 /** The memory pool for the socket */ 128 Pool socketPool; 129 130 InetAddressPtr address; 131 132 InetAddressPtr localAddress; 133 134 int port; 135 136 /** The local port number to which this socket is connected. */ 137 int localPort; 138 139 }; 140 LOG4CXX_PTR_DEF(DatagramSocket); 141 } // namespace helpers 142 } // namespace log4cxx 143 144 #endif //_LOG4CXX_HELPERS_DATAGRAM_SOCKET_H 145