1
0
mirror of https://github.com/lidgren/lidgren-network-gen3.git synced 2026-05-17 23:56:30 +09:00

Unconnected sample added

This commit is contained in:
lidgren
2011-01-29 14:01:37 +00:00
parent 876905ca92
commit d3085ec38a
11 changed files with 757 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using SamplesCommon;
using Lidgren.Network;
namespace UnconnectedSample
{
static class Program
{
public static Form1 MainForm;
public static NetPeer Peer;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainForm = new Form1();
NetPeerConfiguration config = new NetPeerConfiguration("unconntest");
config.EnableMessageType(NetIncomingMessageType.UnconnectedData);
config.AcceptIncomingConnections = false; // don't accept connections; we're just using unconnected messages in this sample
Peer = new NetPeer(config);
Peer.Start();
Application.Idle += new EventHandler(AppLoop);
Application.Run(MainForm);
}
static void AppLoop(object sender, EventArgs e)
{
while (NativeMethods.AppStillIdle)
{
// read any incoming messages
NetIncomingMessage im;
while((im = Peer.ReadMessage()) != null)
{
switch (im.MessageType)
{
case NetIncomingMessageType.DebugMessage:
case NetIncomingMessageType.VerboseDebugMessage:
case NetIncomingMessageType.WarningMessage:
case NetIncomingMessageType.ErrorMessage:
MainForm.richTextBox1.AppendText(im.ReadString() + Environment.NewLine);
break;
case NetIncomingMessageType.UnconnectedData:
MainForm.richTextBox1.AppendText("Received from " + im.SenderEndpoint + ": " + im.ReadString() + Environment.NewLine);
Peer.Recycle(im);
break;
}
}
System.Threading.Thread.Sleep(1);
}
}
internal static void Send(string host, int port, string text)
{
NetOutgoingMessage om = Peer.CreateMessage();
om.Write(text);
Peer.SendUnconnectedMessage(om, host, port);
}
}
}