-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
OrderManager.cs
executable file
·213 lines (173 loc) · 5.41 KB
/
OrderManager.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
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
#region Copyright & License Information
/*
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.FileFormats;
namespace OpenRA.Network
{
public class OrderManager : IDisposable
{
readonly SyncReport syncReport;
readonly FrameData frameData = new FrameData();
public Session LobbyInfo = new Session();
public Session.Client LocalClient { get { return LobbyInfo.ClientWithIndex(Connection.LocalClientId); } }
public World world;
public readonly string Host;
public readonly int Port;
public readonly string Password = "";
public string ServerError = "Server is not responding";
public bool AuthenticationFailed = false;
public int NetFrameNumber { get; private set; }
public int LocalFrameNumber;
public int FramesAhead = 0;
public int LastTickTime = Environment.TickCount;
public bool GameStarted { get { return NetFrameNumber != 0; } }
public IConnection Connection { get; private set; }
public readonly int SyncHeaderSize = 9;
List<Order> localOrders = new List<Order>();
public void StartGame()
{
if (GameStarted) return;
NetFrameNumber = 1;
for (var i = NetFrameNumber ; i <= FramesAhead ; i++)
Connection.Send(i, new List<byte[]>());
}
public OrderManager(string host, int port, string password, IConnection conn)
{
Host = host;
Port = port;
Password = password;
Connection = conn;
syncReport = new SyncReport(this);
}
public void IssueOrders(Order[] orders)
{
foreach (var order in orders)
IssueOrder(order);
}
public void IssueOrder(Order order)
{
localOrders.Add(order);
}
public void TickImmediate()
{
var immediateOrders = localOrders.Where( o => o.IsImmediate ).ToList();
if( immediateOrders.Count != 0 )
Connection.SendImmediate( immediateOrders.Select( o => o.Serialize() ).ToList() );
localOrders.RemoveAll( o => o.IsImmediate );
var immediatePackets = new List<Pair<int, byte[]>>();
Connection.Receive(
( clientId, packet ) =>
{
var frame = BitConverter.ToInt32(packet, 0);
if (packet.Length == 5 && packet[4] == 0xBF)
frameData.ClientQuit(clientId, frame);
else if (packet.Length >= 5 && packet[4] == 0x65)
CheckSync(packet);
else if (frame == 0)
immediatePackets.Add(Pair.New(clientId, packet));
else
frameData.AddFrameOrders(clientId, frame, packet);
} );
foreach (var p in immediatePackets)
foreach (var o in p.Second.ToOrderList(world))
UnitOrders.ProcessOrder(this, world, p.First, o);
}
Dictionary<int, byte[]> syncForFrame = new Dictionary<int, byte[]>();
void CheckSync(byte[] packet)
{
var frame = BitConverter.ToInt32(packet, 0);
byte[] existingSync;
if (syncForFrame.TryGetValue(frame, out existingSync))
{
if (packet.Length != existingSync.Length)
{
syncReport.DumpSyncReport(frame);
OutOfSync(frame);
}
else
{
for (int i = 0; i < packet.Length; i++)
{
if (packet[i] != existingSync[i])
{
syncReport.DumpSyncReport(frame);
if (i < SyncHeaderSize)
OutOfSync(frame, "Tick");
else
OutOfSync(frame, (i - SyncHeaderSize) / 4);
}
}
}
}
else
syncForFrame.Add(frame, packet);
}
void OutOfSync(int frame, int index)
{
var orders = frameData.OrdersForFrame(world, frame);
// Invalid index
if (index >= orders.Count())
OutOfSync(frame);
throw new InvalidOperationException("Out of sync in frame {0}.\n {1}\n Compare syncreport.log with other players.".F(frame, orders.ElementAt(index).Order.ToString()));
}
void OutOfSync(int frame)
{
throw new InvalidOperationException("Out of sync in frame {0}.\n Compare syncreport.log with other players.".F(frame));
}
void OutOfSync(int frame, string blame)
{
throw new InvalidOperationException("Out of sync in frame {0}: Blame {1}.\n Compare syncreport.log with other players.".F(frame, blame));
}
public bool IsReadyForNextFrame
{
get { return NetFrameNumber >= 1 && frameData.IsReadyForFrame(NetFrameNumber); }
}
static readonly IEnumerable<Session.Client> NoClients = new Session.Client[] {};
public IEnumerable<Session.Client> GetClientsNotReadyForNextFrame
{
get
{
return NetFrameNumber >= 1
? frameData.ClientsNotReadyForFrame(NetFrameNumber)
.Select(a => LobbyInfo.ClientWithIndex(a))
: NoClients;
}
}
public void Tick()
{
if (!IsReadyForNextFrame)
throw new InvalidOperationException();
Connection.Send(NetFrameNumber + FramesAhead, localOrders.Select(o => o.Serialize()).ToList());
localOrders.Clear();
var sync = new List<int>();
sync.Add(world.SyncHash());
foreach (var order in frameData.OrdersForFrame(world, NetFrameNumber))
{
UnitOrders.ProcessOrder(this, world, order.Client, order.Order);
sync.Add(world.SyncHash());
}
var ss = sync.SerializeSync();
Connection.SendSync(NetFrameNumber, ss);
syncReport.UpdateSyncReport();
++NetFrameNumber;
}
bool disposed;
public void Dispose()
{
if (disposed) return;
Connection.Dispose();
disposed = true;
GC.SuppressFinalize(this);
}
~OrderManager() { Dispose(); }
}
}