Skip to content

Low level library of TCP and UDP communications for desktop apps

Notifications You must be signed in to change notification settings

nimrod46/NetworkBehavior

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

70 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NetworkBehavior

Low level library of TCP and UDP communications for desktop apps.

Overview

Easy to setup library that will allow you to create a server and clients communication, while using it, all network objects will inherit from NetworkIdentity and will have those features:

  1. Sync variables through all clients and server, with one line of code you can update a varibale in all clients
  2. Broadcast method, with one line of code you can broadcast methods with supported parameters
  3. Sync all cliens automatically, new clients will automatically be sync with all the relevent object and object's variables

Usage

Referencing the library

First referance the dll, keep in mind that this lib uses other libs

Runing a server

  1. Create a ServerBehavior object with the wanted port, for instance:
    ServerBehavior serverBehavior = new ServerBehavior(1331);
  2. Run the server:
    serverBehavior.Run();

Creating a client and connecting

  1. Create a ClientBehavior object with the wanted port and the server's ip address, for instance:
    ClientBehavior client = new ClientBehavior(1331, "ServerHostName");
  2. Connect the client:
    client.Connect();

Network objects

All of the examples that are shown here use MonoGame Lib, network features have nothig to do with that lib it's just examples from a real project

Creating and initilazing

  1. First we need to define our objects, this object will need to inherate from NetworkIdentity.
    So lets say we are building a game and we have this object:

    GameObject
     public class GameObject : NetworkIdentity  
          {
              public virtual Vector2 DrawLocation { get; set; }
              public virtual float SyncX
              {
                  get => syncX; set
                  {
                      syncX = value;
                      InvokeSyncVarNetworkly(nameof(SyncX), value, NetworkInterfaceType.UDP);
                      OnXSet();
                  }
              }
    
              public virtual float SyncY
              {
                  get => syncY; set
                  {
                      syncY = value;
                      InvokeSyncVarNetworkly(nameof(SyncY), value, NetworkInterfaceType.UDP);
                      OnYSet();
                  }
              }
    
              protected readonly float DEFAULT_MIN_DISTANCE_TO_UPDATE = 5;
              private float syncX;
              private float syncY;
    
              public GameObject()
              {
                  SyncX = -9999;
                  SyncY = -9999;
                  OnNetworkInitializeEvent += OnNetworkInitialize;
                  OnDestroyEvent += OnDestroyed;
              }
    
              public virtual void OnNetworkInitialize()
              {
                  DrawLocation = new Vector2(SyncX, SyncY);
              }
    
              public virtual void OnXSet()
              {
                  if (MathHelper.Distance(DrawLocation.X, SyncX) >= DEFAULT_MIN_DISTANCE_TO_UPDATE)
                  {
                      DrawLocation = new Vector2(SyncX, DrawLocation.Y);
                  }
              }
    
              public virtual void OnYSet()
              {
                  if (MathHelper.Distance(DrawLocation.Y, SyncY) >= DEFAULT_MIN_DISTANCE_TO_UPDATE)
                  {
                      DrawLocation = new Vector2(DrawLocation.X, SyncY);
                  }
              }
    
              public abstract void OnDestroyed(NetworkIdentity identity);
          }

    Right now you dont really need to understand what is going on there just remember that we have a network object class called GameObject

  2. Before creating the object we need to register it in the server and all clients, registering is done by simply creating a dummy instance (Do not use this object):
    new GameObject();
    You must register all network object before connecting to a server, the server also needs to register all network objects.

  3. Then we want to create an instance in the sever and all clients, only the server can create new network objects:

    1. If the object is a server's object (meaning only the server should control it):
      SpawnWithServerAuthority(typeof(GameObject));
    2. If the object is belong to a client you need the client id, then simply:
      SpawnWithClientAuthority(typeof(GameObject), clientId);
  4. TBC

Communicating

TBC...

About

Low level library of TCP and UDP communications for desktop apps

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages