[30] | 1 | // Copyright (c) 2022 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 | #pragma once
|
---|
| 30 |
|
---|
| 31 | //------------------------------------------------------------------------------
|
---|
| 32 |
|
---|
| 33 | #include "../BufferedStreamAcceptor.h"
|
---|
| 34 |
|
---|
| 35 | #include "Completer.h"
|
---|
| 36 |
|
---|
| 37 | #include "TCPServerSocketBase.h"
|
---|
| 38 | #include "TCPSocket.h"
|
---|
| 39 |
|
---|
| 40 | //------------------------------------------------------------------------------
|
---|
| 41 |
|
---|
| 42 | namespace hu { namespace varadiistvan { namespace scpl { namespace io {
|
---|
| 43 |
|
---|
| 44 | //------------------------------------------------------------------------------
|
---|
| 45 |
|
---|
| 46 | class TCPServerSocket;
|
---|
| 47 |
|
---|
| 48 | //------------------------------------------------------------------------------
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
| 51 | * An acceptor that provides a TCPSocket for each accepted connection
|
---|
| 52 | */
|
---|
| 53 | class TCPAcceptor : public Completer,
|
---|
| 54 | public BufferedStreamAcceptor
|
---|
| 55 | {
|
---|
| 56 | private:
|
---|
| 57 | /**
|
---|
| 58 | * The accepted socket.
|
---|
| 59 | */
|
---|
| 60 | HANDLE acceptedSocket;
|
---|
| 61 |
|
---|
| 62 | private:
|
---|
| 63 | /**
|
---|
| 64 | * Construct the acceptor.
|
---|
| 65 | */
|
---|
| 66 | TCPAcceptor(TCPServerSocketBase* socket);
|
---|
| 67 |
|
---|
| 68 | /**
|
---|
| 69 | * Get the socket.
|
---|
| 70 | *
|
---|
| 71 | * It returns the overlappable from the Completer part, knowing
|
---|
| 72 | * that it must be a TCPServerSocket.
|
---|
| 73 | */
|
---|
| 74 | TCPServerSocketBase& getServerSocket() const;
|
---|
| 75 |
|
---|
| 76 | public:
|
---|
| 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 TCP socket accepted last. If no socket was accepted,
|
---|
| 93 | * return 0. The TCP socket's waiter will be the given one
|
---|
| 94 | * (which can be 0).
|
---|
| 95 | */
|
---|
| 96 | TCPSocket* getSocket(Waiter* waiter,
|
---|
| 97 | size_t readingCapacity = TCPSocket::DEFAULT_CAPACITY,
|
---|
| 98 | size_t writingCapacity = TCPSocket::DEFAULT_CAPACITY);
|
---|
| 99 | /**
|
---|
| 100 | * Get the TCP socket accepted last. If no socket was accepted,
|
---|
| 101 | * return 0. The TCP socket's waiter will be the same as that of
|
---|
| 102 | * the associated server socket.
|
---|
| 103 | */
|
---|
| 104 | TCPSocket* getSocket(size_t readingCapacity = TCPSocket::DEFAULT_CAPACITY,
|
---|
| 105 | size_t writingCapacity = TCPSocket::DEFAULT_CAPACITY);
|
---|
| 106 |
|
---|
| 107 | /**
|
---|
| 108 | * Get the last accepted socket as a buffered stream with the given waiter,
|
---|
| 109 | * if any.
|
---|
| 110 | */
|
---|
| 111 | BufferedStream* getAcceptedBufferedStream(Waiter* waiter = nullptr) override;
|
---|
| 112 |
|
---|
| 113 | protected:
|
---|
| 114 | /**
|
---|
| 115 | * Set the indicator of an accepted connection to true.
|
---|
| 116 | *
|
---|
| 117 | * @see Completer::handleWaitedResult
|
---|
| 118 | */
|
---|
| 119 | void handleWaitedResult(DWORD size) override;
|
---|
| 120 |
|
---|
| 121 | friend class TCPServerSocket;
|
---|
| 122 | };
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | //------------------------------------------------------------------------------
|
---|
| 126 | // Inline definitions
|
---|
| 127 | //------------------------------------------------------------------------------
|
---|
| 128 |
|
---|
| 129 | inline TCPAcceptor::TCPAcceptor(TCPServerSocketBase* socket) :
|
---|
| 130 | Completer(socket),
|
---|
| 131 | acceptedSocket(WSAInterface::get().getInvalidSocket())
|
---|
| 132 | {
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | //------------------------------------------------------------------------------
|
---|
| 136 |
|
---|
| 137 | inline TCPServerSocketBase& TCPAcceptor::getServerSocket() const
|
---|
| 138 | {
|
---|
| 139 | return static_cast<TCPServerSocketBase&>(overlappable);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | //------------------------------------------------------------------------------
|
---|
| 143 |
|
---|
| 144 | inline TCPSocket* TCPAcceptor::getSocket(Waiter* waiter,
|
---|
| 145 | size_t readingCapacity,
|
---|
| 146 | size_t writingCapacity)
|
---|
| 147 | {
|
---|
| 148 | if (acceptedSocket==WSAInterface::get().getInvalidSocket()) return 0;
|
---|
| 149 |
|
---|
| 150 | TCPSocket* socket = new TCPSocket(acceptedSocket, waiter,
|
---|
| 151 | readingCapacity, writingCapacity);
|
---|
| 152 | acceptedSocket = WSAInterface::get().getInvalidSocket();
|
---|
| 153 | return socket;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | //------------------------------------------------------------------------------
|
---|
| 157 |
|
---|
| 158 | inline TCPSocket* TCPAcceptor::getSocket(size_t readingCapacity,
|
---|
| 159 | size_t writingCapacity)
|
---|
| 160 | {
|
---|
| 161 | return getSocket(overlappable.getWaiter(), readingCapacity, writingCapacity);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | //------------------------------------------------------------------------------
|
---|
| 165 |
|
---|
| 166 | } /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
|
---|
| 167 |
|
---|
| 168 | //------------------------------------------------------------------------------
|
---|
| 169 |
|
---|
| 170 | // Local Variables:
|
---|
| 171 | // mode: C++
|
---|
| 172 | // c-basic-offset: 4
|
---|
| 173 | // indent-tabs-mode: nil
|
---|
| 174 | // End:
|
---|