P1Client.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using LJLib.Net.SPI.Client;
  2. using LJLib.Tools.Helper;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.IO.Compression;
  8. using System.Text;
  9. using System.Threading;
  10. namespace LJLib.Client
  11. {
  12. public sealed class P1Client : ILJClient
  13. {
  14. private const byte NEW_ALLGZIP = 0x10;
  15. private const byte SVRBEAT = 0x08;
  16. private const byte PROTOCOL_FLAG = 0x04;
  17. private const byte READ_GZIP = 0x02;
  18. private const byte WRITE_GZIP = 0x01;
  19. private Stream _stream = null;
  20. private object syncRoot = new object();
  21. public P1Client(Stream stream)
  22. {
  23. _stream = stream;
  24. }
  25. public string DoExcute(string apiName, string requestJSON)
  26. {
  27. lock (syncRoot)
  28. {
  29. var apiname = apiName;
  30. var firstByte = new[]
  31. {
  32. (byte)(SVRBEAT | PROTOCOL_FLAG | READ_GZIP | WRITE_GZIP | NEW_ALLGZIP),
  33. (byte)(SVRBEAT | PROTOCOL_FLAG | READ_GZIP | WRITE_GZIP),
  34. (byte)(SVRBEAT | PROTOCOL_FLAG),
  35. };
  36. var headByte = firstByte[0];
  37. if (headByte > 0x1F || (headByte & PROTOCOL_FLAG) != PROTOCOL_FLAG)
  38. {
  39. throw new Exception("不支持协议" + headByte.ToString("X2"));
  40. }
  41. if ((headByte & NEW_ALLGZIP) == NEW_ALLGZIP)
  42. {
  43. if ((headByte & READ_GZIP) != READ_GZIP)
  44. {
  45. throw new Exception("协议异常:新协议必须压缩发送");
  46. }
  47. if ((headByte & WRITE_GZIP) != WRITE_GZIP)
  48. {
  49. throw new Exception("协议异常:新协议必须压缩返回");
  50. }
  51. }
  52. if (!_stream.CanWrite)
  53. {
  54. throw new Exception(string.Format("远端服务已中断(不能写)"));
  55. }
  56. _stream.Write(firstByte, 0, 1);
  57. var ostream = _stream;
  58. if ((headByte & READ_GZIP) == READ_GZIP)
  59. {
  60. ostream = new MemoryStream();
  61. }
  62. string str = apiname + requestJSON;
  63. WriteStringAndFiles(ostream, str, null, null);
  64. if ((headByte & READ_GZIP) == READ_GZIP)
  65. {
  66. using (ostream)
  67. using (var gzipms = new MemoryStream())
  68. using (var gzip = new GZipStream(gzipms, CompressionMode.Compress, true))
  69. {
  70. ostream.Seek(0, SeekOrigin.Begin);
  71. StreamHelper.StreamCopy(gzip, ostream);
  72. gzip.Close();
  73. if ((headByte & NEW_ALLGZIP) == NEW_ALLGZIP)
  74. {
  75. var filelen = BitConverter.GetBytes((int)gzipms.Length);
  76. _stream.Write(filelen, 0, filelen.Length);
  77. }
  78. gzipms.Seek(0, SeekOrigin.Begin);
  79. StreamHelper.StreamCopy(_stream, gzipms);
  80. }
  81. }
  82. if (!_stream.CanRead)
  83. {
  84. throw new Exception(string.Format("远端服务已中断(不能读)"));
  85. }
  86. if ((headByte & SVRBEAT) == SVRBEAT)
  87. {
  88. var oldTimeout = _stream.ReadTimeout;
  89. _stream.ReadTimeout = 10000;
  90. var hasRslt = _stream.ReadByte();
  91. while (hasRslt == 0)
  92. {
  93. Thread.Sleep(100);
  94. if (!_stream.CanRead)
  95. {
  96. throw new Exception(string.Format("远端服务已中断(不能读)"));
  97. }
  98. hasRslt = _stream.ReadByte();
  99. if (hasRslt == -1)
  100. {
  101. throw new Exception(string.Format("远端服务已中断(不能读)"));
  102. }
  103. }
  104. _stream.ReadTimeout = oldTimeout;
  105. }
  106. Debug.Write("WaitServer End");
  107. var istream = _stream;
  108. MemoryStream ms = null;
  109. GZipStream gzipstream = null;
  110. if ((headByte & WRITE_GZIP) == WRITE_GZIP)
  111. {
  112. if ((headByte & NEW_ALLGZIP) == NEW_ALLGZIP)
  113. {
  114. ms = new MemoryStream();
  115. var allen = new byte[4];
  116. _stream.Read(allen, 0, allen.Length);
  117. var alen = BitConverter.ToInt32(allen, 0);
  118. StreamHelper.StreamCopy(ms, _stream, alen);
  119. Debug.Write("ReadResponseFromNet End");
  120. ms.Seek(0, SeekOrigin.Begin);
  121. gzipstream = new GZipStream(ms, CompressionMode.Decompress, false);
  122. istream = gzipstream;
  123. }
  124. else
  125. {
  126. gzipstream = new GZipStream(_stream, CompressionMode.Decompress, true);
  127. istream = gzipstream;
  128. }
  129. }
  130. byte[] bytes;
  131. Dictionary<string, byte[]> rfiles = new Dictionary<string, byte[]>();
  132. using (ms)
  133. using (gzipstream)
  134. {
  135. var reader = new BinaryReader(istream);
  136. var len = reader.ReadInt32();
  137. bytes = reader.ReadBytes(len);
  138. var filecnt = reader.ReadByte();
  139. for (int i = 0; i < filecnt; i++)
  140. {
  141. var namelen = reader.ReadInt32();
  142. var namebytes = reader.ReadBytes(namelen);
  143. var datalen = reader.ReadInt32();
  144. var databytes = reader.ReadBytes(datalen);
  145. rfiles.Add(Encoding.UTF8.GetString(namebytes), databytes);
  146. }
  147. Debug.Write("ReadResponse End");
  148. }
  149. var strrsp = Encoding.UTF8.GetString(bytes);
  150. return strrsp;
  151. }
  152. }
  153. private static void WriteStringAndFiles(Stream ostream, string str, IDictionary<string, string> files, Dictionary<string, byte[]> exfiles)
  154. {
  155. var strbytes = Encoding.UTF8.GetBytes(str);
  156. var lenbytes = BitConverter.GetBytes(strbytes.Length);
  157. ostream.Write(lenbytes, 0, lenbytes.Length);
  158. ostream.Write(strbytes, 0, strbytes.Length);
  159. ostream.Flush();
  160. if (files == null)
  161. {
  162. files = new Dictionary<string, string>();
  163. }
  164. if (exfiles == null)
  165. {
  166. exfiles = new Dictionary<string, byte[]>();
  167. }
  168. var shortbytes = BitConverter.GetBytes((short)(files.Count + exfiles.Count));
  169. ostream.Write(shortbytes, 0, shortbytes.Length);
  170. foreach (var file in files)
  171. {
  172. var namebytes = Encoding.UTF8.GetBytes(file.Key);
  173. var namelen = BitConverter.GetBytes(namebytes.Length);
  174. ostream.Write(namelen, 0, namelen.Length);
  175. ostream.Write(namebytes, 0, namebytes.Length);
  176. ostream.Flush();
  177. using (var fs = File.OpenRead(file.Value))
  178. {
  179. var filelen = BitConverter.GetBytes((int)fs.Length);
  180. ostream.Write(filelen, 0, filelen.Length);
  181. byte[] buff = new byte[10240];
  182. int read = 0;
  183. while ((read = fs.Read(buff, 0, buff.Length)) > 0)
  184. {
  185. ostream.Write(buff, 0, read);
  186. ostream.Flush();
  187. }
  188. }
  189. }
  190. foreach (var file in exfiles)
  191. {
  192. var namebytes = Encoding.UTF8.GetBytes(file.Key);
  193. var namelen = BitConverter.GetBytes(namebytes.Length);
  194. ostream.Write(namelen, 0, namelen.Length);
  195. ostream.Write(namebytes, 0, namebytes.Length);
  196. ostream.Flush();
  197. var filelen = BitConverter.GetBytes((int)file.Value.Length);
  198. ostream.Write(filelen, 0, filelen.Length);
  199. using (var ms = new MemoryStream(file.Value))
  200. {
  201. byte[] buff = new byte[10240];
  202. int read = 0;
  203. while ((read = ms.Read(buff, 0, buff.Length)) > 0)
  204. {
  205. ostream.Write(buff, 0, read);
  206. ostream.Flush();
  207. }
  208. }
  209. ostream.Flush();
  210. }
  211. ostream.Flush();
  212. }
  213. public void Beat()
  214. {
  215. lock (syncRoot)
  216. {
  217. _stream.WriteByte(0x00);
  218. }
  219. }
  220. public void Dispose()
  221. {
  222. _stream.Dispose();
  223. }
  224. }
  225. }