ugly hacked async connection

This commit is contained in:
Chris Forbes
2010-01-18 18:08:10 +13:00
parent b7c5b9e265
commit f6e70f74e3
4 changed files with 77 additions and 21 deletions

View File

@@ -210,6 +210,15 @@ namespace OpenRa
lineRenderer.Flush();
}
public void DrawDialog(string text)
{
var w = renderer.MeasureText(text).X + 120;
var h = 100;
var r = new Rectangle((Game.viewport.Width - w) / 2, (Game.viewport.Height - h) / 2, w, h);
DrawDialogBackground(r, optionsSprites, true);
DrawCentered(text, new int2(Game.viewport.Width / 2, Game.viewport.Height / 2 - 8), Color.White);
}
public void DrawLobby()
{
DrawDownloadBar();

View File

@@ -2,6 +2,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using OpenRa.Traits;
using OpenRa.Orders;
namespace OpenRa.Graphics
{
@@ -55,6 +56,23 @@ namespace OpenRa.Graphics
}
else
{
if (Game.orderManager.IsNetplay)
{
var nos = Game.orderManager.Sources.OfType<NetworkOrderSource>().First();
switch (nos.State)
{
case ConnectionState.Connecting:
Game.chrome.DrawDialog("Connecting to server...");
break;
case ConnectionState.NotConnected:
Game.chrome.DrawDialog("Connection failed.");
break;
case ConnectionState.Connected:
Game.chrome.DrawLobby();
break;
}
}
else
Game.chrome.DrawLobby();
}

View File

@@ -3,9 +3,17 @@ using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Threading;
using System;
namespace OpenRa.Orders
{
enum ConnectionState
{
NotConnected,
Connecting,
Connected,
}
class NetworkOrderSource : IOrderSource
{
TcpClient socket;
@@ -13,16 +21,29 @@ namespace OpenRa.Orders
Dictionary<int, List<byte[]>> orderBuffers = new Dictionary<int, List<byte[]>>();
Dictionary<int, bool> gotEverything = new Dictionary<int, bool>();
public ConnectionState State { get; private set; }
public NetworkOrderSource(string host, int port)
{
socket = new TcpClient(host, port);
this.socket.NoDelay = true;
var reader = new BinaryReader(socket.GetStream());
State = ConnectionState.Connecting;
socket = new TcpClient();
socket.BeginConnect(host, port, OnConnected, null);
socket.NoDelay = true;
}
void OnConnected(IAsyncResult r)
{
try
{
socket.EndConnect(r);
State = ConnectionState.Connected;
new Thread(() =>
{
for (; ; )
{
var reader = new BinaryReader(socket.GetStream());
var len = reader.ReadInt32();
var frame = reader.ReadInt32();
var buf = reader.ReadBytes(len - 4);
@@ -43,6 +64,11 @@ namespace OpenRa.Orders
}
}) { IsBackground = true }.Start();
}
catch
{
State = ConnectionState.NotConnected;
}
}
static List<byte[]> NoOrders = new List<byte[]>();
List<byte[]> ExtractOrders(int frame)
@@ -66,6 +92,7 @@ namespace OpenRa.Orders
public void SendLocalOrders(int localFrame, List<Order> localOrders)
{
if (socket.Connected)
socket.GetStream().WriteFrameData(localOrders, localFrame);
}

View File

@@ -26,6 +26,8 @@ namespace OpenRa.Orders
p.SendLocalOrders(i, new List<Order>());
}
public IEnumerable<IOrderSource> Sources { get { return sources; } }
public int FrameNumber { get { return frameNumber; } }
public OrderManager( IEnumerable<IOrderSource> sources )