DotNetDupe
4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Toggle main menu visibility
Loading...
Searching...
No Matches
TcpClient.cpp
Go to the documentation of this file.
1
#include "
pch.h
"
2
#include "
System/Net/Sockets/TcpClient.h
"
3
#include "
System/Net/Sockets/SocketException.h
"
4
5
namespace
DotNetDupe
{
6
namespace
System
{
7
namespace
Net
{
8
namespace
Sockets
{
9
10
TcpClient::TcpClient
()
11
: m_pClientSocket(new
Socket
(
AddressFamily
::
InterNetwork
,
SocketType
::
Stream
,
ProtocolType
::
Tcp
), true) {
12
}
13
14
TcpClient::TcpClient
(
AddressFamily
family)
15
: m_pClientSocket(new
Socket
(family,
SocketType
::
Stream
,
ProtocolType
::
Tcp
), true) {
16
}
17
18
TcpClient::TcpClient
(
const
SmartPointer<Socket>
& socket)
19
: m_pClientSocket(socket) {
20
}
21
22
TcpClient::~TcpClient
() {
24
Close
();
25
}
26
27
TcpClient::TcpClient
(
TcpClient
&& other) noexcept
28
: m_pClientSocket(std::move(other.m_pClientSocket)),
29
m_pNetworkStream(std::move(other.m_pNetworkStream)) {
30
}
31
32
TcpClient
&
TcpClient::operator=
(
TcpClient
&& other)
noexcept
{
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()) {
45
m_pClientSocket.Attach(
new
Socket
(
AddressFamily::InterNetwork
,
SocketType::Stream
,
ProtocolType::Tcp
),
true
);
46
}
47
49
m_pClientSocket->
Connect
(ip, port);
50
}
51
52
SmartPointer<NetworkStream>
TcpClient::GetStream
() {
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
65
void
TcpClient::Close
() {
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
81
SmartPointer<Socket>
TcpClient::GetClient
()
const
{
82
return
m_pClientSocket;
83
}
84
85
void
TcpClient::SetClient
(
const
SmartPointer<Socket>
& socket) {
86
m_pClientSocket = socket;
87
m_pNetworkStream =
nullptr
;
88
}
89
90
}
91
}
92
}
93
}
SocketException.h
Exception thrown when a network socket error occurs per RFC 793 / RFC 768.
TcpClient.h
Provides client connections for TCP network services.
DotNetDupe::System::Net::Sockets::NetworkStream::NetworkStream
NetworkStream(const SmartPointer< Socket > &socket)
Creates a new instance of the NetworkStream class for the specified Socket.
Definition
NetworkStream.cpp:11
DotNetDupe::System::Net::Sockets::SocketException::SocketException
SocketException(int errorCode, const String &message)
Initializes a new instance of the SocketException class with the specified error code and message.
Definition
SocketException.cpp:8
DotNetDupe::System::Net::Sockets::Socket
Implements the Berkeley sockets interface.
Definition
Socket.h:69
DotNetDupe::System::Net::Sockets::Socket::Socket
Socket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
Initializes a new instance of the Socket class using the specified address family,...
Definition
Socket.cpp:113
DotNetDupe::System::Net::Sockets::TcpClient::Close
void Close()
Disposes this TcpClient instance and closes the underlying connection.
Definition
TcpClient.cpp:65
DotNetDupe::System::Net::Sockets::TcpClient::SetClient
void SetClient(const SmartPointer< Socket > &socket)
Sets the underlying Socket.
Definition
TcpClient.cpp:85
DotNetDupe::System::Net::Sockets::TcpClient::TcpClient
TcpClient()
Initializes a new instance of the TcpClient class with AddressFamily::InterNetwork.
Definition
TcpClient.cpp:10
DotNetDupe::System::Net::Sockets::TcpClient::GetStream
SmartPointer< NetworkStream > GetStream()
Returns the NetworkStream used to send and receive data.
Definition
TcpClient.cpp:52
DotNetDupe::System::Net::Sockets::TcpClient::GetClient
SmartPointer< Socket > GetClient() const
Gets the underlying Socket.
Definition
TcpClient.cpp:81
DotNetDupe::System::Net::Sockets::TcpClient::Connect
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
DotNetDupe::System::Net::Sockets::TcpClient::operator=
TcpClient & operator=(const TcpClient &)=delete
DotNetDupe::System::Net::Sockets::TcpClient::~TcpClient
~TcpClient()
Destructor closing connection resources.
Definition
TcpClient.cpp:22
DotNetDupe::System::Net::Sockets::TcpClient::Connected
bool Connected() const
Gets a value indicating whether the underlying Socket for a TcpClient is connected to a remote host.
Definition
TcpClient.cpp:77
DotNetDupe::System::SmartPointer
A unified smart pointer that supports both unique and shared ownership semantics.
Definition
SmartPointer.h:72
DotNetDupe::System::SmartPointer::IsNull
bool IsNull() const noexcept
Checks if the SmartPointer is null.
Definition
SmartPointer.h:393
DotNetDupe::System::String
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition
String.h:74
DotNetDupe::System::String::String
String()
Initializes a new instance of the String class to an empty string.
Definition
String.cpp:64
DotNetDupe::System::Net::Sockets
Definition
HttpClient.h:20
DotNetDupe::System::Net::Sockets::AddressFamily
AddressFamily
Specifies the addressing scheme that an instance of the Socket class can use.
Definition
Socket.h:20
DotNetDupe::System::Net::Sockets::AddressFamily::InterNetwork
@ InterNetwork
Definition
Socket.h:23
DotNetDupe::System::Net::Sockets::ProtocolType
ProtocolType
Specifies the protocols that the Socket class supports.
Definition
Socket.h:38
DotNetDupe::System::Net::Sockets::ProtocolType::Tcp
@ Tcp
Definition
Socket.h:40
DotNetDupe::System::Net::Sockets::SocketType
SocketType
Specifies the type of socket that an instance of the Socket class represents.
Definition
Socket.h:29
DotNetDupe::System::Net::Sockets::SocketType::Stream
@ Stream
Definition
Socket.h:31
DotNetDupe::System::Net
Definition
Dns.h:13
DotNetDupe::System
Definition
Action.h:11
DotNetDupe
Definition
IServiceCollection.h:7
pch.h
DotNetDupe
TcpClient.cpp
Generated by
1.18.0