1 | // Copyright (c) 2013 by István Váradi
|
---|
2 |
|
---|
3 | // This file is part of VSCPL, a simple cross-platform utility library
|
---|
4 |
|
---|
5 | // Redistribution and use in source and binary forms, with or without
|
---|
6 | // modification, are permitted provided that the following conditions are met:
|
---|
7 |
|
---|
8 | // 1. Redistributions of source code must retain the above copyright notice, this
|
---|
9 | // list of conditions and the following disclaimer.
|
---|
10 | // 2. Redistributions in binary form must reproduce the above copyright notice,
|
---|
11 | // this list of conditions and the following disclaimer in the documentation
|
---|
12 | // and/or other materials provided with the distribution.
|
---|
13 |
|
---|
14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
---|
15 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
---|
16 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
17 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
---|
18 | // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
19 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
20 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
21 | // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
22 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
23 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
24 |
|
---|
25 | // The views and conclusions contained in the software and documentation are those
|
---|
26 | // of the authors and should not be interpreted as representing official policies,
|
---|
27 | // either expressed or implied, of the FreeBSD Project.
|
---|
28 |
|
---|
29 | #ifndef HU_VARADIISTVAN_SCPL_IO_POSIX_ACCEPTOR_H
|
---|
30 | #define HU_VARADIISTVAN_SCPL_IO_POSIX_ACCEPTOR_H
|
---|
31 | //------------------------------------------------------------------------------
|
---|
32 |
|
---|
33 | #include "../Failable.h"
|
---|
34 | #include "../BufferedStreamAcceptor.h"
|
---|
35 |
|
---|
36 | #include "ServerSocket.h"
|
---|
37 |
|
---|
38 | #include <cstdlib>
|
---|
39 |
|
---|
40 | //------------------------------------------------------------------------------
|
---|
41 |
|
---|
42 | namespace hu { namespace varadiistvan { namespace scpl { namespace io {
|
---|
43 |
|
---|
44 | //------------------------------------------------------------------------------
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Class representing the accepting of an incoming connection on a
|
---|
48 | * socket.
|
---|
49 | */
|
---|
50 | class Acceptor : public FailableReference<Acceptor>,
|
---|
51 | public BufferedStreamAcceptor
|
---|
52 | {
|
---|
53 | protected:
|
---|
54 | /**
|
---|
55 | * The socket we belong to.
|
---|
56 | */
|
---|
57 | ServerSocket& socket;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * The file descriptor accepted the last time.
|
---|
61 | */
|
---|
62 | int acceptedFD;
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * The buffer for the remote address, if we are interested.
|
---|
66 | */
|
---|
67 | unsigned char* address;
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * The length of the remote address buffer.
|
---|
71 | */
|
---|
72 | size_t addressLength;
|
---|
73 |
|
---|
74 | protected:
|
---|
75 | /**
|
---|
76 | * Construct the acceptor.
|
---|
77 | *
|
---|
78 | * @param addressLength the length of the buffer for the peer
|
---|
79 | * address. If 0, not buffer will be created.
|
---|
80 | */
|
---|
81 | Acceptor(ServerSocket* socket, size_t addressLength = 0);
|
---|
82 |
|
---|
83 | public:
|
---|
84 | /**
|
---|
85 | * Destroy the acceptor.
|
---|
86 | */
|
---|
87 | virtual ~Acceptor();
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Start accepting a connection.
|
---|
91 | *
|
---|
92 | * If a connection is already accepted, but not retrieved yet, it
|
---|
93 | * returns true.
|
---|
94 | *
|
---|
95 | * If a new socket becomes immediately available, it returns true.
|
---|
96 | *
|
---|
97 | * If some error occurs, the acceptor is marked as failed.
|
---|
98 | */
|
---|
99 | bool accept();
|
---|
100 |
|
---|
101 | protected:
|
---|
102 | /**
|
---|
103 | * Called when a connection gets processed and we can become ready
|
---|
104 | * for the next one.
|
---|
105 | */
|
---|
106 | void connectionProcessed();
|
---|
107 |
|
---|
108 | private:
|
---|
109 | /**
|
---|
110 | * Get the failable object.
|
---|
111 | */
|
---|
112 | const Failable& getFailable() const;
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Get the failable object.
|
---|
116 | */
|
---|
117 | Failable& getFailable();
|
---|
118 |
|
---|
119 | friend class FailableReference<Acceptor>;
|
---|
120 | };
|
---|
121 |
|
---|
122 | //------------------------------------------------------------------------------
|
---|
123 | // Inline definitions
|
---|
124 | //------------------------------------------------------------------------------
|
---|
125 |
|
---|
126 | inline Acceptor::Acceptor(ServerSocket* socket, size_t addressLength) :
|
---|
127 | socket(*socket),
|
---|
128 | acceptedFD(-1),
|
---|
129 | address( (addressLength==0) ? 0 : new unsigned char[addressLength]),
|
---|
130 | addressLength(addressLength)
|
---|
131 | {
|
---|
132 | }
|
---|
133 |
|
---|
134 | //------------------------------------------------------------------------------
|
---|
135 |
|
---|
136 | inline void Acceptor::connectionProcessed()
|
---|
137 | {
|
---|
138 | acceptedFD = -1;
|
---|
139 | }
|
---|
140 |
|
---|
141 | //------------------------------------------------------------------------------
|
---|
142 |
|
---|
143 | inline const Failable& Acceptor::getFailable() const
|
---|
144 | {
|
---|
145 | return socket;
|
---|
146 | }
|
---|
147 |
|
---|
148 | //------------------------------------------------------------------------------
|
---|
149 |
|
---|
150 | inline Failable& Acceptor::getFailable()
|
---|
151 | {
|
---|
152 | return socket;
|
---|
153 | }
|
---|
154 |
|
---|
155 | //------------------------------------------------------------------------------
|
---|
156 |
|
---|
157 | } /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
|
---|
158 |
|
---|
159 | //------------------------------------------------------------------------------
|
---|
160 | #endif // HU_VARADIISTVAN_SCPL_IO_POSIX_ACCEPTOR_H
|
---|
161 |
|
---|
162 | // Local Variables:
|
---|
163 | // mode: C++
|
---|
164 | // c-basic-offset: 4
|
---|
165 | // indent-tabs-mode: nil
|
---|
166 | // End:
|
---|