1//
2// SocketStream.cpp
3//
4// Library: Net
5// Package: Sockets
6// Module: SocketStream
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/SocketStream.h"
16#include "Poco/Net/StreamSocketImpl.h"
17#include "Poco/Exception.h"
18
19
20using Poco::BufferedBidirectionalStreamBuf;
21using Poco::InvalidArgumentException;
22
23
24namespace Poco {
25namespace Net {
26
27
28//
29// SocketStreamBuf
30//
31
32
33SocketStreamBuf::SocketStreamBuf(const Socket& socket):
34 BufferedBidirectionalStreamBuf(STREAM_BUFFER_SIZE, std::ios::in | std::ios::out),
35 _pImpl(dynamic_cast<StreamSocketImpl*>(socket.impl()))
36{
37 if (_pImpl)
38 _pImpl->duplicate();
39 else
40 throw InvalidArgumentException("Invalid or null SocketImpl passed to SocketStreamBuf");
41}
42
43
44SocketStreamBuf::~SocketStreamBuf()
45{
46 _pImpl->release();
47}
48
49
50int SocketStreamBuf::readFromDevice(char* buffer, std::streamsize length)
51{
52 return _pImpl->receiveBytes(buffer, (int) length);
53}
54
55
56int SocketStreamBuf::writeToDevice(const char* buffer, std::streamsize length)
57{
58 return _pImpl->sendBytes(buffer, (int) length);
59}
60
61
62//
63// SocketIOS
64//
65
66
67SocketIOS::SocketIOS(const Socket& socket):
68 _buf(socket)
69{
70 poco_ios_init(&_buf);
71}
72
73
74SocketIOS::~SocketIOS()
75{
76 try
77 {
78 _buf.sync();
79 }
80 catch (...)
81 {
82 }
83}
84
85
86SocketStreamBuf* SocketIOS::rdbuf()
87{
88 return &_buf;
89}
90
91
92void SocketIOS::close()
93{
94 _buf.sync();
95 _buf.socketImpl()->close();
96}
97
98
99StreamSocket SocketIOS::socket() const
100{
101 return StreamSocket(_buf.socketImpl());
102}
103
104
105//
106// SocketOutputStream
107//
108
109
110SocketOutputStream::SocketOutputStream(const Socket& socket):
111 SocketIOS(socket),
112 std::ostream(&_buf)
113{
114}
115
116
117SocketOutputStream::~SocketOutputStream()
118{
119}
120
121
122//
123// SocketInputStream
124//
125
126
127SocketInputStream::SocketInputStream(const Socket& socket):
128 SocketIOS(socket),
129 std::istream(&_buf)
130{
131}
132
133
134SocketInputStream::~SocketInputStream()
135{
136}
137
138
139//
140// SocketStream
141//
142
143
144SocketStream::SocketStream(const Socket& socket):
145 SocketIOS(socket),
146 std::iostream(&_buf)
147{
148}
149
150
151SocketStream::~SocketStream()
152{
153}
154
155
156} } // namespace Poco::Net
157