source: xplcommon/src/xplcommon/win32/LocalAcceptor.h@ 23:e6c4c31ce833

Last change on this file since 23:e6c4c31ce833 was 23:e6c4c31ce833, checked in by István Váradi <ivaradi@…>, 11 years ago

Eliminated the double storage of the Overlappable instances in the children of Completer

File size: 6.2 KB
Line 
1// Copyright (c) 2012 by István Váradi
2
3// This file is part of libxplcommon, a common utility library for
4// development related to X-Plane
5
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are met:
8
9// 1. Redistributions of source code must retain the above copyright notice, this
10// list of conditions and the following disclaimer.
11// 2. Redistributions in binary form must reproduce the above copyright notice,
12// this list of conditions and the following disclaimer in the documentation
13// and/or other materials provided with the distribution.
14
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26// The views and conclusions contained in the software and documentation are those
27// of the authors and should not be interpreted as representing official policies,
28// either expressed or implied, of the FreeBSD Project.
29
30#ifndef XPLCOMMON_WIN32_LOCALACCEPTOR_H
31#define XPLCOMMON_WIN32_LOCALACCEPTOR_H
32//------------------------------------------------------------------------------
33
34#include "Completer.h"
35
36#include "LocalServerSocketBase.h"
37#include "LocalSocket.h"
38
39//------------------------------------------------------------------------------
40
41namespace xplcommon { namespace win32 {
42
43//------------------------------------------------------------------------------
44
45/**
46 * Instances of this class are used to accept connection requests to a
47 * local server socket, i.e. named pipe.
48 */
49class LocalAcceptor : public Completer,
50 public FailableReference<LocalAcceptor>
51{
52private:
53 /**
54 * The path of the pipe to accept.
55 */
56 char path[256];
57
58 /**
59 * Indicate if the connection has been accepted.
60 */
61 bool accepted;
62
63protected:
64 /**
65 * Construct the acceptor.
66 */
67 LocalAcceptor(LocalServerSocketBase* socket, const char* name);
68
69 /**
70 * Get the socket.
71 *
72 * It returns the overlappable from the Completer part, knowing
73 * that it must be a LocalServerSocketBase.
74 */
75 LocalServerSocketBase& getServerSocket() const;
76
77public:
78 /**
79 * Try to accept a connection.
80 *
81 * If one is already accepted, but not retrieved yet, return true.
82 *
83 * If a new connection becomes immediately available, return true.
84 *
85 * If a new connection is not yet available, returns false.
86 *
87 * If some error occurs, the acceptor is marked failed and false
88 * is returned.
89 */
90 bool accept();
91
92 /**
93 * Get the local socket accepted last. If no socket was accepted,
94 * return 0. The local socket's waiter will be the given one.
95 */
96 LocalSocket* getSocket(Waiter* waiter,
97 size_t readingCapacity = LocalSocket::DEFAULT_CAPACITY,
98 size_t writingCapacity = LocalSocket::DEFAULT_CAPACITY);
99 /**
100 * Get the local socket accepted last. If no socket was accepted,
101 * return 0. The local socket's waiter will be the same as that of
102 * the associated server socket.
103 */
104 LocalSocket* getSocket(size_t readingCapacity = LocalSocket::DEFAULT_CAPACITY,
105 size_t writingCapacity = LocalSocket::DEFAULT_CAPACITY);
106
107
108protected:
109 /**
110 * Set the indicator of an accepted connection to true.
111 *
112 * @see Completer::handleWaitedResult
113 */
114 virtual void handleWaitedResult(DWORD size);
115
116private:
117 /**
118 * Get the failable object.
119 */
120 const ::xplcommon::Failable& getFailable() const;
121
122 /**
123 * Get the failable object.
124 */
125 ::xplcommon::Failable& getFailable();
126
127 friend class FailableReference<LocalAcceptor>;
128 friend class LocalServerSocket;
129};
130
131//------------------------------------------------------------------------------
132// Inline definitions
133//------------------------------------------------------------------------------
134
135inline LocalServerSocketBase& LocalAcceptor::getServerSocket() const
136{
137 return static_cast<LocalServerSocketBase&>(overlappable);
138}
139
140//------------------------------------------------------------------------------
141
142inline LocalSocket* LocalAcceptor::getSocket(Waiter* waiter,
143 size_t readingCapacity,
144 size_t writingCapacity)
145{
146 if (!accepted) return 0;
147
148 LocalSocket* s = new LocalSocket(waiter, getServerSocket().releaseHandle(),
149 readingCapacity, writingCapacity);
150 accepted = false;
151 return s;
152}
153
154//------------------------------------------------------------------------------
155
156inline LocalSocket* LocalAcceptor::getSocket(size_t readingCapacity,
157 size_t writingCapacity)
158{
159 return getSocket(getServerSocket().waiter,
160 readingCapacity, writingCapacity);
161}
162
163//------------------------------------------------------------------------------
164
165inline const ::xplcommon::Failable& LocalAcceptor::getFailable() const
166{
167 return overlappable;
168}
169
170//------------------------------------------------------------------------------
171
172inline ::xplcommon::Failable& LocalAcceptor::getFailable()
173{
174 return overlappable;
175}
176
177//------------------------------------------------------------------------------
178
179} /* namespace xplcommon::win32 */ } /* namespace xplcommon */
180
181//------------------------------------------------------------------------------
182#endif // XPLCOMMON_WIN32_LOCALACCEPTOR_H
183
184// Local Variables:
185// mode: C++
186// c-basic-offset: 4
187// indent-tabs-mode: nil
188// End:
Note: See TracBrowser for help on using the repository browser.