[32] | 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 "Acceptor.h"
|
---|
| 34 |
|
---|
| 35 | //------------------------------------------------------------------------------
|
---|
| 36 |
|
---|
| 37 | namespace hu { namespace varadiistvan { namespace scpl { namespace io {
|
---|
| 38 |
|
---|
| 39 | //------------------------------------------------------------------------------
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * Socket acceptor template for a certain kind of client sockets.
|
---|
| 43 | */
|
---|
| 44 | template <class Socket>
|
---|
| 45 | class SocketAcceptor : public Acceptor
|
---|
| 46 | {
|
---|
| 47 | protected:
|
---|
| 48 | /**
|
---|
| 49 | * Construct the acceptor.
|
---|
| 50 | */
|
---|
| 51 | SocketAcceptor(ServerSocket* socket);
|
---|
| 52 |
|
---|
| 53 | public:
|
---|
| 54 | /**
|
---|
| 55 | * Get the socket accepted last. If no socket was accepted,
|
---|
| 56 | * return 0. The socket's waiter will be the given one
|
---|
| 57 | * (which can be 0).
|
---|
| 58 | */
|
---|
| 59 | Socket* getSocket(Waiter* waiter,
|
---|
| 60 | size_t readingCapacity = Socket::DEFAULT_CAPACITY,
|
---|
| 61 | size_t writingCapacity = Socket::DEFAULT_CAPACITY);
|
---|
| 62 | /**
|
---|
| 63 | * Get the socket accepted last. If no socket was accepted,
|
---|
| 64 | * return 0. The socket's waiter will be the same as that of
|
---|
| 65 | * the associated server socket.
|
---|
| 66 | */
|
---|
| 67 | Socket* getSocket(size_t readingCapacity = Socket::DEFAULT_CAPACITY,
|
---|
| 68 | size_t writingCapacity = Socket::DEFAULT_CAPACITY);
|
---|
| 69 |
|
---|
| 70 | /**
|
---|
| 71 | * Get the last accepted socket as a buffered stream with the given waiter,
|
---|
| 72 | * if any.
|
---|
| 73 | */
|
---|
| 74 | BufferedStream* getAcceptedBufferedStream(Waiter* waiter = nullptr) override;
|
---|
| 75 | };
|
---|
| 76 |
|
---|
| 77 | //------------------------------------------------------------------------------
|
---|
| 78 | // Template definitions
|
---|
| 79 | //------------------------------------------------------------------------------
|
---|
| 80 |
|
---|
| 81 | template <class Socket>
|
---|
| 82 | inline SocketAcceptor<Socket>::SocketAcceptor(ServerSocket* socket) :
|
---|
| 83 | Acceptor(socket)
|
---|
| 84 | {
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | //------------------------------------------------------------------------------
|
---|
| 88 |
|
---|
| 89 | template <class Socket>
|
---|
| 90 | inline Socket*
|
---|
| 91 | SocketAcceptor<Socket>::getSocket(Waiter* waiter,
|
---|
| 92 | size_t readingCapacity,
|
---|
| 93 | size_t writingCapacity)
|
---|
| 94 | {
|
---|
| 95 | if (acceptedFD<0) return 0;
|
---|
| 96 |
|
---|
| 97 | Socket* socket = new Socket(acceptedFD, waiter,
|
---|
| 98 | readingCapacity, writingCapacity);
|
---|
| 99 | connectionProcessed();
|
---|
| 100 | return socket;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | //------------------------------------------------------------------------------
|
---|
| 104 |
|
---|
| 105 | template <class Socket>
|
---|
| 106 | inline Socket*
|
---|
| 107 | SocketAcceptor<Socket>::getSocket(size_t readingCapacity,
|
---|
| 108 | size_t writingCapacity)
|
---|
| 109 | {
|
---|
| 110 | return getSocket(socket.getWaiter(), readingCapacity, writingCapacity);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | //------------------------------------------------------------------------------
|
---|
| 114 |
|
---|
| 115 | template <class Socket>
|
---|
| 116 | BufferedStream* SocketAcceptor<Socket>::
|
---|
| 117 | getAcceptedBufferedStream(Waiter* waiter)
|
---|
| 118 | {
|
---|
| 119 | return (waiter==nullptr) ? getSocket() : getSocket(waiter);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | //------------------------------------------------------------------------------
|
---|
| 123 |
|
---|
| 124 | } /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
|
---|
| 125 |
|
---|
| 126 | //------------------------------------------------------------------------------
|
---|
| 127 |
|
---|
| 128 | // Local Variables:
|
---|
| 129 | // mode: C++
|
---|
| 130 | // c-basic-offset: 4
|
---|
| 131 | // indent-tabs-mode: nil
|
---|
| 132 | // End:
|
---|