FastStreamTest.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using CommonLang.IO;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace ZeusServerEdgeJSTest
  12. {
  13. class FastStreamComposer
  14. {
  15. private const int LEFT_SHIFT_BITS = 1 << 7;
  16. private MemoryStream stream;
  17. public FastStreamComposer(string uid,byte[] data)
  18. {
  19. byte[] bytesUid = System.Text.UTF8Encoding.UTF8.GetBytes(uid);
  20. int contentSize = 6 + bytesUid.Length + data.Length;
  21. int lengthSize = calLengthSize(contentSize);
  22. this.stream = new MemoryStream(lengthSize + contentSize);
  23. //composer head
  24. writeLength(contentSize, lengthSize);
  25. //协议 head
  26. writeU16((ushort)bytesUid.Length);
  27. writeU32((uint)data.Length);
  28. //uid
  29. writeBytes(bytesUid);
  30. //data
  31. writeBytes(data);
  32. }
  33. public byte[] getBytes()
  34. {
  35. return stream.GetBuffer();
  36. }
  37. public static int calLengthSize(int length)
  38. {
  39. int res = 0;
  40. while (length > 0)
  41. {
  42. length = length >> 7;
  43. res++;
  44. }
  45. return res;
  46. }
  47. public void writeLength(int data,int size)
  48. {
  49. int offset = size - 1, b;
  50. byte[] bytes = new byte[size];
  51. for (; offset >= 0; offset--)
  52. {
  53. b = data % LEFT_SHIFT_BITS;
  54. if (offset < size - 1)
  55. {
  56. b |= 0x80;
  57. }
  58. bytes[offset] = (byte)b;
  59. data = data >> 7;
  60. }
  61. stream.Write(bytes, 0, bytes.Length);
  62. }
  63. public void writeU16(UInt16 value)
  64. {
  65. stream.WriteByte((byte)(value));
  66. stream.WriteByte((byte)(value >> 8));
  67. }
  68. public void writeU32(UInt32 value)
  69. {
  70. stream.WriteByte((byte)(value));
  71. stream.WriteByte((byte)(value >> 8));
  72. stream.WriteByte((byte)(value >> 16));
  73. stream.WriteByte((byte)(value >> 24));
  74. }
  75. public void writeBytes(byte[] value)
  76. {
  77. stream.Write(value, 0, value.Length);
  78. }
  79. }
  80. class FastStreamTest
  81. {
  82. private Socket socket;
  83. public void connect(string host,int port)
  84. {
  85. IPAddress ipAddress = IPAddress.Parse(host);
  86. this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  87. IPEndPoint ie = new IPEndPoint(ipAddress, port);
  88. socket.BeginConnect(ie, new AsyncCallback((result) =>
  89. {
  90. try
  91. {
  92. this.socket.EndConnect(result);
  93. this.socket.NoDelay = true;
  94. for (int i = 1; i <= 3; ++i )
  95. send("rovesky", data);
  96. }
  97. catch (SocketException e)
  98. {
  99. Console.WriteLine(e.Message);
  100. }
  101. finally
  102. {
  103. }
  104. }), this.socket);
  105. }
  106. private byte[] data = new byte[]{1,134,0,0,239,219,3,98};
  107. public void send(string uid, byte[] data)
  108. {
  109. FastStreamComposer composer = new FastStreamComposer(uid, data);
  110. this.socket.Send(composer.getBytes());
  111. // socket.Send()
  112. }
  113. public static void start()
  114. {
  115. FastStreamTest fastStreamTest = new FastStreamTest();
  116. fastStreamTest.connect("127.0.0.1", 3050);
  117. do
  118. {
  119. Thread.Sleep(10);
  120. }
  121. while (true);
  122. }
  123. }
  124. }