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 "WSAInterface.h"
|
---|
34 |
|
---|
35 | #include "BufferedStream.h"
|
---|
36 | #include "SocketMixin.h"
|
---|
37 | #include "SocketEventFactory.h"
|
---|
38 |
|
---|
39 | //------------------------------------------------------------------------------
|
---|
40 |
|
---|
41 | namespace hu { namespace varadiistvan { namespace scpl { namespace io {
|
---|
42 |
|
---|
43 | //------------------------------------------------------------------------------
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * A socket that works on a TCP connection.
|
---|
47 | */
|
---|
48 | class TCPSocket : public SocketMixin<BufferedStream>
|
---|
49 | {
|
---|
50 | protected:
|
---|
51 | /**
|
---|
52 | * Construct the TCP socket.
|
---|
53 | */
|
---|
54 | TCPSocket(Waiter* waiter = 0, int protocol = 0,
|
---|
55 | size_t readingCapacity = DEFAULT_CAPACITY,
|
---|
56 | size_t writingCapacity = DEFAULT_CAPACITY);
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Construct the socket with the given parameters.
|
---|
60 | */
|
---|
61 | TCPSocket(HANDLE s, Waiter* waiter = 0,
|
---|
62 | size_t readingCapacity = DEFAULT_CAPACITY,
|
---|
63 | size_t writingCapacity = DEFAULT_CAPACITY);
|
---|
64 |
|
---|
65 | public:
|
---|
66 | /**
|
---|
67 | * Destroy the socket by closing the file descriptor
|
---|
68 | */
|
---|
69 | ~TCPSocket();
|
---|
70 |
|
---|
71 | protected:
|
---|
72 | /**
|
---|
73 | * Connect the client socket to the server.
|
---|
74 | */
|
---|
75 | bool connect(const struct sockaddr_in& sin);
|
---|
76 |
|
---|
77 | friend class TCPAcceptor;
|
---|
78 | friend class TCPConnector;
|
---|
79 | };
|
---|
80 |
|
---|
81 | //------------------------------------------------------------------------------
|
---|
82 | // Inline definitions
|
---|
83 | //------------------------------------------------------------------------------
|
---|
84 |
|
---|
85 | inline TCPSocket::TCPSocket(Waiter* waiter, int protocol,
|
---|
86 | size_t readingCapacity,
|
---|
87 | size_t writingCapacity) :
|
---|
88 | TCPSocket(WSAInterface::get().socket(AF_INET, SOCK_STREAM, protocol),
|
---|
89 | waiter, readingCapacity, writingCapacity)
|
---|
90 | {
|
---|
91 | }
|
---|
92 |
|
---|
93 | //------------------------------------------------------------------------------
|
---|
94 |
|
---|
95 | inline TCPSocket::TCPSocket(HANDLE s, Waiter* waiter,
|
---|
96 | size_t readingCapacity,
|
---|
97 | size_t writingCapacity) :
|
---|
98 | SocketMixin<BufferedStream>(std::make_unique<SocketEventFactory>(),
|
---|
99 | waiter, s,
|
---|
100 | readingCapacity, writingCapacity)
|
---|
101 | {
|
---|
102 | }
|
---|
103 |
|
---|
104 | //------------------------------------------------------------------------------
|
---|
105 |
|
---|
106 | inline TCPSocket::~TCPSocket()
|
---|
107 | {
|
---|
108 | if (!WSAInterface::get().isInvalidSocket(handle)) {
|
---|
109 | WSAInterface::get().closesocket(handle);
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | //------------------------------------------------------------------------------
|
---|
115 |
|
---|
116 | inline bool TCPSocket::connect(const struct sockaddr_in& sin)
|
---|
117 | {
|
---|
118 | auto a = WSAInterface::get().connect(handle,
|
---|
119 | reinterpret_cast<const struct sockaddr*>(&sin),
|
---|
120 | sizeof(sin));
|
---|
121 | if (a==SOCKET_ERROR) {
|
---|
122 | auto lastError = GetLastError();
|
---|
123 | if (lastError!=WSAEWOULDBLOCK && lastError!=WSAEINPROGRESS) {
|
---|
124 | setErrorCode(lastError);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | return a!=SOCKET_ERROR;
|
---|
129 | }
|
---|
130 |
|
---|
131 | //------------------------------------------------------------------------------
|
---|
132 |
|
---|
133 | } /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
|
---|
134 |
|
---|
135 | //------------------------------------------------------------------------------
|
---|
136 |
|
---|
137 | // Local Variables:
|
---|
138 | // mode: C++
|
---|
139 | // c-basic-offset: 4
|
---|
140 | // indent-tabs-mode: nil
|
---|
141 | // End:
|
---|