Subversion Repositories Programming Utils

Rev

Rev 113 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
113 rm5248 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
 
115 rm5248 27
#ifdef LOG4CXX_QT
28
    #include <QUdpSocket>
29
#else
113 rm5248 30
extern "C" { struct apr_socket_t; }
115 rm5248 31
#endif
113 rm5248 32
 
33
namespace log4cxx
34
{
35
        namespace helpers
36
        {
37
                /** This class represents a socket for sending and receiving
38
                datagram packets.*/
39
                class LOG4CXX_EXPORT DatagramSocket : public helpers::ObjectImpl
40
                {
41
                public:
42
                        DECLARE_ABSTRACT_LOG4CXX_OBJECT(DatagramSocket)
43
                        BEGIN_LOG4CXX_CAST_MAP()
44
                                LOG4CXX_CAST_ENTRY(DatagramSocket)
45
                        END_LOG4CXX_CAST_MAP()
46
 
47
                        /** Constructs a datagram socket and binds it to any available port
48
                        on the local host machine.*/
49
                        DatagramSocket();
50
 
51
                        /** Constructs a datagram socket and binds it to the specified
52
                        port on the local host machine. */
53
                        DatagramSocket(int port);
54
 
55
                        /**  Creates a datagram socket, bound to the specified local
56
                        address. */
57
                        DatagramSocket(int port, InetAddressPtr laddr);
58
 
59
                        /** ensure the socket is closed. */
60
                        ~DatagramSocket();
61
 
62
                        /**  Binds a datagram socket to a local port and address.*/
63
                        void bind(int lport, InetAddressPtr laddress);
64
 
65
                        /** Creates a datagram socket.*/
66
                        void create();
67
 
68
                        /** Closes this datagram socket */
69
                        void close();
70
 
71
                        /** Connects the socket to a remote address for this socket. */
72
                        void connect(InetAddressPtr address, int port);
73
 
74
                        /** Returns the address to which this socket is connected. */
75
                        inline InetAddressPtr getInetAddress() const
76
                                { return address; }
77
 
78
                        /** Gets the local address to which the socket is bound. */
79
                        inline InetAddressPtr getLocalAddress() const
80
                                { return localAddress; }
81
 
82
                        /**  Returns the port number on the local host to which this
83
                        socket is bound. */
84
                        inline int getLocalPort() const
85
                                { return localPort; }
86
 
87
                        /** Returns the port for this socket */
88
                        inline int getPort() const
89
                                { return port; }
90
 
91
                        /** Returns the binding state of the socket. **/
92
                        inline bool isBound() const
93
                                { return localPort != 0; }
94
 
95
                        /** Returns wether the socket is closed or not. */
96
                        inline bool isClosed() const
97
                                { return socket != 0; }
98
 
99
                        /** Returns the connection state of the socket. */
100
                        inline bool isConnected() const
101
                                { return port != 0; }
102
 
103
                        /**  Receives a datagram packet from this socket. */
104
                        void receive(DatagramPacketPtr& p);
105
 
106
                        /** Sends a datagram packet from this socket. */
107
                        void  send(DatagramPacketPtr& p);
108
 
109
                private:
110
                        DatagramSocket(const DatagramSocket&);
111
                        DatagramSocket& operator=(const DatagramSocket&);
115 rm5248 112
#ifdef LOG4CXX_QT
113
                    QUdpSocket* socket;
114
#else
113 rm5248 115
                        /** The APR socket */
116
                        apr_socket_t *socket;
115 rm5248 117
#endif
113 rm5248 118
 
119
                        /** The memory pool for the socket */
120
                        Pool socketPool;
121
 
122
                        InetAddressPtr address;
123
 
124
                        InetAddressPtr localAddress;
125
 
126
                        int port;
127
 
128
                        /** The local port number to which this socket is connected. */
129
                        int localPort;
130
 
131
                };
132
            LOG4CXX_PTR_DEF(DatagramSocket);
133
        }  // namespace helpers
134
} // namespace log4cxx
135
 
136
#endif //_LOG4CXX_HELPERS_DATAGRAM_SOCKET_H