source: xplcommon/src/xplcommon/win32/LocalAcceptor.h@ 22:d13fbc745959

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

Cleaned up and simplified the code

File size: 5.8 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 local server socket for which we are accepting connections.
55 */
56 LocalServerSocketBase& socket;
57
58 /**
59 * The path of the pipe to accept.
60 */
61 char path[256];
62
63 /**
64 * Indicate if the connection has been accepted.
65 */
66 bool accepted;
67
68protected:
69 /**
70 * Construct the acceptor.
71 */
72 LocalAcceptor(LocalServerSocketBase* socket, const char* name);
73
74public:
75 /**
76 * Try to accept a connection.
77 *
78 * If one is already accepted, but not retrieved yet, return true.
79 *
80 * If a new connection becomes immediately available, return true.
81 *
82 * If a new connection is not yet available, returns false.
83 *
84 * If some error occurs, the acceptor is marked failed and false
85 * is returned.
86 */
87 bool accept();
88
89 /**
90 * Get the local socket accepted last. If no socket was accepted,
91 * return 0. The local socket's waiter will be the given one.
92 */
93 LocalSocket* getSocket(Waiter* waiter,
94 size_t readingCapacity = LocalSocket::DEFAULT_CAPACITY,
95 size_t writingCapacity = LocalSocket::DEFAULT_CAPACITY);
96 /**
97 * Get the local socket accepted last. If no socket was accepted,
98 * return 0. The local socket's waiter will be the same as that of
99 * the associated server socket.
100 */
101 LocalSocket* getSocket(size_t readingCapacity = LocalSocket::DEFAULT_CAPACITY,
102 size_t writingCapacity = LocalSocket::DEFAULT_CAPACITY);
103
104
105protected:
106 /**
107 * Set the indicator of an accepted connection to true.
108 *
109 * @see Completer::handleWaitedResult
110 */
111 virtual void handleWaitedResult(DWORD size);
112
113private:
114 /**
115 * Get the failable object.
116 */
117 const ::xplcommon::Failable& getFailable() const;
118
119 /**
120 * Get the failable object.
121 */
122 ::xplcommon::Failable& getFailable();
123
124 friend class FailableReference<LocalAcceptor>;
125 friend class LocalServerSocket;
126};
127
128//------------------------------------------------------------------------------
129// Inline definitions
130//------------------------------------------------------------------------------
131
132inline LocalSocket* LocalAcceptor::getSocket(Waiter* waiter,
133 size_t readingCapacity,
134 size_t writingCapacity)
135{
136 if (!accepted) return 0;
137
138 LocalSocket* s = new LocalSocket(waiter, socket.releaseHandle(),
139 readingCapacity, writingCapacity);
140 accepted = false;
141 return s;
142}
143//------------------------------------------------------------------------------
144
145inline LocalSocket* LocalAcceptor::getSocket(size_t readingCapacity,
146 size_t writingCapacity)
147{
148 return getSocket(socket.waiter, readingCapacity, writingCapacity);
149}
150
151//------------------------------------------------------------------------------
152
153inline const ::xplcommon::Failable& LocalAcceptor::getFailable() const
154{
155 return socket;
156}
157
158//------------------------------------------------------------------------------
159
160inline ::xplcommon::Failable& LocalAcceptor::getFailable()
161{
162 return socket;
163}
164
165//------------------------------------------------------------------------------
166
167} /* namespace xplcommon::win32 */ } /* namespace xplcommon */
168
169//------------------------------------------------------------------------------
170#endif // XPLCOMMON_WIN32_LOCALACCEPTOR_H
171
172// Local Variables:
173// mode: C++
174// c-basic-offset: 4
175// indent-tabs-mode: nil
176// End:
Note: See TracBrowser for help on using the repository browser.