-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerClient.cs
54 lines (48 loc) · 1.72 KB
/
ServerClient.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
using Jonathon594.SimpleTCP.Data;
using System.Net;
using System.Net.Sockets;
namespace Jonathon594.SimpleTCP
{
/// <summary>
/// A class representing a lower level conntion to a <see cref="TcpClient"/>.
/// Can be used as an identifier for connected clients. See <see cref="Server.SendPacket(Packet, ServerClient)"/>
/// </summary>
public class ServerClient
{
private TcpClient client;
internal ServerClient(TcpClient client)
{
this.client = client;
}
/// <summary>
/// Checks to see whether the client is actually connected to a remote host.
/// </summary>
/// <returns>True when connected. False when not.</returns>
public bool isConnected()
{
return client.Connected;
}
/// <summary>
/// Gets the <see cref="EndPoint"/> of the connected <see cref="ServerClient"/>.
/// </summary>
/// <returns>The <see cref="EndPoint"/> of the connected <see cref="ServerClient"/>.</returns>
public EndPoint GetIPEndPoint()
{
return client.Client.RemoteEndPoint;
}
internal ByteBuffer readBuffer(int size)
{
NetworkStream stream = client.GetStream();
byte[] bytes = new byte[size];
int length = stream.Read(bytes, 0, bytes.Length);
return new ByteBuffer(bytes, length);
}
internal void SendPacket(Packet packet)
{
ByteBuffer buffer = new ByteBuffer();
buffer.WriteByte(NetworkManager.GetPacketIndex(packet.GetType()));
packet.Encode(buffer);
client.GetStream().Write(buffer.GetBytes(), 0, buffer.GetByteCount());
}
}
}