source: xplcommon/src/xplcommon/win32/LocalAcceptor.h@ 24:efa33e16e135

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

Made Completer a FailableReference

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