This repository has been archived by the owner on Nov 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
NetworkHandler.cs
108 lines (84 loc) · 3.29 KB
/
NetworkHandler.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
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using MineLib.Network.IO;
namespace MineLib.Network
{
public sealed partial class NetworkHandler : INetworkHandler
{
// -- Debugging
public List<IPacket> PacketsReceived { get; set; }
public List<IPacket> PacketsSended { get; set; }
// -- Debugging.
#region Properties
public NetworkMode NetworkMode { get { return _minecraft.Mode; } }
public bool DebugPackets { get; set; }
public bool Connected { get { return _baseSock.Connected; } }
public bool Crashed { get; private set; }
#endregion
private readonly IMinecraftClient _minecraft;
private Socket _baseSock;
private IMinecraftStream _stream;
public NetworkHandler(IMinecraftClient client)
{
_minecraft = client;
PacketsReceived = new List<IPacket>();
PacketsSended = new List<IPacket>();
}
/// <summary>
/// Start NetworkHandler.
/// </summary>
public void Start(bool debugPackets = true)
{
DebugPackets = debugPackets;
// -- Connect to server.
switch (NetworkMode)
{
case NetworkMode.Modern:
_baseSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_baseSock.BeginConnect(_minecraft.ServerHost, _minecraft.ServerPort, ConnectedModern, null);
break;
case NetworkMode.Classic:
_baseSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_baseSock.BeginConnect(_minecraft.ServerHost, _minecraft.ServerPort, ConnectedClassic, null);
break;
case NetworkMode.PocketEdition:
_baseSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Udp);
_baseSock.BeginConnect(_minecraft.ServerHost, _minecraft.ServerPort, ConnectedPocketEdition, null);
break;
}
}
public IAsyncResult BeginSendPacket(IPacket packet, AsyncCallback asyncCallback, object state)
{
if (!Connected)
throw new Exception("Connection error");
if (DebugPackets)
PacketsSended.Add(packet);
IAsyncResult result = BeginSend(packet, asyncCallback, state);
EndSend(result);
return result;
}
public IAsyncResult BeginSend(IPacket packet, AsyncCallback asyncCallback, object state)
{
return _stream.BeginWrite(packet, asyncCallback, state);
}
public void EndSend(IAsyncResult asyncResult)
{
_stream.EndWrite(asyncResult);
}
/// <summary>
/// Dispose NetworkHandler.
/// </summary>
public void Dispose()
{
if (_baseSock != null)
_baseSock.Close();
if (_stream != null)
_stream.Dispose();
if (PacketsReceived != null)
PacketsReceived.Clear();
if (PacketsSended != null)
PacketsSended.Clear();
}
}
}