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
22using Poco::InvalidArgumentException;
23using Poco::Mutex;
24using Poco::ScopedLock;
25
26
27namespace Poco {
28namespace Net {
29
30
31StreamSocket::StreamSocket(): Socket(new StreamSocketImpl)
32{
33}
34
35
36StreamSocket::StreamSocket(const SocketAddress& address): Socket(new StreamSocketImpl(address.family()))
37{
38 connect(address);
39}
40
41
42StreamSocket::StreamSocket(SocketAddress::Family family): Socket(new StreamSocketImpl(family))
43{
44}
45
46
47StreamSocket::StreamSocket(const Socket& socket): Socket(socket)
48{
49 if (!dynamic_cast<StreamSocketImpl*>(impl()))
50 throw InvalidArgumentException("Cannot assign incompatible socket");
51}
52
53
54StreamSocket::StreamSocket(SocketImpl* pImpl): Socket(pImpl)
55{
56 if (!dynamic_cast<StreamSocketImpl*>(impl()))
57 throw InvalidArgumentException("Cannot assign incompatible socket");
58}
59
60
61StreamSocket::~StreamSocket()
62{
63}
64
65
66StreamSocket& 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
76void StreamSocket::bind(const SocketAddress& address, bool reuseAddress)
77{
78 impl()->bind(address, reuseAddress);
79}
80
81
82void StreamSocket::connect(const SocketAddress& address)
83{
84 impl()->connect(address);
85}
86
87
88void StreamSocket::connect(const SocketAddress& address, const Poco::Timespan& timeout)
89{
90 impl()->connect(address, timeout);
91}
92
93
94void StreamSocket::connectNB(const SocketAddress& address)
95{
96 impl()->connectNB(address);
97}
98
99
100void StreamSocket::shutdownReceive()
101{
102 impl()->shutdownReceive();
103}
104
105
106void StreamSocket::shutdownSend()
107{
108 impl()->shutdownSend();
109}
110
111
112void StreamSocket::shutdown()
113{
114 impl()->shutdown();
115}
116
117
118int StreamSocket::sendBytes(const void* buffer, int length, int flags)
119{
120 return impl()->sendBytes(buffer, length, flags);
121}
122
123
124int StreamSocket::sendBytes(const SocketBufVec& buffers, int flags)
125{
126 return impl()->sendBytes(buffers, flags);
127}
128
129
130int StreamSocket::sendBytes(FIFOBuffer& fifoBuf)
131{
132 ScopedLock<Mutex> l(fifoBuf.mutex());
133
134 int ret = impl()->sendBytes(fifoBuf.begin(), (int) fifoBuf.used());
135 if (ret > 0) fifoBuf.drain(ret);
136 return ret;
137}
138
139
140int StreamSocket::receiveBytes(void* buffer, int length, int flags)
141{
142 return impl()->receiveBytes(buffer, length, flags);
143}
144
145
146int StreamSocket::receiveBytes(SocketBufVec& buffers, int flags)
147{
148 return impl()->receiveBytes(buffers, flags);
149}
150
151
152int StreamSocket::receiveBytes(Poco::Buffer<char>& buffer, int flags, const Poco::Timespan& timeout)
153{
154 return impl()->receiveBytes(buffer, flags, timeout);
155}
156
157
158int StreamSocket::receiveBytes(FIFOBuffer& fifoBuf)
159{
160 ScopedLock<Mutex> l(fifoBuf.mutex());
161
162 int ret = impl()->receiveBytes(fifoBuf.next(), (int)fifoBuf.available());
163 if (ret > 0) fifoBuf.advance(ret);
164 return ret;
165}
166
167
168void StreamSocket::sendUrgent(unsigned char data)
169{
170 impl()->sendUrgent(data);
171}
172
173
174} } // namespace Poco::Net
175