Dec 27, 2016 07:13 PM
(This post was last modified: Jan 20, 2017 08:17 AM by BahNahNah.)
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
Client
or non-blocking way of waiting for handshake:
To-Do
More throughout examples:
Listening on ports
Listening on multiple ports:
Looping through open ports:
Getting already open port from server without var:
Custom types
To send custom types, the classes must meet two chriteriers:
For example, a valid class for sending might look like this:
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:
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)
Handlers
There are three types of handlers for both client and server.
1) Generic array handler
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:
2) Specific packet handler
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:
2) Generic packet handler
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.
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");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:
- Impliment IPacket interface
- 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.


