NetCoreServer icon indicating copy to clipboard operation
NetCoreServer copied to clipboard

For thoese who want to have a tcp packet instead of stream client/server

Open unununon opened this issue 2 years ago • 2 comments

if you want to use tcp with packet instead of stream,just follow this.

public class Utility
    {
        public static void Int2Byte(int i,out byte b1,out byte b2) {
            b1 = (byte)(i >> 8);
            b2 = (byte)(i >> 0);
        }
        public static int Byte2Int(byte b1,byte b2) {
            return (b1 & 0xff) << 8 | (b2 & 0xff) << 0;
        }
}
class TcpPacketClient : TcpClient
    {
        List<byte> buffers = new List<byte>();
        byte[] bufferArray = new byte[1024];
        byte[] sendByteArray = new byte[1024];

        public TcpPacketClient(string address, int port) : base(address, port) {}

        public override bool SendAsync(byte[] buffer, long offset, long size) {
            Array.Copy(buffer, offset, sendByteArray, 2, size);
            Utility.Int2Byte((int)size, out var b1, out var b2);
            sendByteArray[0] = b1;
            sendByteArray[1] = b2;
            return base.SendAsync(sendByteArray, 0, size + 2);
        }

        void OnReceived(string str) {
            Console.WriteLine(str);
        }
        protected override void OnReceived(byte[] buffer, long offset, long size) {
            int len = -1;
            lock (buffers) {
                if (buffer.Length < 2) {
                    buffers.Add(buffer[0]);
                    return;
                }
                for (long i = offset; i < offset + size; i++) {
                    buffers.Add(buffer[i]);
                }
                if (buffers.Count >= 2) {
                    while (buffers.Count>2) {
                        len = Utility.Byte2Int(buffers[0], buffers[1]);
                        //if bytes length is not enough
                        //then return and waiting for next round
                        if (len > buffers.Count - 2) {
                            return;
                        }
                        for (int i = 0; i < len; i++) {
                            bufferArray[i] = buffers[i + 2];
                        }
                        buffers.RemoveRange(0, len + 2);
                        OnReceived(Encoding.UTF8.GetString(bufferArray, 0, len));
                    }
                }
            }
        }
}
class TcpPacketSession : TcpSession
    {
        List<byte> buffers = new List<byte>();
        byte[] bufferArray = new byte[1024];
        byte[] byteArray = new byte[1024];

        public TcpPacketSession(TcpServer server) : base(server) {}

        public override bool SendAsync(byte[] buffer, long offset, long size) {
            Array.Copy(buffer, offset, byteArray, 2, size);
            Utility.Int2Byte((int)size, out var b1, out var b2);
            byteArray[0] = b1;
            byteArray[1] = b2;

            return base.SendAsync(byteArray, 0, size+2);
        }
        
        protected override void OnReceived(byte[] buffer, long offset, long size)
        {
            int len = -1;
            lock (buffers) {
                if (buffer.Length < 2) {
                    buffers.Add(buffer[0]);
                    return;
                }
                for (long i = offset; i < offset + size; i++) {
                    buffers.Add(buffer[i]);
                }
                if (buffers.Count >= 2) {
                    while (buffers.Count > 2) {
                        len = Utility.Byte2Int(buffers[0], buffers[1]);
                        //if bytes length is not enough
                        //then return and waiting for next round
                        if (len > buffers.Count - 2) {
                            return;
                        }
                        for (int i = 0; i < len; i++) {
                            bufferArray[i] = buffers[i + 2];
                        }
                        buffers.RemoveRange(0, len + 2);
                        OnReceived(Encoding.UTF8.GetString(bufferArray, 0, len));
                    }
                }
            }
        }
        void OnReceived(string str) {
            Console.WriteLine("incoming:" + str);
        }
}
`
just modify client/session class or Inheritance from them and override it.
 

unununon avatar Dec 04 '21 05:12 unununon

Is it possible to send, wait and receive string result as sync? Example: string result = client.Send("GetX");

aqoamann avatar Dec 14 '21 07:12 aqoamann

Is it possible to send, wait and receive string result as sync? Example: string result = client.Send("GetX");

just using Encoding.utf8.GetString()/Encoding.Utf8.GetBytes("GetX");

liunliun avatar Jan 05 '22 03:01 liunliun