1 | // |
2 | // StreamSocket.cpp |
3 | // |
4 | // Library: Net |
5 | // Package: Sockets |
6 | // Module: StreamSocket |
7 | // |
8 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/Net/StreamSocket.h" |
16 | #include "Poco/Net/StreamSocketImpl.h" |
17 | #include "Poco/FIFOBuffer.h" |
18 | #include "Poco/Mutex.h" |
19 | #include "Poco/Exception.h" |
20 | |
21 | |
22 | using Poco::InvalidArgumentException; |
23 | using Poco::Mutex; |
24 | using Poco::ScopedLock; |
25 | |
26 | |
27 | namespace Poco { |
28 | namespace Net { |
29 | |
30 | |
31 | StreamSocket::StreamSocket(): Socket(new StreamSocketImpl) |
32 | { |
33 | } |
34 | |
35 | |
36 | StreamSocket::StreamSocket(const SocketAddress& address): Socket(new StreamSocketImpl(address.family())) |
37 | { |
38 | connect(address); |
39 | } |
40 | |
41 | |
42 | StreamSocket::StreamSocket(SocketAddress::Family family): Socket(new StreamSocketImpl(family)) |
43 | { |
44 | } |
45 | |
46 | |
47 | StreamSocket::StreamSocket(const Socket& socket): Socket(socket) |
48 | { |
49 | if (!dynamic_cast<StreamSocketImpl*>(impl())) |
50 | throw InvalidArgumentException("Cannot assign incompatible socket" ); |
51 | } |
52 | |
53 | |
54 | StreamSocket::StreamSocket(SocketImpl* pImpl): Socket(pImpl) |
55 | { |
56 | if (!dynamic_cast<StreamSocketImpl*>(impl())) |
57 | throw InvalidArgumentException("Cannot assign incompatible socket" ); |
58 | } |
59 | |
60 | |
61 | StreamSocket::~StreamSocket() |
62 | { |
63 | } |
64 | |
65 | |
66 | StreamSocket& StreamSocket::operator = (const Socket& socket) |
67 | { |
68 | if (dynamic_cast<StreamSocketImpl*>(socket.impl())) |
69 | Socket::operator = (socket); |
70 | else |
71 | throw InvalidArgumentException("Cannot assign incompatible socket" ); |
72 | return *this; |
73 | } |
74 | |
75 | |
76 | void StreamSocket::connect(const SocketAddress& address) |
77 | { |
78 | impl()->connect(address); |
79 | } |
80 | |
81 | |
82 | void StreamSocket::connect(const SocketAddress& address, const Poco::Timespan& timeout) |
83 | { |
84 | impl()->connect(address, timeout); |
85 | } |
86 | |
87 | |
88 | void StreamSocket::connectNB(const SocketAddress& address) |
89 | { |
90 | impl()->connectNB(address); |
91 | } |
92 | |
93 | |
94 | void StreamSocket::shutdownReceive() |
95 | { |
96 | impl()->shutdownReceive(); |
97 | } |
98 | |
99 | |
100 | void StreamSocket::shutdownSend() |
101 | { |
102 | impl()->shutdownSend(); |
103 | } |
104 | |
105 | |
106 | void StreamSocket::shutdown() |
107 | { |
108 | impl()->shutdown(); |
109 | } |
110 | |
111 | |
112 | int StreamSocket::sendBytes(const void* buffer, int length, int flags) |
113 | { |
114 | return impl()->sendBytes(buffer, length, flags); |
115 | } |
116 | |
117 | |
118 | int StreamSocket::sendBytes(FIFOBuffer& fifoBuf) |
119 | { |
120 | ScopedLock<Mutex> l(fifoBuf.mutex()); |
121 | |
122 | int ret = impl()->sendBytes(fifoBuf.begin(), (int) fifoBuf.used()); |
123 | if (ret > 0) fifoBuf.drain(ret); |
124 | return ret; |
125 | } |
126 | |
127 | |
128 | int StreamSocket::receiveBytes(void* buffer, int length, int flags) |
129 | { |
130 | return impl()->receiveBytes(buffer, length, flags); |
131 | } |
132 | |
133 | |
134 | int StreamSocket::receiveBytes(FIFOBuffer& fifoBuf) |
135 | { |
136 | ScopedLock<Mutex> l(fifoBuf.mutex()); |
137 | |
138 | int ret = impl()->receiveBytes(fifoBuf.next(), (int)fifoBuf.available()); |
139 | if (ret > 0) fifoBuf.advance(ret); |
140 | return ret; |
141 | } |
142 | |
143 | |
144 | void StreamSocket::sendUrgent(unsigned char data) |
145 | { |
146 | impl()->sendUrgent(data); |
147 | } |
148 | |
149 | |
150 | } } // namespace Poco::Net |
151 | |