Repos

MELHARFI-2D-Game-Engine.

Got to repo.

21. LidgrenNetwork

  • LidgrenNetwork network library
  • LidgrenNetwork is a third party networking library that i include inside mainmy lib, and I tried many network lib, and I can say that LidgrenNetwork is the best, because of its Mit license and for stability.

    I am not going to make a full tutorial of LidgrenNetwork as he already did in his page:.

    Tuto.

    Repo.

    I will just show you how to use it from my lib, if you already experience it. pay attention that i wrappe it inside my lib so the name space is changed to mine

    You need to add reference to using MELHARFI.Lidgren.Network;

    Obiously there is a Client and a Server

  • Client
  •                         
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using MELHARFI.Lidgren.Network;
    using System.Threading;
    using System.Windows.Forms;
    
    namespace _2dProject.Network
    {
        public class Client
        {
            ///////////// Connnection socket////////////////
            public static string host;
            public static string ip;
            public static int port;
            /// ////////////////////////////////////////////
    
            public static NetClient netClient;
    
            static Client()
            {
                NetPeerConfiguration config = new NetPeerConfiguration("the-morpher");
                config.AutoFlushSendQueue = false;
                config.PingInterval = 1F;
                config.ConnectionTimeout = 10;
                netClient = new NetClient(config);
                netClient.RegisterReceivedCallback(new SendOrPostCallback(GotMessage));
            }
    
            public static void Connect(string host, int port)
            {
                netClient.Start();
                NetOutgoingMessage hail = netClient.CreateMessage("Try to connect");
                netClient.Connect(host, port, hail);
            }
            public static void GotMessage(object peer)
            {
                NetIncomingMessage im;
                while ((im = netClient.ReadMessage()) != null)
                {
                    switch (im.MessageType)
                    {
                        case NetIncomingMessageType.DebugMessage:
                        case NetIncomingMessageType.ErrorMessage:
                        case NetIncomingMessageType.WarningMessage:
                        case NetIncomingMessageType.VerboseDebugMessage:
                        case NetIncomingMessageType.StatusChanged:
                            NetConnectionStatus status = (NetConnectionStatus)im.ReadByte();
    
                            if (status == NetConnectionStatus.Connected)
                                client_Connected();
                            else if (status == NetConnectionStatus.Disconnected)
                            {
                                string reason = im.ReadString();
                                client_Disconnected(reason);
                            }
                            break;
                        case NetIncomingMessageType.Data:
                            string data = im.ReadString();
                            // Data received and stored in data variable
                            break;
                        default:
                            MessageBox.Show("Unhandled type: " + im.MessageType + " " + im.LengthBytes + " bytes");
                            break;
                    }
                }
            }
    
            private static void client_Connected()
            {
                // new client connected
            }
            static void client_Disconnected(string reason)
            {
                // a client is disconnected
            }
    
            public static void SendMessage(string msg)
            {
                NetOutgoingMessage om = netClient.CreateMessage(msg);
                netClient.SendMessage(om, NetDeliveryMethod.ReliableOrdered);
                netClient.FlushSendQueue();
            }
        }
    }
                            
                          

  • Server
  •                         
    using MELHARFI.Lidgren.Network;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _2dProject.Network
    {
      public class Server
      {
        public NetServer netServer;
        NetPeerConfiguration config = new NetPeerConfiguration("the-morpher");
        public Int32 Port = 7070;
    
            public Server()
            {
          config.Port = Port;
          netServer = new NetServer(config);
          netServer.Start();
    
                while (!Console.KeyAvailable)
                {
                    NetIncomingMessage im;
                    while ((im = netServer.ReadMessage()) != null)
                    {
                        // handle incoming message
                        switch (im.MessageType)
                        {
                            case NetIncomingMessageType.DebugMessage:
                            //Console.WriteLine(im.ReadString());
                            //break;
                            case NetIncomingMessageType.ErrorMessage:
                            //Console.WriteLine(im.ReadString());
                            //break;
                            case NetIncomingMessageType.WarningMessage:
                            //Console.WriteLine(im.ReadString());
                            //break;
                            case NetIncomingMessageType.VerboseDebugMessage:
                            //Console.WriteLine(im.ReadString());
                            //break;
                            case NetIncomingMessageType.StatusChanged:
                                try
                                {
                                    string reason = im.ReadString();
                                    
                                }
                                catch
                                {
                                    //Console.WriteLine (ex.ToString ());
                                }
                                break;
                            case NetIncomingMessageType.Data:
                                string receivedData = im.ReadString();
                                break;
                            default:
                                Console.WriteLine("Unhandled type: " + im.MessageType + " " + im.LengthBytes + " bytes " + im.DeliveryMethod + "|" + im.SequenceChannel);
                                break;
                        }
                    }
                }
            }
    
        
      }
    }
                            
                          

    Services

    App Developpement

    Want me to dev an app for you ? dont hesitate to contact me.

    Programming

    Are you looking for a coder/teammate for your project ? Let's give it a try.

    Job Invitation

    Have a proposal for me ? we can discuss about it.

    Donation maybe ?!

    You want to buy me a coffe ? m.elharfi@gmail.com