NetSimplifyClient.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using HslCommunication.Core.Net;
  6. using HslCommunication.Core.IMessage;
  7. using HslCommunication.Core;
  8. namespace HslCommunication.Enthernet
  9. {
  10. /// <summary>
  11. /// 异步访问数据的客户端类,用于向服务器请求一些确定的数据信息
  12. /// </summary>
  13. public class NetSimplifyClient : NetworkDoubleBase<HslMessage, RegularByteTransform>
  14. {
  15. #region Constructor
  16. /// <summary>
  17. /// 实例化一个客户端的对象,用于和服务器通信
  18. /// </summary>
  19. public NetSimplifyClient( string ipAddress, int port )
  20. {
  21. IpAddress = ipAddress;
  22. Port = port;
  23. }
  24. /// <summary>
  25. /// 实例化一个客户端对象,需要手动指定Ip地址和端口
  26. /// </summary>
  27. public NetSimplifyClient( )
  28. {
  29. }
  30. #endregion
  31. /// <summary>
  32. /// 客户端向服务器进行请求,请求字符串数据
  33. /// </summary>
  34. /// <param name="customer">用户的指令头</param>
  35. /// <param name="send">发送数据</param>
  36. /// <returns></returns>
  37. public OperateResult<string> ReadFromServer(NetHandle customer,string send = null)
  38. {
  39. var result = new OperateResult<string>( );
  40. var data = string.IsNullOrEmpty( send ) ? new byte[0] : Encoding.Unicode.GetBytes( send );
  41. var temp = ReadFromServerBase( HslProtocol.ProtocolUserString, customer, data);
  42. result.IsSuccess = temp.IsSuccess;
  43. result.ErrorCode = temp.ErrorCode;
  44. result.Message = temp.Message;
  45. if (temp.IsSuccess)
  46. {
  47. result.Content = Encoding.Unicode.GetString( temp.Content );
  48. }
  49. temp = null;
  50. return result;
  51. }
  52. /// <summary>
  53. /// 客户端向服务器进行请求,请求字节数据
  54. /// </summary>
  55. /// <param name="customer">用户的指令头</param>
  56. /// <param name="send">发送的字节内容</param>
  57. /// <returns></returns>
  58. public OperateResult<byte[]> ReadFromServer(NetHandle customer,byte[] send)
  59. {
  60. return ReadFromServerBase( HslProtocol.ProtocolUserBytes, customer, send);
  61. }
  62. /// <summary>
  63. /// 需要发送的底层数据
  64. /// </summary>
  65. /// <param name="headcode">数据的指令头</param>
  66. /// <param name="customer">用户的指令头</param>
  67. /// <param name="send">需要发送的底层数据</param>
  68. /// <returns></returns>
  69. private OperateResult<byte[]> ReadFromServerBase(int headcode,int customer,byte[] send)
  70. {
  71. var read = ReadFromCoreServer( HslProtocol.CommandBytes( headcode, customer, Token, send ) );
  72. if(!read.IsSuccess)
  73. {
  74. return read;
  75. }
  76. byte[] headBytes = new byte[HslProtocol.HeadByteLength];
  77. byte[] contentBytes = new byte[read.Content.Length - HslProtocol.HeadByteLength];
  78. Array.Copy( read.Content, 0, headBytes, 0, HslProtocol.HeadByteLength );
  79. if(contentBytes.Length>0)
  80. {
  81. Array.Copy( read.Content, HslProtocol.HeadByteLength, contentBytes, 0, read.Content.Length - HslProtocol.HeadByteLength );
  82. }
  83. contentBytes = HslProtocol.CommandAnalysis( headBytes, contentBytes );
  84. return OperateResult.CreateSuccessResult( contentBytes );
  85. }
  86. #region Object Override
  87. /// <summary>
  88. /// 获取本对象的字符串表示形式
  89. /// </summary>
  90. /// <returns></returns>
  91. public override string ToString()
  92. {
  93. return base.ToString( );
  94. }
  95. #endregion
  96. }
  97. }