source: xplcommon/src/xplcommon/win32/LocalAcceptor.h@ 21:eb59943050c9

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

Added the implementation of the local sockets for Win32 and it seems to work

File size: 5.6 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 name of the pipe to accept.
60 */
61 char name[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
105private:
106 /**
107 * Get the failable object.
108 */
109 const ::xplcommon::Failable& getFailable() const;
110
111 /**
112 * Get the failable object.
113 */
114 ::xplcommon::Failable& getFailable();
115
116 friend class FailableReference<LocalAcceptor>;
117 friend class LocalServerSocket;
118};
119
120//------------------------------------------------------------------------------
121// Inline definitions
122//------------------------------------------------------------------------------
123
124inline LocalSocket* LocalAcceptor::getSocket(Waiter* waiter,
125 size_t readingCapacity,
126 size_t writingCapacity)
127{
128 if (!accepted) return 0;
129
130 LocalSocket* s= new LocalSocket(waiter, socket.releaseHandle(),
131 readingCapacity, writingCapacity);
132 accepted = false;
133 return s;
134}
135//------------------------------------------------------------------------------
136
137inline LocalSocket* LocalAcceptor::getSocket(size_t readingCapacity,
138 size_t writingCapacity)
139{
140 return getSocket(socket.waiter, readingCapacity, writingCapacity);
141}
142
143//------------------------------------------------------------------------------
144
145inline const ::xplcommon::Failable& LocalAcceptor::getFailable() const
146{
147 return socket;
148}
149
150//------------------------------------------------------------------------------
151
152inline ::xplcommon::Failable& LocalAcceptor::getFailable()
153{
154 return socket;
155}
156
157//------------------------------------------------------------------------------
158
159} /* namespace xplcommon::win32 */ } /* namespace xplcommon */
160
161//------------------------------------------------------------------------------
162#endif // XPLCOMMON_WIN32_LOCALACCEPTOR_H
163
164// Local Variables:
165// mode: C++
166// c-basic-offset: 4
167// indent-tabs-mode: nil
168// End:
Note: See TracBrowser for help on using the repository browser.