Easy socket is just an abstraction over the C# socket
Main advantage of this library is to use the events from the library
This library is built in concept of MCU Main Control Unit. MCU is like a universal socket that
can send and receive both TCP and UDP using different methods Mcu.SendTcpAsync() and Mcu.SendUdpAsync()
See Implementation
This piece of code will send TCP packet and waits for the reply from server. If there was any error you can check ReplyPacket.Error property
using Stira.Socket.Models;
var Mcu = new Mcu()
{
IP = "192.168.10.252",
PortTcp = 3030,
PortUdp = 2020,
};
ReplyPacket rxPacket = await Mcu.SendTcpAsync(byteCommand).ConfigureAwait(false);
if (rxPacket.IsSentAndReplyReceived)
{
//Here is your reply
var reply = rxPacket.Reply;
}
This piece of code will send UDP packet and waits for the reply from server. If there was any error you can check ReplyPacket.Error property
using Stira.Socket.Models;
var Mcu = new Mcu()
{
IP = "192.168.10.252",
PortTcp = 3030,
PortUdp = 2020,
};
ReplyPacket rxPacket = await Mcu.SendUdpAsync(byteCommand).ConfigureAwait(false);
if (rxPacket.IsSentAndReplyReceived)
{
//Here is your reply
var reply = rxPacket.Reply;
}
If you want to know if packet is sent and don't want to wait for Rx/rxTimeout then use fireOnSent parameter delegate
See Implementation
var byteCommand = new byte[]{0x01, 0x02, 0x03};
ReplyPacket reply = await Mcu.SendTcpAsync(byteCommand, true, fireOnSent: (isSent) =>
{
//This code will be called when packet is sent
}).ConfigureAwait(false);
if (reply.IsSentAndReplyReceived)
{
DevReplyIncoming?.Invoke(this, reply);
}
If you want to listen on some port (3030 in this example) on your system then use ListenerTcp Class as follows
See Implementation
ListenerTcp = new ListenerTcp();
ListenerTcp.Port = 3030;
ListenerTcp.DataEvent += DataIncoming;
ListenerTcp.StartListener();
/// <summary>
/// This method will be invoked everytime when there's some incoming data
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void DataIncoming(object sender, IReplyPacket e)
{
//e.Reply will be the packet received
}
If you want to connect to the server (here 192.168.10.227 is server ip) and port number (4040 in the given example)
This example will fire the DataEvent whenever there's a new packet
See Implementation
Use Case 1 (Recommended)
ITcpClient easySocket = new EasyTcpClient() { Ip = "192.168.10.227", Port = 4545 };
Task.Run(async () => await easySocket.ConnectAsync(dataReady).ConfigureAwait(false));
private void dataReady(IReplyPacket obj)
{
List<byte> datareadyByte = obj.Reply;
}
Use Case 2
ITcpClient tcpClient = new EasyTcpClient
{
Ip = "192.168.10.227",
Port = 4040
};
tcpClient.Connect();
tcpClient.DataEvent += DataIncoming;
/// <summary>
/// This method will be invoked everytime when there's
/// some incoming data from the server. This example will loopback the data
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void DataIncoming(object sender, IReplyPacket e)
{
//e.Reply will be the packet received, it will loopback
tcpClient.SendAsync(e.Reply.ToArray()).ConfigureAwait(false);
}
If you want to connect to the server (here 192.168.10.227 is server ip) and port number (4040 in the given example)
This example will fire the DataEvent whenever it will receive the given endbytes
The data will be accumulated till it receives the give endbytes (0x0a 0x0b in this example)
See Implementation
ITcpClient tcpClient = new EasyTcpClient
{
Ip = "192.168.10.227",
Port = 4040
};
var endBytesIdentifier = new byte[] { 0x0a, 0x0b };
tcpClient.Connect(endBytesIdentifier);
tcpClient.DataEvent += DataIncoming;
e.g. Lets assume server sent 4 packets as given bellow
- 0x11 0x12 0x13 (Rx buffer will accumulate this and DataEvent wont trigger)
- 0x10 0x15 (Rx buffer will accumulate this and DataEvent wont trigger)
- 0x0a 0x45 0x78 (Rx buffer will accumulate this and DataEvent wont trigger)
- 0x15 0x0a 0x0b (Here we received the endbytes as requested so the DataEvent will be triggered)
Data will be 0x11 0x12 0x13 0x10 0x15 0x0a 0x45 0x78 0x15 0x0a 0x0b