Пример #1
0
        /// <summary>
        /// 处理ACK消息
        /// </summary>
        /// <param name="bufferMessage"></param>
        private void ProcessMessage_ACK(string bufferMessage)
        {
            var responseAckInfo = bufferMessage.ToObject <Message.ResponseAckInfo>();

            if (EnableEncrypt && responseAckInfo.Data != null)
            {
                try
                {
                    dynamic deString = CommonCryptoService.Decrypt_DES(responseAckInfo.Data.ToString(), responseAckInfo.MessageId);
                    responseAckInfo.Data = CommonExt.JsonStringToDynamic(deString.ToString());
                    LogOpInfo("Receive--Decode", responseAckInfo.ToJsonString());
                }
                catch (Exception ex)
                {
                    LogInfo("用户:DeCryptOp异常", ex);
                    //throw ex;//抛出用户异常
                }
            }

            var requestInfo = m_SendBuffer.FirstOrDefault(x => x.Key == responseAckInfo.MessageId).Value;

            if (requestInfo != null)
            {
                MessageContext context = new MessageContext()
                {
                    IMRequest = requestInfo, IMResponse = responseAckInfo
                };
                if (IsFileMessage(requestInfo.MsgType) && requestInfo.Callback == null)
                {//文件类型消息且用户不提供回调自动下载文件
                    ProcessFileMessage(context);
                }
                else if (requestInfo.Callback != null)
                {//非文件类消息收到ack直接回调
                    requestInfo.Callback(requestInfo, responseAckInfo);
                }
                m_SendBuffer.TryRemove(responseAckInfo.MessageId, out requestInfo);
            }
        }
Пример #2
0
        /// <summary>
        /// 处理普通的下行消息
        /// </summary>
        /// <param name="bufferMessage"></param>
        private void ProcessMessage_Response(string bufferMessage)
        {
            var responseInfo = bufferMessage.ToObject <Message.ResponseInfo>();

            if (EnableEncrypt && responseInfo.Data != null)
            {
                try
                {
                    dynamic deString = CommonCryptoService.Decrypt_DES(responseInfo.Data.ToString(), responseInfo.MessageId);
                    responseInfo.Data = CommonExt.JsonStringToDynamic(deString.ToString());
                    LogOpInfo("Receive--Decode", responseInfo.ToJsonString());
                }
                catch (Exception ex)
                {
                    LogInfo("用户:DeCryptOp异常", ex);
                    //throw ex;//抛出用户异常
                }
            }

            if (IsFileMessage(responseInfo.MsgType))
            {
                //用户自己根据url下载,无需处理
            }

            if (OnReceive != null)
            {
                try
                {
                    OnReceive(responseInfo.MsgType, responseInfo.FromId, responseInfo.GroupId, responseInfo.MessageId, responseInfo.MsgTime, responseInfo.Data);
                }
                catch (Exception ex)
                {
                    LogInfo("用户:OnReceive异常", ex);
                    throw ex;//抛出用户异常
                }
            }
        }
Пример #3
0
        /// <summary>
        /// 打开连接
        /// </summary>
        /// <param name="host">服务器地址</param>
        /// <param name="port">服务器端口号</param>
        /// <param name="userType">用户类型</param>
        /// <param name="userName">用户显示的名称</param>
        /// <param name="userAgent">用户登陆的终端信息</param>
        /// <param name="accessToken">访问令牌</param>
        /// <param name="lastTick">收到最后一条消息的UNIX时间戳</param>
        /// <param name="callback">回调函数</param>
        /// <returns>连接是否成功</returns>
        public bool Connect(string host, int port, string userType, string userName, string userAgent, string accessToken, int lastTick, Action <RequestInfo, ResponseAckInfo> callback)
        {
            var requestInfo = new Message.RequestInfo()
            {
                MsgType = MessageType.Connect,
                //MessageId = TimeStamp.Create(),
                Data = new
                {
                    userType    = userType,
                    userName    = userName,
                    userAgent   = userAgent,
                    accessToken = accessToken,
                    lastTick    = lastTick
                },
                Version = Encrypt_Version
            };
            var preRequestInfo = new Message.RequestInfo()
            {
                MsgType = MessageType.Connect,
                Data    = requestInfo.Data,
                Version = Encrypt_Version
            };

            if (IsAvailable)
            {
                //已经连接成功
                if (callback != null)
                {
                    callback(requestInfo, new ResponseAckInfo()
                    {
                        MsgType = MessageType.Ack, Status = ResponseCode.OK
                    });
                }
                return(true);
            }
            bool result = false;

            //开始连接
            try
            {
                m_Client = new Socket(SocketType.Stream, ProtocolType.Tcp);

                if (EnableEncrypt)
                {
                    requestInfo.Data = EncryptAdapter.Encode(CommonExt.DynamicToJsonString(requestInfo.Data), requestInfo);
                }

                LogOpInfo("BeginConnect:", requestInfo.ToJsonString());
                m_Client.Connect(host, port);
                m_IpAddress = host;
                m_Port      = port;
                if (m_Client.Connected)
                {
                    byte[] sendData = requestInfo.ToByte <Message.RequestInfo>();

                    m_Client.Send(sendData);
                    byte[] bufferData   = new byte[BufferSize];
                    int    receiveLen   = m_Client.Receive(bufferData);
                    byte[] receivedData = bufferData.ToList().GetRange(0, receiveLen).ToArray();
                    var    responseInfo = receivedData.ToObject <Message.ResponseAckInfo>();
                    if (EnableEncrypt)
                    {
                        dynamic deString = CommonCryptoService.Decrypt_DES(responseInfo.Data.ToString(), responseInfo.MessageId);
                        responseInfo.Data = CommonExt.JsonStringToDynamic(deString.ToString());
                    }
                    //LogOpInfo("ConnectOver:", responseInfo.ToJsonString());
                    if (responseInfo.Status == ResponseCode.OK)
                    {
                        m_User.IsAuthenticated    = true;
                        m_User.Name               = userName;
                        m_User.Token              = accessToken;
                        m_User.UserAgent          = userAgent;
                        m_User.UserType           = userType;
                        m_User.AuthenticationType = responseInfo.Data.userPermission;
                        m_User.PermissionList     = Permission.GetUserPermission(m_User.AuthenticationType);
                        //连接使用同步
                        // _sendBuffer.Add(requestInfo);
                        m_LastPingTime = DateTime.Now;

                        //启动线程接收
                        AfterConnectedServer();
                        result            = true;
                        m_IsCanConnecting = true;

                        if (this.clientPingMode == PingMode.Initiative)
                        {
                            _lastSendTime = DateTime.Now;
                            PingTimer.Start();
                        }
                    }
                    else
                    {
                        m_Client.Disconnect(true);
                    }

                    RunUserCallback(callback, preRequestInfo, responseInfo);
                }
            }
            catch (Exception ex)
            {
                LogInfo("ConnectError", ex);
                RunUserCallback(callback, preRequestInfo, new ResponseAckInfo()
                {
                    MsgType = MessageType.Ack, Status = ResponseCode.CLINET_ERR, Data = ex.Message
                });
            }

            return(result);
        }