You've already forked lidgren-network-gen3
mirror of
https://github.com/lidgren/lidgren-network-gen3.git
synced 2026-05-17 15:46:33 +09:00
Tweaked/fixed some samples
This commit is contained in:
@@ -17,6 +17,8 @@ namespace ImageClient
|
|||||||
public bool[] ReceivedSegments;
|
public bool[] ReceivedSegments;
|
||||||
public int NumReceivedSegments;
|
public int NumReceivedSegments;
|
||||||
|
|
||||||
|
private double m_startedFetching;
|
||||||
|
|
||||||
public ImageGetter(string host, NetPeerConfiguration copyConfig)
|
public ImageGetter(string host, NetPeerConfiguration copyConfig)
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
@@ -27,7 +29,23 @@ namespace ImageClient
|
|||||||
Client = new NetClient(config);
|
Client = new NetClient(config);
|
||||||
Client.Start();
|
Client.Start();
|
||||||
|
|
||||||
Client.DiscoverLocalPeers(14242);
|
if (!string.IsNullOrEmpty(host))
|
||||||
|
{
|
||||||
|
Client.Connect(host, 14242, GetApproveData());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Client.DiscoverLocalPeers(14242);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private NetOutgoingMessage GetApproveData()
|
||||||
|
{
|
||||||
|
// create approval data
|
||||||
|
NetOutgoingMessage approval = Client.CreateMessage();
|
||||||
|
approval.Write(42);
|
||||||
|
approval.Write("secret");
|
||||||
|
return approval;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Heartbeat()
|
public void Heartbeat()
|
||||||
@@ -39,15 +57,8 @@ namespace ImageClient
|
|||||||
{
|
{
|
||||||
case NetIncomingMessageType.DiscoveryResponse:
|
case NetIncomingMessageType.DiscoveryResponse:
|
||||||
// found server! just connect...
|
// found server! just connect...
|
||||||
|
|
||||||
string serverResponseHello = inc.ReadString();
|
string serverResponseHello = inc.ReadString();
|
||||||
|
Client.Connect(inc.SenderEndpoint, GetApproveData());
|
||||||
// create approval data
|
|
||||||
NetOutgoingMessage approval = Client.CreateMessage();
|
|
||||||
approval.Write(42);
|
|
||||||
approval.Write("secret");
|
|
||||||
|
|
||||||
Client.Connect(inc.SenderEndpoint, approval);
|
|
||||||
break;
|
break;
|
||||||
case NetIncomingMessageType.DebugMessage:
|
case NetIncomingMessageType.DebugMessage:
|
||||||
case NetIncomingMessageType.VerboseDebugMessage:
|
case NetIncomingMessageType.VerboseDebugMessage:
|
||||||
@@ -55,12 +66,14 @@ namespace ImageClient
|
|||||||
case NetIncomingMessageType.ErrorMessage:
|
case NetIncomingMessageType.ErrorMessage:
|
||||||
string str = inc.ReadString();
|
string str = inc.ReadString();
|
||||||
NativeMethods.AppendText(richTextBox1, str);
|
NativeMethods.AppendText(richTextBox1, str);
|
||||||
System.IO.File.AppendAllText("C:\\tmp\\clientlog.txt", str + Environment.NewLine);
|
//System.IO.File.AppendAllText("C:\\tmp\\clientlog.txt", str + Environment.NewLine);
|
||||||
break;
|
break;
|
||||||
case NetIncomingMessageType.StatusChanged:
|
case NetIncomingMessageType.StatusChanged:
|
||||||
NetConnectionStatus status = (NetConnectionStatus)inc.ReadByte();
|
NetConnectionStatus status = (NetConnectionStatus)inc.ReadByte();
|
||||||
string reason = inc.ReadString();
|
string reason = inc.ReadString();
|
||||||
NativeMethods.AppendText(richTextBox1, "New status: " + status + " (" + reason + ")");
|
NativeMethods.AppendText(richTextBox1, "New status: " + status + " (" + reason + ")");
|
||||||
|
if (status == NetConnectionStatus.Connected)
|
||||||
|
m_startedFetching = NetTime.Now;
|
||||||
break;
|
break;
|
||||||
case NetIncomingMessageType.Data:
|
case NetIncomingMessageType.Data:
|
||||||
// image data, whee!
|
// image data, whee!
|
||||||
@@ -140,6 +153,10 @@ namespace ImageClient
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double span = NetTime.Now - m_startedFetching;
|
||||||
|
double bytesPerSecond = (double)totalBytes / span;
|
||||||
|
NativeMethods.AppendText(richTextBox1, "Fetched at " + NetUtility.ToHumanReadable((long)bytesPerSecond) + " per second");
|
||||||
|
|
||||||
Client.Disconnect("So long and thanks for all the fish!");
|
Client.Disconnect("So long and thanks for all the fish!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,8 +19,15 @@ namespace ImageClient
|
|||||||
Application.SetCompatibleTextRenderingDefault(false);
|
Application.SetCompatibleTextRenderingDefault(false);
|
||||||
MainForm = new Form1();
|
MainForm = new Form1();
|
||||||
|
|
||||||
Application.Idle += new EventHandler(AppLoop);
|
try
|
||||||
Application.Run(MainForm);
|
{
|
||||||
|
Application.Idle += new EventHandler(AppLoop);
|
||||||
|
Application.Run(MainForm);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
MessageBox.Show("Ouch: " + ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void AppLoop(object sender, EventArgs e)
|
static void AppLoop(object sender, EventArgs e)
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ namespace ImageServer
|
|||||||
NetPeerConfiguration config = new NetPeerConfiguration("ImageTransfer");
|
NetPeerConfiguration config = new NetPeerConfiguration("ImageTransfer");
|
||||||
config.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
|
config.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
|
||||||
config.EnableMessageType(NetIncomingMessageType.DiscoveryRequest);
|
config.EnableMessageType(NetIncomingMessageType.DiscoveryRequest);
|
||||||
|
config.AutoExpandMTU = true;
|
||||||
|
|
||||||
// listen on port 14242
|
// listen on port 14242
|
||||||
config.Port = 14242;
|
config.Port = 14242;
|
||||||
@@ -57,7 +58,7 @@ namespace ImageServer
|
|||||||
// just print any message
|
// just print any message
|
||||||
string str = inc.ReadString();
|
string str = inc.ReadString();
|
||||||
NativeMethods.AppendText(MainForm.richTextBox1, str);
|
NativeMethods.AppendText(MainForm.richTextBox1, str);
|
||||||
System.IO.File.AppendAllText("C:\\tmp\\serverlog.txt", str + Environment.NewLine);
|
//System.IO.File.AppendAllText("C:\\tmp\\serverlog.txt", str + Environment.NewLine);
|
||||||
break;
|
break;
|
||||||
case NetIncomingMessageType.DiscoveryRequest:
|
case NetIncomingMessageType.DiscoveryRequest:
|
||||||
NetOutgoingMessage dom = Server.CreateMessage();
|
NetOutgoingMessage dom = Server.CreateMessage();
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ namespace SpeedTestServer
|
|||||||
break;
|
break;
|
||||||
case NetDeliveryMethod.UnreliableSequenced:
|
case NetDeliveryMethod.UnreliableSequenced:
|
||||||
case NetDeliveryMethod.ReliableSequenced:
|
case NetDeliveryMethod.ReliableSequenced:
|
||||||
if (expected < nr)
|
if (nr < expected)
|
||||||
throw new NetException(im.DeliveryMethod.ToString() + " failed! Expected " + expected + " received " + nr);
|
throw new NetException(im.DeliveryMethod.ToString() + " failed! Expected " + expected + " received " + nr);
|
||||||
s_nextNumber[slot] = nr + 1;
|
s_nextNumber[slot] = nr + 1;
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in New Issue
Block a user