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,等等

转载于:https://www.cnblogs.com/big-zhou/p/10093093.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值