| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 | using DocumentFormat.OpenXml.Spreadsheet;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;namespace MECF.Framework.Common.Net{    public class JetMessageTcpClient<TNetMessage,T> : JetTcpClient where TNetMessage : INetMessage<T>,new () where T:new ()    {        #region 内部变量        private object _locker = new object();        private int _lockTimeout = 1000;        private TNetMessage _netMessage;        #endregion        /// <summary>        /// 构造函数        /// </summary>        /// <param name="ip"></param>        /// <param name="port"></param>        public JetMessageTcpClient(string ip, int port) : base(ip, port)        {            _netMessage = new TNetMessage();        }        /// <summary>        /// 申请数据        /// </summary>        /// <typeparam name="T">申请指令类型</typeparam>        /// <param name="data">指令对象</param>        /// <returns>返回数据对象</returns>        public NetResult<T> ApplyData(T data)        {            if(Monitor.TryEnter(_locker,_lockTimeout))            {                NetResult result = ReadFromServer(data);                if(!result.IsSuccess)                {                    Monitor.Exit(_locker);                    return NetResult.CreateFailedResult<T>(result.ErrorCode, result.Message);                }                Monitor.Exit(_locker);                return NetResult.CreateSuccessResult<T>(_netMessage.Decode());            }            else            {                return NetResult.CreateFailedResult<T>(NetErrorCode.GetLockTimeout);            }        }        /// <summary>        /// 设置数据        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="data"></param>        /// <returns></returns>        public NetResult SetData(T data)        {            if (Monitor.TryEnter(_locker, _lockTimeout))            {                NetResult result = ReadFromServer(data);                if (!result.IsSuccess)                {                    Monitor.Exit(_locker);                    return NetResult.CreateFailedResult(result.ErrorCode, result.Message);                }                bool confirmResult = _netMessage.ConfirmResponseResult();                if(!confirmResult)                {                    Monitor.Exit(_locker);                    return NetResult.CreateFailedResult(_netMessage.ErrorCode, _netMessage.ErrorMsg);                }                Monitor.Exit(_locker);                return NetResult.CreateSuccessResult();            }            else            {                return NetResult.CreateFailedResult(NetErrorCode.GetLockTimeout);            }        }        /// <summary>        /// 从服务端读取数据        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="data"></param>        /// <returns></returns>        private NetResult ReadFromServer(T data)        {            byte[] buffer = _netMessage.Code(data);            NetResult sendResult = Send(buffer);            if (!sendResult.IsSuccess)            {                return NetResult.CreateFailedResult(sendResult.ErrorCode, sendResult.Message);            }            _netMessage.SendBytes = buffer;            NetResult<byte[]> headerResult = Receive(_netMessage.ProtocolHeadBytesLength);            if (!headerResult.IsSuccess)            {                return NetResult.CreateFailedResult(headerResult.ErrorCode, headerResult.Message);            }            _netMessage.HeadBytes = headerResult.Data;            if (!_netMessage.CheckHeadBytesLegal())            {                return NetResult.CreateFailedResult(NetErrorCode.InvalidHeader);            }            int contentLength = _netMessage.GetContentLengthByHeadBytes();            if (contentLength !=0)            {                NetResult<byte[]> contentResult = Receive(contentLength);                if (!contentResult.IsSuccess)                {                    return NetResult.CreateFailedResult(contentResult.ErrorCode, contentResult.Message);                }                _netMessage.ContentBytes = contentResult.Data;                bool dataValid = _netMessage.CheckDataLegal();                if (!dataValid)                {                    return NetResult.CreateFailedResult(_netMessage.ErrorCode, _netMessage.ErrorMsg);                }            }            return NetResult.CreateSuccessResult();        }    }}
 |