forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInnerNetworkClient.cs
More file actions
169 lines (146 loc) · 5.88 KB
/
InnerNetworkClient.cs
File metadata and controls
169 lines (146 loc) · 5.88 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Net.Sockets;
using log4net;
using L2dotNET.Utility;
namespace L2dotNET.Network
{
/// <summary>
/// Delegate for packet handling
/// </summary>
/// <param name="packet">Incoming packet</param>
public delegate void PacketHandleDelegate(Packet packet);
/// <summary>
/// Represents inner network client (remote) connection class.
/// </summary>
public class InnerNetworkClient : NetworkClient
{
private static readonly ILog Log = LogManager.GetLogger(typeof(InnerNetworkClient));
/// <summary>
/// Remote service settings.
/// </summary>
public RemoteServiceSettings RemoteServiceSettings;
/// <summary>
/// Fires when connection is lost or broken.
/// </summary>
public override event OnDisconnectedEventHandler OnDisconnected;
/// <summary>
/// Packet handling method.
/// </summary>
public PacketHandleDelegate HandleDelegate;
/// <summary>
/// Initializes new instance of <see cref="InnerNetworkClient"/> object.
/// </summary>
public InnerNetworkClient() { }
/// <summary>
/// Initializes new instance of <see cref="InnerNetworkClient"/> object.
/// </summary>
/// <param name="serviceId">Remote service unique id.</param>
/// <param name="serviceType">Remote service type.</param>
/// <param name="socket"><see cref="Socket"/> used by connection.</param>
/// <param name="handleDelegate">Service Handle Delegate, if null packet will not be handled</param>
public InnerNetworkClient(byte serviceId, ServiceType serviceType, Socket socket, PacketHandleDelegate handleDelegate) : base(socket)
{
ServiceId = serviceId;
ServiceType = serviceType;
HandleDelegate = handleDelegate;
}
/// <summary>
/// Creates new instance of <see cref="InnerNetworkClient"/> object.
/// </summary>
/// <param name="serviceId">Service unique id.</param>
/// <param name="serviceType">Service type.</param>
/// <param name="socket">Service <see cref="Socket"/> object.</param>
public InnerNetworkClient(byte serviceId, ServiceType serviceType, Socket socket) : this(serviceId, serviceType, socket, null) { }
/// <summary>
/// Handles incoming packet.
/// </summary>
/// <param name="packet">Incoming packet.</param>
protected override void Handle(Packet packet)
{
if (HandleDelegate == null)
Log.Info("Skipping handling");
else
HandleDelegate(packet);
}
/// <summary>
/// Begins receive from connection socket.
/// </summary>
public override void BeginReceive()
{
MSocket.BeginReceive(MReceiveBuffer, 0, 4, 0, MReceiveCallback, null);
}
/// <summary>
/// Receive <see cref="AsyncCallback"/> method.
/// </summary>
/// <exception cref="InvalidOperationException" />
protected override unsafe void ReceiveCallback(IAsyncResult ar)
{
try
{
MReceivedLength += MSocket.EndReceive(ar);
fixed (byte* buf = MReceiveBuffer)
{
if (!MHeaderReceived) //get packet capacity
{
L2Buffer.Extend(ref MReceiveBuffer, 0, *(int*)buf - sizeof(int));
MReceivedLength = 0;
MHeaderReceived = true;
}
if (MReceivedLength == MReceiveBuffer.Length) // all data received
{
Handle(new Packet(2, MReceiveBuffer));
MReceivedLength = 0;
MReceiveBuffer = MDefaultBuffer;
MHeaderReceived = false;
MSocket.BeginReceive(MReceiveBuffer, 0, 4, 0, ReceiveCallback, null);
}
else
{
if (MReceivedLength < MReceiveBuffer.Length) // not all data received
MSocket.BeginReceive(MReceiveBuffer, MReceivedLength, MReceiveBuffer.Length - MReceivedLength, 0, MReceiveCallback, null);
else
throw new InvalidOperationException();
}
}
}
catch (SocketException se)
{
Logger.WriteLine(Source.InnerNetwork, "{0} \r\nError code: {1}", se.ToString(), se.ErrorCode);
CloseConnection();
OnDisconnected?.Invoke(se.ErrorCode, this, ConnectionId);
}
catch (Exception e)
{
Logger.Exception(e);
CloseConnection();
OnDisconnected?.Invoke(-1, this, ConnectionId);
}
}
/// <summary>
/// Sends <see cref="Packet"/> to remote side.
/// </summary>
/// <param name="p"><see cref="Packet"/> to send.</param>
public virtual void Send(Packet p)
{
p.Prepare(sizeof(int));
SendData(p.GetBuffer());
}
/// <summary>
/// Creates packet from received buffer.
/// </summary>
/// <param name="buffer">Received buffer.</param>
/// <param name="length">Received buffer length.</param>
public override void ReceiveData(byte[] buffer, int length)
{
Handle(new Packet(sizeof(int), buffer));
}
/// <summary>
/// Gets or sets connected service unique id.
/// </summary>
public byte ServiceId { get; set; }
/// <summary>
/// Gets or sets connected <see cref="ServiceType"/>.
/// </summary>
public ServiceType ServiceType { get; set; }
}
}