Exemple #1
0
        private void ReceiveEvent(TcpSocketAsync client, byte[] data, int length)
        {
            data = OnDecrypt(data);
            MsgData msg = MsgData.Deserialize(data);

            if (msg != null)
            {
                ThreadPool.QueueUserWorkItem((o) =>
                {
                    MsgData _msg = o as MsgData;
                    if (_msg.Id != 0)
                    {
                        var callModel = this.GetMsgIdCall(_msg.Id);
                        if (callModel != null)
                        {
                            this.OnMsgIdCall(_msg, callModel);
                            return;
                        }
                    }

                    var call = this.GetCmdCall(_msg.Cmd);
                    this.OnMsgCmdCall(_msg, call);
                }, msg);
            }
        }
Exemple #2
0
        private void ExecCall(CmdMethodInfo cmdMethodInfo, TcpSocketAsync client, MsgData msg)
        {
            var session = client.UserState as Session;

            this.OnCmdExecutingEvent(session, msg);
            if (cmdMethodInfo != null && client != null)
            {
                ActionResult result = null;

                using (var controller = Activator.CreateInstance(cmdMethodInfo.Type) as Controller)
                {
                    try
                    {
                        controller.Init(session, msg);
                        if (this.OnAuth(cmdMethodInfo, session, msg, out result))
                        {
                            controller.OnExecuting();
                            result = cmdMethodInfo.Method.Invoke(controller, null) as ActionResult;
                            controller.OnResult(result);
                            this.OnCmdExecutedEvent(session, msg, result.Result);
                        }
                    }
                    catch (Exception ex)
                    {
                        bool ishand = false;
                        try
                        {
                            ExceptionContext excontext = new ExceptionContext()
                            {
                                Msg = msg, IsHandle = false, Exception = ex
                            };
                            controller.OnException(excontext);
                            ishand = excontext.IsHandle;
                            result = excontext.Result;
                        }
                        catch (Exception _ex)
                        {
                            this.OnMvcHostServerErrorEvent(_ex);
                        }
                        if (!ishand)
                        {
                            this.OnMvcHostServerErrorEvent(ex);
                        }
                        if (result == null)
                        {
                            result = new ActionResult();
                            result.SetMsg(MsgStatus.ServerError, "服务器发生错误!");
                        }
                    }
                }


                if (result != null && client.IsConnected)
                {
                    result.Result.Cmd = msg.Cmd;
                    result.Result.Id  = msg.Id;
                    this.SendMsg(result.Result, session);
                }
            }
        }
Exemple #3
0
        private void OnClientReceive(TcpSocketAsync client, byte[] data, int length)
        {
            byte[]  buffer = this.OnDecrypt(data);
            MsgData msg    = MsgData.Deserialize(buffer);

            if (msg != null)
            {
                try
                {
                    if (cmdCallbackDic.ContainsKey(msg.Cmd))
                    {
                        var callModel = cmdCallbackDic[msg.Cmd];
                        this.ExecCall(callModel, client, msg);
                        return;
                    }
                    else
                    {
                        msg.Rest();
                        msg.Status = MsgStatus.Unknown;
                        this.SendMsg(msg, client.UserState as Session);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    this.OnMvcHostServerErrorEvent(ex);
                }
            }
            else
            {
                this.OnMvcHostServerErrorEvent(new Exception(string.Format("client({0}): msg is null!", client.RemoteEndPoint.ToString())));
            }
        }
Exemple #4
0
 /// <summary>
 /// 初始化
 /// </summary>
 public MsgClient()
 {
     this.tcpClient = new TcpSocketAsync();
     this.tcpClient.AsyncConnectEvent += OnAsyncConnectEvent;
     this.tcpClient.ErrorEvent        += OnErrorEvent;
     this.tcpClient.ReceiveEvent      += ReceiveEvent;
     this.tcpClient.ReadingEvent      += ReadingEvent;
 }
Exemple #5
0
        private void OnAsyncConnectEvent(TcpSocketAsync client, bool isSuccess)
        {
            if (isSuccess)
            {
                this.tcpClient.SetKeepAlive(10000, 10000);
            }

            this.OnConnectCall(isSuccess);
        }
Exemple #6
0
        private void OnClientReadingEvent(TcpSocketAsync client, int position, int length)
        {
            Session session = client.UserState as Session;

            if (session != null)
            {
                session.LastReceiveTime = DateTime.Now;
            }
        }
Exemple #7
0
        /// <summary>
        /// 监听到客户端连接
        /// </summary>
        /// <param name="client"></param>
        protected virtual void OnAccept(TcpSocketAsync client)
        {
            Session session = new Session(client);

            session.SendCall  = this.SendMsg;
            session.CloseCall = this.CloseClient;
            client.UserState  = session;
            this.OnClientConnected(session);
        }
Exemple #8
0
        private void OnClientClosed(TcpSocketAsync client, Exception ex)
        {
            Session session = client.UserState as Session;

            this.OnClientClosedEvent(session);
            session.SendCall  = null;
            session.CloseCall = null;
            client.Dispose();
            session.Dispose();
        }
Exemple #9
0
 /// <summary>
 /// Dispose
 /// </summary>
 public virtual void Dispose()
 {
     if (!this.isDisposed)
     {
         this.isDisposed = true;
         this.Reset();
         this.MsgCmdCallDic.Clear();
         this.MsgIdCallDic.Clear();
         this.tcpClient.Dispose();
         this.MsgCmdCallDic = null;
         this.MsgIdCallDic  = null;
         this.tcpClient     = null;
     }
 }
Exemple #10
0
        internal Session(TcpSocketAsync client)
        {
            client.SetKeepAlive(15 * 1000, 15 * 1000);
            string address = client.RemoteEndPoint.ToString();
            int    index   = address.IndexOf(':');

            if (index > 0)
            {
                address = address.Substring(0, index);
            }

            this.Client          = client;
            this.Sid             = Guid.NewGuid().ToString("n");
            this.Address         = address;
            this.dic             = new Dictionary <string, object>();
            this.LastReceiveTime = DateTime.Now;
        }
Exemple #11
0
 private void OnErrorEvent(TcpSocketAsync client, Exception ex)
 {
     this.Close();
     this.OnClosedCall(ex);
 }
Exemple #12
0
 private void ReadingEvent(TcpSocketAsync client, int position, int length)
 {
     this.OnReadingCall(position, length);
 }