DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
NetworkStream.cpp
Go to the documentation of this file.
1#include "pch.h"
5
6namespace DotNetDupe {
7 namespace System {
8 namespace Net {
9 namespace Sockets {
10
12 : m_pSocket(socket), m_bOwnsSocket(false), m_bDisposed(false) {
14 if (socket.IsNull()) {
15 throw ArgumentNullException("Socket cannot be null.");
16 }
17 }
18
19 NetworkStream::NetworkStream(const SmartPointer<Socket>& socket, bool bOwnsSocket)
20 : m_pSocket(socket), m_bOwnsSocket(bOwnsSocket), m_bDisposed(false) {
22 if (socket.IsNull()) {
23 throw ArgumentNullException("Socket cannot be null.");
24 }
25 }
26
31
33 return !m_bDisposed;
34 }
35
37 return false;
38 }
39
41 return !m_bDisposed;
42 }
43
45 throw IO::IOException("NetworkStream does not support seeking.");
46 }
47
49 throw IO::IOException("NetworkStream does not support seeking.");
50 }
51
52 void NetworkStream::SetPosition(long value) {
53 throw IO::IOException("NetworkStream does not support seeking.");
54 }
55
58 }
59
60 int NetworkStream::Read(char* buffer, int offset, int count) {
62 if (m_bDisposed) throw IO::IOException("Stream is disposed.");
63
65 return m_pSocket->Receive(buffer, offset, count);
66 }
67
68 long NetworkStream::Seek(long offset, int origin) {
69 throw IO::IOException("NetworkStream does not support seeking.");
70 }
71
72 void NetworkStream::SetLength(long value) {
73 throw IO::IOException("NetworkStream does not support seeking.");
74 }
75
76 void NetworkStream::Write(const char* buffer, int offset, int count) {
78 if (m_bDisposed) throw IO::IOException("Stream is disposed.");
79
81 m_pSocket->Send(buffer, offset, count);
82 }
83
86 if (!m_bDisposed) {
87 m_bDisposed = true;
88 if (m_bOwnsSocket && !m_pSocket.IsNull()) {
89 m_pSocket->Close();
90 }
91 }
92 }
93
95 Dispose();
96 }
97
98 }
99 }
100 }
101}
Defines the exception thrown when a null reference is passed to a method that does not accept it.
The exception that is thrown when an I/O error occurs.
Provides the underlying stream of data for network access.
The exception that is thrown when an I/O error occurs.
Definition IOException.h:17
bool CanWrite() const override
Gets a value that indicates whether the NetworkStream supports writing.
~NetworkStream()
Destructor closing stream and owned socket.
void Write(const char *buffer, int offset, int count) override
Writes data to the NetworkStream.
void SetPosition(long value) override
Sets the current position of the stream. Not supported.
bool CanSeek() const override
Gets a value that indicates whether the stream supports seeking. Always returns false.
int Read(char *buffer, int offset, int count) override
Reads data from the NetworkStream and stores it in the buffer.
void Dispose() override
Releases the unmanaged resources used by the NetworkStream.
NetworkStream(const SmartPointer< Socket > &socket)
Creates a new instance of the NetworkStream class for the specified Socket.
void SetLength(long value) override
Sets the length of the stream. Not supported.
bool CanRead() const override
Gets a value that indicates whether the NetworkStream supports reading.
long GetPosition() const override
Gets the current position within the stream. Not supported.
void Flush() override
Flushes data from the stream.
void Close()
Closes the NetworkStream and releases all resources.
long GetLength() const override
Gets the length of the data available on the stream. Not supported.
long Seek(long offset, int origin) override
Sets the current position of the stream to the given value. Not supported.
A unified smart pointer that supports both unique and shared ownership semantics.
bool IsNull() const noexcept
Checks if the SmartPointer is null.