winnar
git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1334 993157c7-ee19-0410-b2c4-bb4e9862e678
This commit is contained in:
@@ -1,10 +1,79 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Net;
|
||||
using System.IO;
|
||||
|
||||
namespace OpenRa.Game
|
||||
{
|
||||
class Network
|
||||
{
|
||||
public const int Port = 6543;
|
||||
const int netSyncInterval = 40 * 5;
|
||||
|
||||
UdpClient client = new UdpClient(Port);
|
||||
int nextSyncTime = 0;
|
||||
int currentFrame = 0;
|
||||
|
||||
Queue<Packet> incomingPackets = new Queue<Packet>();
|
||||
|
||||
public Network()
|
||||
{
|
||||
client.EnableBroadcast = true;
|
||||
|
||||
Thread receiveThread = new Thread(delegate()
|
||||
{
|
||||
for (; ; )
|
||||
{
|
||||
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
|
||||
byte[] data = client.Receive(ref sender);
|
||||
|
||||
Packet packet = Packet.FromReceivedData(sender, data);
|
||||
|
||||
lock (this)
|
||||
{
|
||||
if (currentFrame <= packet.Frame)
|
||||
incomingPackets.Enqueue(packet);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void Send(byte[] data)
|
||||
{
|
||||
IPEndPoint destination = new IPEndPoint(IPAddress.Broadcast, Port);
|
||||
using(MemoryStream ms = new MemoryStream())
|
||||
using (BinaryWriter writer = new BinaryWriter(ms))
|
||||
{
|
||||
writer.Write(currentFrame);
|
||||
writer.Write(data);
|
||||
writer.Flush();
|
||||
|
||||
byte[] toSend = ms.ToArray();
|
||||
|
||||
client.Send(toSend, toSend.Length);
|
||||
}
|
||||
}
|
||||
|
||||
public Queue<Packet> Tick()
|
||||
{
|
||||
Queue<Packet> toProcess = new Queue<Packet>();
|
||||
|
||||
if (Environment.TickCount > nextSyncTime)
|
||||
lock (this)
|
||||
{
|
||||
while (incomingPackets.Count > 0 && incomingPackets.Peek().Frame <= currentFrame)
|
||||
{
|
||||
Packet p = incomingPackets.Dequeue();
|
||||
if (p.Frame == currentFrame)
|
||||
toProcess.Enqueue(p);
|
||||
}
|
||||
++currentFrame;
|
||||
}
|
||||
|
||||
return toProcess;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
33
OpenRa.Game/Network/Packet.cs
Normal file
33
OpenRa.Game/Network/Packet.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Net;
|
||||
using System.IO;
|
||||
|
||||
namespace OpenRa.Game
|
||||
{
|
||||
class Packet : IComparable<Packet>
|
||||
{
|
||||
IPEndPoint address;
|
||||
int frame;
|
||||
byte[] data;
|
||||
|
||||
public int Frame { get { return frame; } }
|
||||
|
||||
Packet(IPEndPoint address, byte[] data)
|
||||
{
|
||||
this.address = address;
|
||||
|
||||
using (MemoryStream ms = new MemoryStream(data))
|
||||
using (BinaryReader reader = new BinaryReader(ms))
|
||||
{
|
||||
frame = reader.ReadInt32();
|
||||
this.data = reader.ReadBytes(data.Length - 4);
|
||||
}
|
||||
}
|
||||
|
||||
public static Packet FromReceivedData(IPEndPoint sender, byte[] data) { return new Packet(sender, data); }
|
||||
|
||||
public int CompareTo(Packet other) { return frame.CompareTo(other.frame); }
|
||||
}
|
||||
}
|
||||
@@ -44,6 +44,7 @@
|
||||
<Compile Include="Animation.cs" />
|
||||
<Compile Include="Building.cs" />
|
||||
<Compile Include="Game.cs" />
|
||||
<Compile Include="Network\Packet.cs" />
|
||||
<Compile Include="Sheet.cs" />
|
||||
<Compile Include="Harvester.cs" />
|
||||
<Compile Include="Log.cs" />
|
||||
|
||||
Reference in New Issue
Block a user