source: vscpl/src/hu/varadiistvan/scpl/io/_posix/Acceptor.h@ 14:e46811708269

Last change on this file since 14:e46811708269 was 8:7b2598d1ea55, checked in by István Váradi <ivaradi@…>, 12 years ago

Imported the I/O library for POSIX and the pseudo-random generator

File size: 4.9 KB
Line 
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
35#include "ServerSocket.h"
36
37#include <cstdlib>
38
39//------------------------------------------------------------------------------
40
41namespace hu { namespace varadiistvan { namespace scpl { namespace io {
42
43//------------------------------------------------------------------------------
44
45/**
46 * Class representing the accepting of an incoming connection on a
47 * socket.
48 */
49class Acceptor : public FailableReference<Acceptor>
50{
51protected:
52 /**
53 * The socket we belong to.
54 */
55 ServerSocket& socket;
56
57 /**
58 * The file descriptor accepted the last time.
59 */
60 int acceptedFD;
61
62 /**
63 * The buffer for the remote address, if we are interested.
64 */
65 unsigned char* address;
66
67 /**
68 * The length of the remote address buffer.
69 */
70 size_t addressLength;
71
72protected:
73 /**
74 * Construct the acceptor.
75 *
76 * @param addressLength the length of the buffer for the peer
77 * address. If 0, not buffer will be created.
78 */
79 Acceptor(ServerSocket* socket, size_t addressLength = 0);
80
81public:
82 /**
83 * Destroy the acceptor.
84 */
85 virtual ~Acceptor();
86
87 /**
88 * Start accepting a connection.
89 *
90 * If a connection is already accepted, but not retrieved yet, it
91 * returns true.
92 *
93 * If a new socket becomes immediately available, it returns true.
94 *
95 * If some error occurs, the acceptor is marked as failed.
96 */
97 bool accept();
98
99protected:
100 /**
101 * Called when a connection gets processed and we can become ready
102 * for the next one.
103 */
104 void connectionProcessed();
105
106private:
107 /**
108 * Get the failable object.
109 */
110 const Failable& getFailable() const;
111
112 /**
113 * Get the failable object.
114 */
115 Failable& getFailable();
116
117 friend class FailableReference<Acceptor>;
118};
119
120//------------------------------------------------------------------------------
121// Inline definitions
122//------------------------------------------------------------------------------
123
124inline Acceptor::Acceptor(ServerSocket* socket, size_t addressLength) :
125 socket(*socket),
126 acceptedFD(-1),
127 address( (addressLength==0) ? 0 : new unsigned char[addressLength]),
128 addressLength(addressLength)
129{
130}
131
132//------------------------------------------------------------------------------
133
134inline void Acceptor::connectionProcessed()
135{
136 acceptedFD = -1;
137}
138
139//------------------------------------------------------------------------------
140
141inline const Failable& Acceptor::getFailable() const
142{
143 return socket;
144}
145
146//------------------------------------------------------------------------------
147
148inline Failable& Acceptor::getFailable()
149{
150 return socket;
151}
152
153//------------------------------------------------------------------------------
154
155} /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
156
157//------------------------------------------------------------------------------
158#endif // HU_VARADIISTVAN_SCPL_IO_POSIX_ACCEPTOR_H
159
160// Local Variables:
161// mode: C++
162// c-basic-offset: 4
163// indent-tabs-mode: nil
164// End:
Note: See TracBrowser for help on using the repository browser.