/
TCPClient.cs
115 lines (101 loc) · 3.81 KB
/
TCPClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace VoltRpc.Communication.TCP;
/// <summary>
/// A <see cref="Client" /> that uses TCP to communicate
/// </summary>
public sealed class TCPClient : Client
{
/// <summary>
/// Default receive timeout time
/// </summary>
public const int DefaultReceiveTimeout = 600000;
/// <summary>
/// Default send timeout time
/// </summary>
public const int DefaultSendTimeout = 600000;
/// <summary>
/// Default connection timeout time
/// </summary>
public const int DefaultConnectionTimeout = 7000;
private readonly TcpClient client;
private readonly int connectionTimeout;
private readonly IPEndPoint endPoint;
private Stream clientStream;
/// <summary>
/// Creates a new <see cref="TCPClient" /> instance
/// </summary>
/// <param name="endPoint">The <see cref="IPEndPoint" /> to connect to</param>
/// <param name="bufferSize">The size of the buffers</param>
/// <param name="connectionTimeout">The timeout time for connection</param>
/// <param name="receiveTimeout">The receive timeout</param>
/// <param name="sendTimeout">The send timeout</param>
public TCPClient(IPEndPoint endPoint, int bufferSize = DefaultBufferSize,
int connectionTimeout = DefaultConnectionTimeout,
int receiveTimeout = DefaultReceiveTimeout,
int sendTimeout = DefaultSendTimeout)
: base(bufferSize)
{
client = new TcpClient
{
ReceiveTimeout = receiveTimeout,
SendTimeout = sendTimeout
};
this.endPoint = endPoint;
this.connectionTimeout = connectionTimeout;
}
/// <summary>
/// Creates a new <see cref="TCPClient" /> instance
/// </summary>
/// <param name="endPoint">The <see cref="IPEndPoint" /> to connect to</param>
/// <param name="receiveTimeout">The receive timeout</param>
/// <param name="sendTimeout">The send timeout</param>
public TCPClient(IPEndPoint endPoint, int receiveTimeout, int sendTimeout)
: this(endPoint, DefaultBufferSize, DefaultConnectionTimeout, receiveTimeout, sendTimeout)
{
}
/// <summary>
/// Creates a new <see cref="TCPClient" /> instance
/// </summary>
/// <param name="endPoint">The <see cref="IPEndPoint" /> to connect to</param>
/// <param name="connectionTimeout">The timeout time for connection</param>
public TCPClient(IPEndPoint endPoint, int connectionTimeout)
: this(endPoint, DefaultBufferSize, connectionTimeout, DefaultReceiveTimeout)
{
}
/// <summary>
/// Creates a new <see cref="TCPClient" /> instance
/// </summary>
/// <param name="endPoint">The <see cref="IPEndPoint" /> to connect to</param>
public TCPClient(IPEndPoint endPoint)
: this(endPoint, DefaultBufferSize, DefaultConnectionTimeout, DefaultReceiveTimeout)
{
}
/// <inheritdoc />
/// <exception cref="TimeoutException">Thrown if a connection timeout occurs</exception>
/// <exception cref="ConnectionFailedException">Thrown if an unknown error occurs while connecting.</exception>
public override void Connect()
{
CheckDispose();
try
{
if (!client.ConnectAsync(endPoint.Address, endPoint.Port).Wait(connectionTimeout))
throw new TimeoutException("The TCP client failed to connect in time.");
}
catch (Exception ex)
{
throw new ConnectionFailedException("The TCP client failed to connect!", ex);
}
clientStream = client.GetStream();
Initialize(clientStream, clientStream);
}
/// <inheritdoc />
public override void Dispose()
{
base.Dispose();
clientStream?.Dispose();
client.Dispose();
}
}