lidgren 介绍和使用 (一)
有一年多没写东西了,一懒惰就会找借口,今夜擦拭了堆满灰尘的笔记本电脑,插上电源,写了一些代码,时光好像又回到了以前,那个充满梦想的自己。还能回来吗?
好了,废话不多说了,今天主要说说 实时通讯,因为手机游戏 现在 越来越 需要 这类高速的通信 来交互,不然不好玩!c++有udt,rudp这一类快速稳定不丢包的 开源库,c# 有人用raknet,不过它不能组播也不能p2p。所以我今天推荐的是 lidgren 。这个框架 国内很少有人用,但它能快速安全udp发送,也能p2p发送。最重要的是 相比国内2万一套ESFramework,它是免费的!
先来一点 我修改后的代码。
using Lidgren.Network; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace lidgrenwin { class Program { private static NetServer s_server; static void Main(string[] args) { NetPeerConfiguration config = new NetPeerConfiguration("chat"); config.MaximumConnections = 100; config.Port = 14242; s_server = new NetServer(config); s_server.Start(); while (true) { NetIncomingMessage msg; while ((msg = s_server.ReadMessage()) != null) { switch (msg.MessageType) { case NetIncomingMessageType.StatusChanged: NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte(); string reason = msg.ReadString(); Output(NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier) + " " + status + ": " + reason); if (status == NetConnectionStatus.Connected) Output("Remote hail: " + msg.SenderConnection.RemoteHailMessage.ReadString()); UpdateConnectionsList(); break; case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.VerboseDebugMessage: string text = msg.ReadString(); Output(text); break; case NetIncomingMessageType.ErrorMessage: case NetIncomingMessageType.DebugMessage: var str = msg.ReadString(); if (str.StartsWith("Malformed packet; stated") || str.StartsWith("Received unhandled library message") || str.StartsWith("Unexpected NetMessageType")) break; // we'll get a bunch of these and we're fine with that Console.WriteLine(msg.MessageType + ": " + str); break; case NetIncomingMessageType.Data: string chat = msg.ReadString(); Output("Broadcasting '" + chat + "'"); List<NetConnection> all = s_server.Connections; // get copy all.Remove(msg.SenderConnection); if (all.Count > 0) { NetOutgoingMessage om = s_server.CreateMessage(); om.Write(NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier) + " said: " + chat); s_server.SendMessage(om, all, NetDeliveryMethod.ReliableOrdered, 0); } break; case NetIncomingMessageType.UnconnectedData: Console.WriteLine("Received " + msg.LengthBits + " bits of unconnected data"); break; default: Output("Unhandled type: " + msg.MessageType + " " + msg.LengthBytes + " bytes " + msg.DeliveryMethod + "|" + msg.SequenceChannel); break; } s_server.Recycle(msg); } Thread.Sleep(1); } } private static void Output(string text) { Console.WriteLine(text); } private static void UpdateConnectionsList() { foreach (NetConnection conn in s_server.Connections) { string str = NetUtility.ToHexString(conn.RemoteUniqueIdentifier) + " from " + conn.RemoteEndPoint.ToString() + " [" + conn.Status + "]"; Output(str); } } } }
这一段是 服务器代码,目的就是 接收 客户端来的消息,并且发送给所以 客户端。
这里面最主要的是switch NetIncomingMessageType以及下面代码,快速有序发送。不丢包
s_server.SendMessage(om, all, NetDeliveryMethod.ReliableOrdered, 0);
今天让大家认识一下,从下次开始,我会编写更多的demo和大家讲使用方法,包括unity运用,包括p2p,等等