-
Notifications
You must be signed in to change notification settings - Fork 8
/
ParamsTcpClientBytes.cs
72 lines (63 loc) · 2.8 KB
/
ParamsTcpClientBytes.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
using PHS.Networking.Utilities;
using System;
using System.Linq;
using System.Text;
namespace Tcp.NET.Client.Models
{
public class ParamsTcpClientBytes : IParamsTcpClient
{
public string Host { get; protected set; }
public int Port { get; protected set; }
public byte[] EndOfLineBytes { get; protected set; }
public bool UsePingPong { get; protected set; }
public byte[] PingBytes { get; protected set; }
public byte[] PongBytes { get; protected set; }
public bool IsSSL { get; protected set; }
public bool OnlyEmitBytes { get; protected set; }
public byte[] Token { get; protected set; }
public bool UseDisconnectBytes { get; protected set; }
public byte[] DisconnectBytes { get; protected set; }
public ParamsTcpClientBytes(string host, int port, byte[] endOfLineBytes, byte[] token = null, bool isSSL = true, bool onlyEmitBytes = false, bool usePingPong = true, byte[] pingBytes = null, byte[] pongBytes = null, bool useDisconnectBytes = true, byte[] disconnectBytes = null)
{
if (string.IsNullOrWhiteSpace(host))
{
throw new ArgumentException("Host is not valid");
}
if (port <= 0)
{
throw new ArgumentException("Port is not valid");
}
if (endOfLineBytes == null || endOfLineBytes.Length <= 0 || Statics.ByteArrayEquals(endOfLineBytes, Array.Empty<byte>()))
{
throw new ArgumentException("End of Line Characters are not valid");
}
if (token != null && token.Where(x => x != 0).ToArray().Length <= 0)
{
throw new ArgumentException("Token is not valid");
}
if (usePingPong && (pingBytes == null || pingBytes.Length <= 0 || Statics.ByteArrayEquals(pingBytes, Array.Empty<byte>())))
{
pingBytes = Encoding.UTF8.GetBytes("ping");
}
if (usePingPong && (pongBytes == null || pongBytes.Length <= 0 || Statics.ByteArrayEquals(pongBytes, Array.Empty<byte>())))
{
pongBytes = Encoding.UTF8.GetBytes("pong");
}
Host = host;
Port = port;
EndOfLineBytes = endOfLineBytes;
UsePingPong = usePingPong;
PingBytes = pingBytes;
PongBytes = pongBytes;
IsSSL = isSSL;
OnlyEmitBytes = onlyEmitBytes;
UseDisconnectBytes = useDisconnectBytes;
DisconnectBytes = disconnectBytes;
Token = token;
if (UseDisconnectBytes && (DisconnectBytes == null || Statics.ByteArrayEquals(DisconnectBytes, Array.Empty<byte>())))
{
DisconnectBytes = new byte[] { 3 };
}
}
}
}