123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- using CommonLang.IO;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace ZeusServerEdgeJSTest
- {
- class FastStreamComposer
- {
- private const int LEFT_SHIFT_BITS = 1 << 7;
- private MemoryStream stream;
- public FastStreamComposer(string uid,byte[] data)
- {
- byte[] bytesUid = System.Text.UTF8Encoding.UTF8.GetBytes(uid);
- int contentSize = 6 + bytesUid.Length + data.Length;
- int lengthSize = calLengthSize(contentSize);
- this.stream = new MemoryStream(lengthSize + contentSize);
-
-
- writeLength(contentSize, lengthSize);
-
- writeU16((ushort)bytesUid.Length);
- writeU32((uint)data.Length);
-
- writeBytes(bytesUid);
-
- writeBytes(data);
- }
- public byte[] getBytes()
- {
- return stream.GetBuffer();
- }
- public static int calLengthSize(int length)
- {
- int res = 0;
- while (length > 0)
- {
- length = length >> 7;
- res++;
- }
- return res;
- }
-
- public void writeLength(int data,int size)
- {
- int offset = size - 1, b;
- byte[] bytes = new byte[size];
- for (; offset >= 0; offset--)
- {
- b = data % LEFT_SHIFT_BITS;
- if (offset < size - 1)
- {
- b |= 0x80;
- }
- bytes[offset] = (byte)b;
- data = data >> 7;
- }
- stream.Write(bytes, 0, bytes.Length);
- }
- public void writeU16(UInt16 value)
- {
- stream.WriteByte((byte)(value));
- stream.WriteByte((byte)(value >> 8));
- }
- public void writeU32(UInt32 value)
- {
- stream.WriteByte((byte)(value));
- stream.WriteByte((byte)(value >> 8));
- stream.WriteByte((byte)(value >> 16));
- stream.WriteByte((byte)(value >> 24));
- }
- public void writeBytes(byte[] value)
- {
- stream.Write(value, 0, value.Length);
- }
- }
- class FastStreamTest
- {
- private Socket socket;
- public void connect(string host,int port)
- {
- IPAddress ipAddress = IPAddress.Parse(host);
- this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- IPEndPoint ie = new IPEndPoint(ipAddress, port);
- socket.BeginConnect(ie, new AsyncCallback((result) =>
- {
- try
- {
- this.socket.EndConnect(result);
- this.socket.NoDelay = true;
- for (int i = 1; i <= 3; ++i )
- send("rovesky", data);
- }
- catch (SocketException e)
- {
- Console.WriteLine(e.Message);
- }
- finally
- {
-
- }
- }), this.socket);
- }
- private byte[] data = new byte[]{1,134,0,0,239,219,3,98};
-
-
- public void send(string uid, byte[] data)
- {
- FastStreamComposer composer = new FastStreamComposer(uid, data);
- this.socket.Send(composer.getBytes());
-
-
- }
- public static void start()
- {
- FastStreamTest fastStreamTest = new FastStreamTest();
- fastStreamTest.connect("127.0.0.1", 3050);
- do
- {
- Thread.Sleep(10);
- }
- while (true);
- }
-
- }
- }
|