Hack Forums
[.NET] SyncIO || Easy multipurpose networking library. | Now with UDP and Remote Functions | - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] SyncIO || Easy multipurpose networking library. | Now with UDP and Remote Functions | (/showthread.php?tid=5506453)

Pages: Pages 1 2 3


SyncIO || Easy multipurpose networking library. | Now with UDP and Remote Functions | - BahNahNah - Dec 27, 2016

This has been my mini project for the last couple of weeks, and its finally done 🤗

Download: GitHub
An example chat program can be found HERE on the github repository (and it is also included in the source download)

Here is a very basic example:

Server
Code
var server = new SyncIOServer();
server.SetHandler((SyncIOConnectedClient c, object[] data) => {
    c.Send(data); //Simple echo
});
server.ListenTCP(12345);
Console.ReadLine();

Client
Code
var client = new SyncIOClient();
client.SetHandler((SyncIOClient c, object[] data) => {
    Console.WriteLine(string.Join(" ", data));
});
client.Connect("127.0.0.1", 12345);
client.WaitForHandshake();
client.Send("Hello", "World");
or non-blocking way of waiting for handshake:
Code
client.OnHandshake += (SyncIOClient c, Guid ID, bool success) => {
    if (success)
        c.Send("Hello", "World", "from", ID);
    else
        Console.WriteLine("Failed handshake.");
};


To-Do
  • Implement popper encryption support (Internal structure is done)
  • Add UDP support (mabe)
  • Make this thread more presentable than your waifu.


More throughout examples:

Listening on ports
Code
var port = server.ListenTCP(1000);
sock.OnDisconnect += (sender, err) => {
    //Called when socket is closed.
};
port.Dispose(); //Stop listening on this port.

Listening on multiple ports:
Code
server.ListenTCP(1000);
server.ListenTCP(1001);
server.ListenTCP(1002);

Looping through open ports:
Code
foreach (var s in server) {
     Console.WriteLine(s.EndPoint.Port);
    //s.Dispose() if you want to close all open ports
}

Getting already open port from server without var:

Code
server.ListenTCP(1000);
var sock = server[1000];

//Equivalent to
var sock = server.ListenTCP(1000);

Custom types
To send custom types, the classes must meet two chriteriers:
  1. Impliment IPacket interface
  2. Marked with Seralizable attribute

For example, a valid class for sending might look like this:
Code
[Serializable]
class EchoMessage : IPacket {
    public string Message { get; set; }
    public EchoMessage(string msg) { //(Constructor is not nessisary
        Message = msg;
    }
}

Now we have to add this class to our client and server so they can send this type. First step is to create a packer that has all of the custom types we want.
An example would be:
Code
var packer = new Packager(new Type[] {
    typeof(EchoMessage),
});

Then create server/client instances with this packer.
(They dont have to be the same instance of the Packager, but both the packager for the client and server must contain all the types you want to send)

Code
var server = new SyncIOServer(TransportProtocal.IPv4, packer);
var client = new SyncIOClient(TransportProtocal.IPv4, packer);


Handlers
There are three types of handlers for both client and server.

1) Generic array handler
Code
server.SetHandler((SyncIOConnectedClient sender, object[] data) => {

});
Code
client.SetHandler((SyncIOClient sender, object[] data) => {

});

This handler will be called when the sending item is NOT a SINGLE IPacket object.

For example, these will all trigger this event on the server:
Code
client.Send("Hello World");
//data[0] = "Hello World"

client.Send("Hello", "World");
//data[0] = "Hello"
//data[1] = "World"

client.Send("Hello", 1, 2);
//data[0] = "Hello"
//data[1] = 1
//data[2] = 2

client.Send(new EchoMessage("Echo"), "Message");
//data[0] = (EchoMessage {Message = "Echo" })
//data[1] = "Message"

client.Send(new EchoMessage("Hello"), new EchoMessage("World"));
//data[0] = (EchoMessage {Message = "Hello" })
//data[1] = (EchoMessage {Message = "World" })

2) Specific packet handler
Code
server.SetHandler<EchoMessage >((SyncIOConnectedClient sender, EchoMessage packet) => {

});
Code
client.SetHandler<EchoMessage >((SyncIOClient sender, EchoMessage  packet) => {

});

Any valid custom types can be used, but for this example EchoMessage is used.
Each type can only have one handler, but there can be many handlers of different types.
Spoiler (Click to View)

This will trigger this event on the server:
Code
client.Send(new EchoMessage("Echo"));
//packet = (EchoMessage {Message = "Echo" })

2) Generic packet handler
Code
server.SetHandler<EchoMessage >((SyncIOConnectedClient sender, IPacket packet) => {

});
Code
client.SetHandler<EchoMessage >((SyncIOClient sender, IPacket packet) => {

});

This handler will handle all valid custom types that do not have a specific packet handler. Generally, this would not be used as all custom types added to the packer should have handlers assigned.


* - Malum™ - Dec 27, 2016

Holy shit! Perfect timing. I've been using NetworkComms and playing with other libraries and they're always so complicated for simple things. I will be using this, thanks!


* - CoderEX - Dec 27, 2016

Your code is so clean; it is beautiful! Teach me your ways. LOL.


* - NinjaLeaks - Dec 27, 2016

Damn thats some sexy ass code lol. Really nice op Ill be using this in the future!


* - +Haze - Dec 27, 2016

After all this time BahNahNah, I've only just come to realise you're actually a very talented developer. Keep up the work man, honestly, you'll go far.


* - HFDone - Dec 27, 2016

didnt take alook yet but i hope its not base on your esockt class


* - BahNahNah - Dec 27, 2016

(Dec 27, 2016 08:09 PM)HFDone Wrote:  didnt take alook yet but i hope its not base on your esockt class

We dont talk about that class.


* - HFDone - Dec 27, 2016

whats framework its base on


* - BahNahNah - Dec 27, 2016

(Dec 27, 2016 08:24 PM)HFDone Wrote:  whats framework its base on

Its not based on anything.


* - ubbelol - Dec 28, 2016

Looks like a nice simple library. A few typos here and there in the naming of stuff, but that doesn't really matter. :P You might wanna consider adding a timeout for the blocking handshaking at https://github.com/BahNahNah/SyncIO/blob/master/SyncIO/Client/SyncIOClient.cs#L153 so you don't get a total deadlock if something goes wrong.


* - BahNahNah - Dec 28, 2016

(Dec 28, 2016 02:25 AM)ubbelol Wrote:  Looks like a nice simple library. A few typos here and there in the naming of stuff, but that doesn't really matter. :P You might wanna consider adding a timeout for the blocking handshaking at https://github.com/BahNahNah/SyncIO/blob/master/SyncIO/Client/SyncIOClient.cs#L153 so you don't get a total deadlock if something goes wrong.

Diddnt think about that 🤐
Thanks, updated.


* - Ninja - Dec 28, 2016

First of all, thanks for this awesome release BahNahNah, it is really nice.

Correct me if I am wrong but shouldn't this:
http://prnt.sc/douhoq
be "World"?

And can't you make the WaitForHandshake awaitable or something?


* - BahNahNah - Dec 28, 2016

(Dec 28, 2016 05:18 AM)Ninja Wrote:  First of all, thanks for this awesome release BahNahNah, it is really nice.

Correct me if I am wrong but shouldn't this:
http://prnt.sc/douhoq
be "World"?

And can't you make the WaitForHandshake awaitable or something?

Yes it is ;p
I could make it awaitable, but WaitForHandshake is suppose to be blocking. Subscribing to the event is the non-blocking way.


* - Ninja - Dec 28, 2016

(Dec 28, 2016 05:41 AM)BahNahNah Wrote:  Yes it is ;p
I could make it awaitable, but WaitForHandshake is suppose to be blocking. Subscribing to the event is the non-blocking way.

How about Application.DoEvents? ( ͡° ͜ʖ ͡°)


* - ubbelol - Dec 28, 2016

(Dec 28, 2016 05:42 AM)Ninja Wrote:  How about Application.DoEvents? ( ͡° ͜ʖ ͡°)

I'm not sure if you're serious or not but that's not how ManualResetEvent works. Also Application.DoEvents is windows forms stuff and not really related.