Peer.Lib 1.2.2

dotnet add package Peer.Lib --version 1.2.2
                    
NuGet\Install-Package Peer.Lib -Version 1.2.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Peer.Lib" Version="1.2.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Peer.Lib" Version="1.2.2" />
                    
Directory.Packages.props
<PackageReference Include="Peer.Lib" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Peer.Lib --version 1.2.2
                    
#r "nuget: Peer.Lib, 1.2.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Peer.Lib@1.2.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Peer.Lib&version=1.2.2
                    
Install as a Cake Addin
#tool nuget:?package=Peer.Lib&version=1.2.2
                    
Install as a Cake Tool

peer 通讯协议

基于tcp实现的高效通讯协议

服务端示例

   // 自定义序列化
     public class CJsonConverter : BaseConverter
    {
        public override T Deserialize<T>(byte[] bytes)
        {
            return JsonConvert.DeserializeObject<T>(Encoding.UTF8.GetString(bytes));
        }

        public override object DeserializeObject(byte[] bytes, Type type)
        {
            return JsonConvert.DeserializeObject(Encoding.UTF8.GetString(bytes), type);
        }

        public override byte[] Serialize(object data)
        {
            return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data));
        }
    }
    internal class Program
    {

        static void Main(string[] args)
        {
            using PeerServer peerServer = new PeerServer("123123", new IPEndPoint(IPAddress.Any, 9900), new PeerOptions(new CJsonConverter()));
            peerServer.OnAccept += PeerServer_OnAccept;
            peerServer.Listen();
        }

        private static void PeerServer_OnAccept(object sender, PeerStream e)
        {
            Console.WriteLine("链接");
            e.OnReceive += E_OnReceive;
            e.OnError += E_OnError;
            e.OnDisconnect += E_OnDisconnect;
            e.SendAnyAsync("主动向客户端发消息");
        }

        private static void E_OnDisconnect(object sender, PeerStream e)
        {
            Console.WriteLine("断开");
        }

        private static void E_OnError(object sender, Exception e)
        {
            // 错误
            Console.WriteLine((e.InnerException ?? e).Message);
        }

        private static PeerResponse E_OnReceive(PeerStream peer, PeerRequest request)
        {
            // 响应相同结果给客户端
            var paramters = request.GetParameters();
            var sss = paramters.GetNext<string>();

            return request.CreateResponse(sss);
        }
    }

客户端示例

    
    internal class Program
    {

        static void Main(string[] args)
        {
            PeerClient peerClient = new PeerClient("123123", IPEndPoint.Parse("127.0.0.1:9900"));
            peerClient.OnReceive += PeerClient_OnReceive;
            peerClient.OnDisconnect += PeerClient_OnDisconnect;
            peerClient.OnConnected += PeerClient_OnConnected;
            peerClient.OnError += PeerClient_OnError;
            // 重连机制
            peerClient.WithAutomaticReconnect(count => TimeSpan.FromSeconds(count switch
            {
                0 => 1,
                1 => 2,
                2 => 5,
                3 => 10,
                _ => 60
            }))
                .Connect();

            Console.ReadLine();
        }

        private static void PeerClient_OnError(object sender, Exception e)
        {
            Console.WriteLine((e.InnerException ?? e).Message);
        }

        private static void PeerClient_OnConnected(object sender, PeerStream e)
        {
            Console.WriteLine("连接成功");
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            for (int i = 0; i < 10000; i++)
            {
                // 发送 并 响应结果
                var res = e.SendAnyWithResponseAsync<string>("马上ssss三四次").GetAwaiter().GetResult();

            }
        
            stopwatch.Stop();
            Console.WriteLine("Client QPS:" + 10000d / stopwatch.ElapsedMilliseconds * 1000);
        }

        private static void PeerClient_OnDisconnect(object sender, PeerStream e)
        {
            Console.WriteLine("断开了");
        }

        /// <summary>
        /// 接收服务器消息
        /// </summary>
        /// <param name="peer"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        private static PeerResponse PeerClient_OnReceive(PeerStream peer, PeerRequest request)
        {
            Console.WriteLine("接收服务器消息:" + request.GetParameters().GetNext<string>());
            return request.CreateResponse();
        }
    }
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Peer.Lib:

Package Downloads
MTPCore

微服务通信

MicroMq.Core

轻量级消息中间件

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.2 183 1/1/2025
1.2.1 298 12/22/2024
1.1.3 513 9/28/2024
1.1.2 183 9/5/2024
1.1.1 191 8/22/2024
1.1.0 199 8/22/2024
1.0.0 204 8/21/2024