-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEnetClientConnection.cs
184 lines (155 loc) · 5.25 KB
/
EnetClientConnection.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using DarkRift;
using DarkRift.Client;
using ENet;
using UnityEngine;
using Event = ENet.Event;
using EventType = ENet.EventType;
class EnetClientConnection : NetworkClientConnection
{
public EnetClientConnection(string ip, int port)
{
Library.Initialize();
this.ip = ip;
this.port = port;
remoteEndPoints = new[] {new IPEndPoint(IPAddress.Parse(ip), port)};
}
private string ip;
private int port;
private Host client;
private Peer peer;
private Task clientTask;
private bool disposedValue = false;
private readonly IPEndPoint[] remoteEndPoints;
//Whether we're connected
public override ConnectionState ConnectionState
{
get { return connectionState; }
}
private ConnectionState connectionState;
//A list of endpoints we're connected to on the server
public override IEnumerable<IPEndPoint> RemoteEndPoints
{
get
{
return remoteEndPoints;
}
}
//Given a named endpoint this should return that
public override IPEndPoint GetRemoteEndPoint(string name)
{
throw new ArgumentException("Not a valid endpoint name!");
}
//Called when DarkRiftClient.Connect is called
public override void Connect()
{
client = new Host();
client.Create(null, 1);
Address address = new Address();
address.SetHost(ip);
address.Port = (ushort) port;
peer = client.Connect(address, 200);
}
//Sends a message reliably...
public override bool SendMessageReliable(MessageBuffer message)
{
byte[] data = new byte[message.Count];
Array.Copy(message.Buffer, message.Offset,data,0, message.Count);
bool r = SendReliable(data, 1, peer);
client.Flush();
message.Dispose();
return r;
}
//...Sends a message unreliably!
public override bool SendMessageUnreliable(MessageBuffer message)
{
byte[] data = new byte[message.Count];
Array.Copy(message.Buffer, message.Offset, data, 0, message.Count);
bool r = SendUnreliable(data, 2, peer);
client.Flush();
message.Dispose();
return r;
}
//Called when the server wants to disconnect the client
public override bool Disconnect()
{
peer.DisconnectNow(0);
client.Dispose();
return true;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposedValue)
{
return;
}
if (disposing)
{
Disconnect();
}
disposedValue = true;
}
//We should call HandleMessageReceived(MessageBuffer message, SendMode sendMode) when we get a new message from the client
//And HandleDisconnection(...) if the client disconnects
private bool SendReliable(byte[] data, byte channelID, Peer peer)
{
Packet packet = default(Packet);
packet.Create(data, data.Length, PacketFlags.Reliable | PacketFlags.NoAllocate); // Reliable Sequenced
return peer.Send(channelID, ref packet);
}
private bool SendUnreliable(byte[] data, byte channelID, Peer peer)
{
Packet packet = default(Packet);
packet.Create(data, data.Length, PacketFlags.None | PacketFlags.NoAllocate); // Unreliable Sequenced
return peer.Send(channelID, ref packet);
}
private void HandleEnetMessageReceived(Event netEvent, SendMode mode)
{
MessageBuffer message = MessageBuffer.Create(netEvent.Packet.Length);
netEvent.Packet.CopyTo(message.Buffer);
message.Offset = 0;
message.Count = netEvent.Packet.Length;
HandleMessageReceived(message, mode);
message.Dispose();
}
public void PerformUpdate()
{
Event netEvent;
client.Service(0,out netEvent);
//Debug.Log("client: " + netEvent.Type);
switch (netEvent.Type)
{
case EventType.None:
break;
case EventType.Connect:
Console.WriteLine("Client connected to server - ID: " + peer.ID);
connectionState = ConnectionState.Connected;
break;
case EventType.Disconnect:
Console.WriteLine("Client disconnected from server");
connectionState = ConnectionState.Disconnected;
HandleDisconnection(new ArgumentException("Enet disconnected"));
break;
case EventType.Timeout:
Console.WriteLine("Client connection timeout");
break;
case EventType.Receive:
Console.WriteLine("Packet received from server - Channel ID: " + netEvent.ChannelID +
", Data length: " + netEvent.Packet.Length);
if (netEvent.ChannelID == 1)
{
HandleEnetMessageReceived(netEvent, SendMode.Reliable);
}
else if (netEvent.ChannelID == 2)
{
HandleEnetMessageReceived(netEvent, SendMode.Unreliable);
}
netEvent.Packet.Dispose();
break;
}
}
}