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