forked from ddevault/TrueCraft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiplayerClient.cs
More file actions
216 lines (186 loc) · 6.38 KB
/
MultiplayerClient.cs
File metadata and controls
216 lines (186 loc) · 6.38 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System;
using System.Net;
using System.Net.Sockets;
using System.Collections.Concurrent;
using TrueCraft.API.Networking;
using System.Threading;
using TrueCraft.Core.Networking;
using System.Linq;
using TrueCraft.Core.Networking.Packets;
using TrueCraft.Client.Events;
using TrueCraft.Core.Logic;
using TrueCraft.API.Entities;
using TrueCraft.API;
using System.ComponentModel;
namespace TrueCraft.Client
{
public delegate void PacketHandler(IPacket packet, MultiplayerClient client);
public class MultiplayerClient : IAABBEntity, INotifyPropertyChanged // TODO: Make IMultiplayerClient and so on
{
public event EventHandler<ChatMessageEventArgs> ChatMessage;
public event EventHandler<ChunkEventArgs> ChunkLoaded;
public event EventHandler<ChunkEventArgs> ChunkUnloaded;
public event PropertyChangedEventHandler PropertyChanged;
public ReadOnlyWorld World { get; private set; }
public PhysicsEngine Physics { get; set; }
public bool LoggedIn { get; internal set; }
private TcpClient Client { get; set; }
private IMinecraftStream Stream { get; set; }
private PacketReader PacketReader { get; set; }
private ConcurrentQueue<IPacket> PacketQueue { get; set; }
private Thread NetworkWorker { get; set; }
private readonly PacketHandler[] PacketHandlers;
public MultiplayerClient()
{
Client = new TcpClient();
PacketQueue = new ConcurrentQueue<IPacket>();
PacketReader = new PacketReader();
PacketReader.RegisterCorePackets();
NetworkWorker = new Thread(new ThreadStart(DoNetwork));
PacketHandlers = new PacketHandler[0x100];
Handlers.PacketHandlers.RegisterHandlers(this);
World = new ReadOnlyWorld();
var repo = new BlockRepository();
repo.DiscoverBlockProviders();
World.World.BlockRepository = repo;
Physics = new PhysicsEngine(World, repo);
}
public void RegisterPacketHandler(byte packetId, PacketHandler handler)
{
PacketHandlers[packetId] = handler;
}
public void Connect(IPEndPoint endPoint)
{
Client.BeginConnect(endPoint.Address, endPoint.Port, ConnectionComplete, null);
}
public void Disconnect()
{
NetworkWorker.Abort();
new DisconnectPacket("Disconnecting").WritePacket(Stream);
Stream.BaseStream.Flush();
Client.Close();
}
public void QueuePacket(IPacket packet)
{
PacketQueue.Enqueue(packet);
}
private void ConnectionComplete(IAsyncResult result)
{
Client.EndConnect(result);
Stream = new MinecraftStream(new BufferedStream(Client.GetStream()));
NetworkWorker.Start();
Physics.AddEntity(this);
QueuePacket(new HandshakePacket("TestUser")); // TODO: Get username from somewhere else
}
private void DoNetwork()
{
bool idle = true;
while (true)
{
IPacket packet;
DateTime limit = DateTime.UtcNow.AddMilliseconds(500);
while (Client.Available != 0 && DateTime.UtcNow < limit)
{
idle = false;
packet = PacketReader.ReadPacket(Stream, false);
if (PacketHandlers[packet.ID] != null)
PacketHandlers[packet.ID](packet, this);
}
limit = DateTime.UtcNow.AddMilliseconds(500);
while (!PacketQueue.IsEmpty && DateTime.UtcNow < limit)
{
idle = false;
while (!PacketQueue.TryDequeue(out packet)) { }
PacketReader.WritePacket(Stream, packet);
Stream.BaseStream.Flush();
}
if (idle)
Thread.Sleep(100);
}
}
protected internal void OnChatMessage(ChatMessageEventArgs e)
{
if (ChatMessage != null) ChatMessage(this, e);
}
protected internal void OnChunkLoaded(ChunkEventArgs e)
{
if (ChunkLoaded != null) ChunkLoaded(this, e);
}
protected internal void OnChunkUnloaded(ChunkEventArgs e)
{
if (ChunkUnloaded != null) ChunkUnloaded(this, e);
}
#region IAABBEntity implementation
public const double Width = 0.6;
public const double Height = 1.62;
public const double Depth = 0.6;
public void TerrainCollision(Vector3 collisionPoint, Vector3 collisionDirection)
{
// This space intentionally left blank
}
public BoundingBox BoundingBox
{
get
{
return new BoundingBox(Position, Position + Size);
}
}
public Size Size
{
get { return new Size(Width, Height, Depth); }
}
#endregion
#region IPhysicsEntity implementation
public bool BeginUpdate()
{
return true;
}
public void EndUpdate(Vector3 newPosition)
{
Position = newPosition;
}
public float Yaw { get; set; }
public float Pitch { get; set; }
internal Vector3 _Position;
public Vector3 Position
{
get
{
return _Position;
}
set
{
if (_Position != value)
{
QueuePacket(new PlayerPositionAndLookPacket(value.X, value.Y, value.Y + Height, value.Z, Yaw, Pitch, false));
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Position"));
}
_Position = value;
}
}
public Vector3 Velocity { get; set; }
public float AccelerationDueToGravity
{
get
{
return 0.08f;
}
}
public float Drag
{
get
{
return 0.02f;
}
}
public float TerminalVelocity
{
get
{
return 3.92f;
}
}
#endregion
}
}