Combines both tcp and udp with 'DeliveryMethod' #49
-
Like this.
is this lib have RUDP? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
RUDP or Reliable UDP: Isn't working now but I'll do this working on release of Netly4
If implemented you can use like this: client.To.Data("hello world", RUDP.Unreliable); // can lost packet
client.To.Data("hello world", RUDP.Reliable); // can't lost packet (like TCP) It's imposible combile in same object! How TCP and UDP can be same? But.. This level of programming is unbelievable... 🤣🤣🤣 it's like magic! TCP and UDP together as ONE... 🤣🤣🤣 But it is possible to encapsulate it to work like this. Look below. 👨💻
NetworkTransport client = new NetworkTransport(new Host("127.0.0.1", 13001), new Host("127.0.0.1", 13002));
client.ToData("hello world", DeliveryMethod.TCP); // tcp only
client.ToData("hello world", DeliveryMethod.UDP); // udp only
client.ToData("hello world", DeliveryMethod.Any); // tdp and udp
public enum DeliveryMethod
{
TCP = 1,
UDP = 2,
Any = 0,
}
public class NetworkTransport
{
private TcpClient tcpClient;
private UdpClient udpClient;
public NetworkTransport(Host tcpHost, Host udpHost)
{
tcpClient = new TcpClient(framing: true);
tcpClient.Open(tcpHost);
udpClient = new UdpClient(false);
udpClient.Open(udpHost);
}
public void ToData(byte[] data, DeliveryMethod method)
{
if (method == DeliveryMethod.TCP) tcpClient.ToData(data);
else if (method == DeliveryMethod.UDP) udpClient.ToData(data);
else if (method == DeliveryMethod.Any)
{
tcpClient.ToData(data);
udpClient.ToData(data);
}
}
public void ToData(string data, DeliveryMethod method)
{
ToData(Encoding.UTF8.GetBytes(daya), method);
} |
Beta Was this translation helpful? Give feedback.
-
Increment this code: @nvh2001 @vanhaodev
UsageI created complete wrapper code, I hope this help your project (I create all code here 👇 on github, maybe have syntax error)... 🦖 NetworkTransport client = new NetworkTransport();
// connect to server
client.Open(new Host("127.0.0.1", 13001), new Host("127.0.0.1", 13002));
// disconnect
client.Close();
// opened connection callback
client.OnOpen(deliveryMethod =>
{
...
});
// closed connection callback
client.OnClose(deliveryMethod =>
{
...
});
// receive data callback
client.OnData((bytes, deliveryMethod) =>
{
...
});
// receive event callback
client.OnEvent((name, bytes, deliveryMethod) =>
{
...
});
// send data
client.ToData("hello world", DeliveryMethod.TCP); // tcp only
client.ToData("hello world", DeliveryMethod.UDP); // udp only
client.ToData("hello world", DeliveryMethod.Any); // tdp and udp
// send event
client.ToEvent("event-name", "hello world", DeliveryMethod.TCP); // tcp only
client.ToEvent("event-name", "hello world", DeliveryMethod.UDP); // udp only
client.ToEvent("event-name", "hello world", DeliveryMethod.Any); // tdp and udp Source Code
public enum DeliveryMethod
{
TCP = 1,
UDP = 2,
Any = 0,
}
public class NetworkTransport
{
private TcpClient tcpClient;
private UdpClient udpClient;
public NetworkTransport
{
tcpClient = new TcpClient(framing: true);
udpClient = new UdpClient(false);
}
public void Open(Host tcpHost, Host udpHost)
{
tcpClient.Open(tcpHost);
udpClient.Open(udpHost);
}
public void Close()
{
tcpClient.Close();
udpClient.Close();
}
public void ToData(byte[] data, DeliveryMethod method)
{
if (method == DeliveryMethod.TCP) tcpClient.ToData(data);
else if (method == DeliveryMethod.UDP) udpClient.ToData(data);
else if (method == DeliveryMethod.Any)
{
tcpClient.ToData(data);
udpClient.ToData(data);
}
}
public void ToData(string data, DeliveryMethod method)
{
ToData(Encoding.UTF8.GetBytes(data), method);
}
public void ToEvent(string name, byte[] data, DeliveryMethod method)
{
if (method == DeliveryMethod.TCP) tcpClient.ToData(data);
else if (method == DeliveryMethod.UDP) udpClient.ToData(data);
else if (method == DeliveryMethod.Any)
{
tcpClient.ToEvent(name, data);
udpClient.ToEvent(name, data);
}
}
public void ToEvent(string name, string data, DeliveryMethod method)
{
ToEvent(name, Encoding.UTF8.GetBytes(data), method);
}
void OnEvent(Action<string, byte[], DeliveryMethod> callback)
{
tcpClient.OnEvent((name, data) =>
{
callback(name, data, DeliveryMethod.TCP);
});
udpClient.OnEvent((name, data) =>
{
callback(name, data, DeliveryMethod.UDP);
});
}
void OnData(Action<byte[], DeliveryMethod> callback)
{
tcpClient.OnData((data) =>
{
callback(data, DeliveryMethod.TCP);
});
udpClient.OnData((data) =>
{
callback(data, DeliveryMethod.UDP);
});
}
void OnClose(Action<DeliveryMethod> callback)
{
tcpClient.OnClose(() =>
{
callback(DeliveryMethod.TCP);
});
udpClient.OnClose(() =>
{
callback(DeliveryMethod.UDP);
});
}
void OnOpen(Action<DeliveryMethod> callback)
{
tcpClient.OnOpen(() =>
{
callback(DeliveryMethod.TCP);
});
udpClient.OnOpen(() =>
{
callback(DeliveryMethod.UDP);
});
}
} |
Beta Was this translation helpful? Give feedback.
Ohh:: It's very simple adding ordered UDP.
So with this in mind we can add an ID per message as soon as the message ID if is less than the previous message means old data (so we will delete/destroy/discard it), thus now having the effect of (Ordered data)
Code: