-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWebSocketClient.cs
More file actions
147 lines (128 loc) · 4.4 KB
/
WebSocketClient.cs
File metadata and controls
147 lines (128 loc) · 4.4 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Filename: WebSocketClient.cs
* Author: Alessio Carello
* Last Update: 20/10/2018 21.41.38
*/
namespace WebSocketServer
{
using global::WebSocketServer.EventArgs;
using System;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// Defines the <see cref="WebSocketClient" />
/// </summary>
public class WebSocketClient : IDisposable
{
/// <summary>
/// Defines the m_client
/// </summary>
private TcpClient m_client;
/// <summary>
/// Defines the Stream
/// </summary>
public NetworkStream Stream;
/// <summary>
/// Gets the Id
/// </summary>
public Guid Id { get; private set; }
/// <summary>
/// Defines the Handshake
/// </summary>
public event HandshakeHandler Handshake;
/// <summary>
/// The HandshakeHandler
/// </summary>
/// <param name="sender">The sender<see cref="WebSocketClient"/></param>
/// <param name="e">The e<see cref="WebSocketHandshakeEventArgs"/></param>
public delegate void HandshakeHandler(WebSocketClient sender, WebSocketHandshakeEventArgs e);
/// <summary>
/// Defines the Message
/// </summary>
public event MessageHandler Message;
/// <summary>
/// The MessageHandler
/// </summary>
/// <param name="sender">The sender<see cref="WebSocketClient"/></param>
/// <param name="e">The e<see cref="WebSocketDataEventArgs"/></param>
public delegate void MessageHandler(WebSocketClient sender, WebSocketDataEventArgs e);
/// <summary>
/// Defines the OnStream
/// </summary>
public EventHandler OnStream;
/// <summary>
/// Gets the RemoteEndPoint
/// </summary>
public string RemoteEndPoint
{
get
{
return m_client.Client.RemoteEndPoint.ToString();
}
}
/// <summary>
/// Initializes a new instance of the <see cref="WebSocketClient"/> class.
/// </summary>
/// <param name="client">The client<see cref="TcpClient"/></param>
public WebSocketClient(TcpClient client)
{
Id = Guid.NewGuid();
m_client = client;
}
/// <summary>
/// Defines the m_hasClientAlreadyHandshake
/// </summary>
private bool m_hasClientAlreadyHandshake;
/// <summary>
/// The StartListen
/// </summary>
/// <returns>The <see cref="Task"/></returns>
public async Task StartListen()
{
Stream = m_client.GetStream();
while (m_client.Connected)
{
while (m_client.Available < 3 && m_client.Connected)
{
/* wait for enough bytes to be available (if client is still connected)*/
Thread.Sleep(50);
}
byte[] bytes = new byte[m_client.Available];
Stream.Read(bytes, 0, m_client.Available);
while (m_client.Available > 0)
{
var offset = bytes.Length;
var tmpBytes = new byte[bytes.Length + m_client.Available];
Array.Copy(bytes, 0, tmpBytes, 0, bytes.Length);
Stream.Read(tmpBytes, bytes.Length, m_client.Available);
bytes = tmpBytes;
}
//translate bytes of request to string
if (!m_hasClientAlreadyHandshake)
{
var data = Encoding.UTF8.GetString(bytes);
if (new Regex("^GET").IsMatch(data))
{
m_hasClientAlreadyHandshake = true;
Handshake?.Invoke(this, new WebSocketHandshakeEventArgs(data));
}
}
else
{
Message?.Invoke(this, new WebSocketDataEventArgs(bytes));
}
}
}
/// <summary>
/// The Dispose
/// </summary>
public void Dispose()
{
if (Stream != null) { Stream.Dispose(); }
if (m_client != null) { m_client.Dispose(); }
}
}
}