Coder Social home page Coder Social logo

ginet's Introduction

Ginet

Cedrus logo

Build status Nuget downloads

A fluent networking library build on top of lidgren network. Ginet extends lidgren network with a fluent and functional API.


Quick start

Ginet is available as a NuGet package. You can install it using the NuGet Package Console window:

PM> Install-Package Ginet

Ginet favors message exchange. All classes that are handled via the INetworkManager must be marked with the GinetPackageAttribute. Ginet inferres messages by adding a unique byte id.

The classes must be marked as public with public getters / setters and contain only primitive types. The client and server registered packages should be the same. For complex serialization you can provide your custom serializer via attribute.

          [GinetPackage]
          public class MyPackage
          {
             public string Message { get; set; }
          }
          
          [GinetPackage]
          public class ConnectionApprovalMessage
          {
             public string Sender { get; set; }
             public string Password { get; set; }
          }

A simple client-server chat example here.


Server

Create, configure and register the classes that are marked with the GinetPackage attribute

          var server = new NetworkServer("MyServer",
                    builder =>
                    {
                        //via reflection
                        builder.RegisterPackages(Assembly.Load("Packages.Assembly.Name"));
                        //or manually
                        builder.Register<MyPackage>();
                    },
                    cfg =>
                    {
                       cfg.Port = 1234;
                       cfg.ConnectionTimeout = 5.0f;
                       //Additional configuration
                    });
          server.IncomingMessageHandler.LogTraffic();

Start the server

          server.Start(NetDeliveryMethod.ReliableOrdered, channel: 0);

Process Incoming Messages in a separate thread

          server.ProcessMessagesInBackground();           

If you have an update loop you can await the message processing in an async method

        await server.ProcessMessages()

Configure how to respond to incoming packages

          server.Broadcast<ChatMessage>((msg, im, om) =>
          {
              server.Out.Info($"Received {msg.Message}");
              server.SendToAllExcept(om, im.SenderConnection, NetDeliveryMethod.ReliableOrdered, channel: 0);
          }, 
          packageTransformer: msg => 
              msg.Message += "this is broadcasted");
          
          server.BroadcastExceptSender<ChatMessage>((sender, msg) =>
          {
              server.Out.Info($"Broadcasting {msg.Message}. Received from: {sender}");
          });

Configure context specific behavior with the NetIncomingMessageType and NetConnectionStatus enums

        server.IncomingMessageHandler.OnMessage(NetIncomingMessageType.ConnectionApproval, incomingMsg =>
        {
           //Configure connection approval
           var parsedMsg = server.ReadAs<ConnectionApprovalMessage>(incomingMsg);
           if(parsedMsg.Password == "my secret and encrypted password")
           {
                incomingMsg.SenderConnection.Approve();
                incomingMsg.SenderConnection.Tag = parsedMsg.Sender;
           }
           else
           {
                incomingMsg.SenderConnection.Deny();
           }
        });
         server.IncomingMessageHandler.OnConnectionChange(NetConnectionStatus.Disconnected, incomingMsg =>
         {
                 server.SendToAllExcept(
                     server.ConvertToOutgoingMessage(new MyPackage
                     { Message = $"Disconnected {incomingMsg.SenderConnection}" }),
                     im.SenderConnection,
                     NetDeliveryMethod.ReliableOrdered,
                     channel: 0);
          });
         server.IncomingMessageHandler.OnConnection(server.Host.Connections.First(), (msgType, incomingMsg) =>
         {
                 //...
         });

Upon adding a handler, an IDisposable object is returned. Disposing it causes the handler deregistration.

        var handlerDisposable = server.IncomingMessageHandler.OnMessage(NetIncomingMessageType.Data, im => { });
        
        handlerDisposable.Dispose();

Client

Create, configure and register the classes that are marked with the GinetPackage attribute

         var client = new NetworkClient("Chat", 
                   builder=>
                        builder.RegisterPackages(Assembly.Load("Packages.Assembly.Name")),
                   cfg =>
                   {  
                      //client configuration
                   });

Start / Connect

         client.Start(NetDeliveryMethod.ReliableOrdered, channel: 0);
         client.Connect("localhost", 1234, new ConnectionApprovalMessage
         {
             Sender = "Me",
             Password = "1234"
         });

Process Incoming messages

         client.ProcessMessagesInBackground();           

Handle incoming messages

        client.IncomingMessageHandler
              .OnPackage<MyPackage>((msg, sender) => 
                  Console.WriteLine($"Received {msg.Message} from {sender.SenderConnection}"));
                  

Send a message

       client.Send(new MyPackage { Message = "Hello" },
                  (om,peer) => peer.SendMessage(om,NetDeliveryMethod.ReliableOrdered));

Create a message sender lifter

       IPackageSender packageSender = client.LiftSender((msg, peer) =>
              peer.SendMessage(msg, NetDeliveryMethod.ReliableOrdered));
              
       packageSender.Send(new MyPackage { Message = "Hello" });

Logging

For custom logging targets implement the IAppender interface and pass it in the client , server creation

          var server = new NetworkServer(
          "MyServer",
          cfg => {},
          output: new MyAppenderImplementation());

The default IAppender uses the Console.WriteLine in the ActionAppender constructor.

          var server = new NetworkServer(
          "MyServer",
          cfg => {},
          output: new ActionAppender(Console.WriteLine));

Custom serializers

For custom encoding / decoding for a type, simply implement the IPackageSerializer interface add the PackageSerializerAttribute to the class

       [GinetPackage]
       [PackageSerializer(typeof(MyCustomSerializer))]
       public class MyPackage
       {
          public string Message { get; set; }
       }

Icons made by Freepik from www.flaticon.com is licensed by CC 3.0 BY

ginet's People

Contributors

gmich avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ginet's Issues

Doesn't seem to work with Unity

When I try to use this Library with Unity I get these two errors:

Unloading broken assembly Assets/Packages/Ginet.1.0.1/lib/Ginet.dll, this assembly can cause crashes in the runtime

And after that:

MissingFieldException: Field 'Ginet.NetworkManager"1<TNetPeer_REF>.<Out>k__BackingField" not found. Ginet.NetworkClient..ctor (System.String serverName, System.Action"1[T] packageContainer, System.Action"1[T] configuration, Ginet.Logging.IAppender output, System.Boolean enableAllIncomingMessages) (at <43308d2ee7014ad58c94f8424f269b3e>:0) NetworkServer.Start () (at Assets/Scripts/NetworkServer.cs:15)

(Lidgren.Networking already loaded)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.