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