DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
TcpClient.cpp
Go to the documentation of this file.
1#include "pch.h"
4
5namespace DotNetDupe {
6 namespace System {
7 namespace Net {
8 namespace Sockets {
9
13
15 : m_pClientSocket(new Socket(family, SocketType::Stream, ProtocolType::Tcp), true) {
16 }
17
19 : m_pClientSocket(socket) {
20 }
21
26
28 : m_pClientSocket(std::move(other.m_pClientSocket)),
29 m_pNetworkStream(std::move(other.m_pNetworkStream)) {
30 }
31
34 if (this != &other) {
35 Close();
36 m_pClientSocket = std::move(other.m_pClientSocket);
37 m_pNetworkStream = std::move(other.m_pNetworkStream);
38 }
39 return *this;
40 }
41
42 void TcpClient::Connect(const String& ip, int port) {
44 if (m_pClientSocket.IsNull()) {
46 }
47
49 m_pClientSocket->Connect(ip, port);
50 }
51
54 if (m_pClientSocket.IsNull() || !m_pClientSocket->Connected()) {
55 throw SocketException(-1, String("TcpClient is not connected."));
56 }
57
59 if (m_pNetworkStream.IsNull()) {
60 m_pNetworkStream.Attach(new NetworkStream(m_pClientSocket, true), true);
61 }
62 return m_pNetworkStream;
63 }
64
67 if (!m_pNetworkStream.IsNull()) {
68 m_pNetworkStream->Close();
69 m_pNetworkStream = nullptr;
70 }
71 if (!m_pClientSocket.IsNull()) {
72 m_pClientSocket->Close();
73 m_pClientSocket = nullptr;
74 }
75 }
76
77 bool TcpClient::Connected() const {
78 return !m_pClientSocket.IsNull() && m_pClientSocket->Connected();
79 }
80
82 return m_pClientSocket;
83 }
84
86 m_pClientSocket = socket;
87 m_pNetworkStream = nullptr;
88 }
89
90 }
91 }
92 }
93}
Exception thrown when a network socket error occurs per RFC 793 / RFC 768.
Provides client connections for TCP network services.
NetworkStream(const SmartPointer< Socket > &socket)
Creates a new instance of the NetworkStream class for the specified Socket.
SocketException(int errorCode, const String &message)
Initializes a new instance of the SocketException class with the specified error code and message.
Implements the Berkeley sockets interface.
Definition Socket.h:69
Socket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
Initializes a new instance of the Socket class using the specified address family,...
Definition Socket.cpp:113
void Close()
Disposes this TcpClient instance and closes the underlying connection.
Definition TcpClient.cpp:65
void SetClient(const SmartPointer< Socket > &socket)
Sets the underlying Socket.
Definition TcpClient.cpp:85
TcpClient()
Initializes a new instance of the TcpClient class with AddressFamily::InterNetwork.
Definition TcpClient.cpp:10
SmartPointer< NetworkStream > GetStream()
Returns the NetworkStream used to send and receive data.
Definition TcpClient.cpp:52
SmartPointer< Socket > GetClient() const
Gets the underlying Socket.
Definition TcpClient.cpp:81
void Connect(const String &ip, int port)
Connects the client to a remote TCP host using the specified IP address and port number.
Definition TcpClient.cpp:42
TcpClient & operator=(const TcpClient &)=delete
~TcpClient()
Destructor closing connection resources.
Definition TcpClient.cpp:22
bool Connected() const
Gets a value indicating whether the underlying Socket for a TcpClient is connected to a remote host.
Definition TcpClient.cpp:77
A unified smart pointer that supports both unique and shared ownership semantics.
bool IsNull() const noexcept
Checks if the SmartPointer is null.
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition String.h:74
String()
Initializes a new instance of the String class to an empty string.
Definition String.cpp:64
AddressFamily
Specifies the addressing scheme that an instance of the Socket class can use.
Definition Socket.h:20
ProtocolType
Specifies the protocols that the Socket class supports.
Definition Socket.h:38
SocketType
Specifies the type of socket that an instance of the Socket class represents.
Definition Socket.h:29