Example #1
0
        //客户端处理服务
        private void ClientServer(object obj)
        {
            TcpClient tcpClient = (TcpClient)obj;

            tcpClient.ReceiveTimeout = 1000;
            bool bVerify = false;

            ClientInfo clientInfo = new ClientInfo(tcpClient);

            Byte[] recvPacket = new Byte[1024];
            int    nErr       = 0;
            int    nDataLen   = 0;

            while (true)
            {
                try
                {
                    Array.Clear(recvPacket, 0, 1024);
                    nDataLen = 0;
                    nErr     = RecvPacket(clientInfo.Client, recvPacket, ref nDataLen);

                    if (nDataLen > 0)
                    {
                        try
                        {
                        }
                        catch (Exception ex)
                        {
                            //Log.ErrLog(string.Format("解析报文出错, {0}", ex.Message));
                            Log.RecvLog("Error", "解析报文出错", ex.Message);
                        }
                    }
                    if (0 == nErr)
                    {
                        try
                        {
                            TcpClientProtocol.ParseData(ref clientInfo, recvPacket, nDataLen);
                        }
                        catch (Exception ex)
                        {
                            //Log.ErrLog(string.Format("解析报文出错, {0}", MyHelper.ByteArray2HexStr(recvPacket, nDataLen)));
                            //Log.ErrLog(ex.Message);
                            Log.RecvLog("Error", "解析报文出错", MyHelper.ByteArray2HexStr(recvPacket, nDataLen));
                            Log.RecvLog("Error", "Error", ex.Message);
                        }
                        clientInfo.RecvTime = DateTime.Now;
                    }
                    else if (1 == nErr)
                    {
                        clientInfo.CloseClient();
                        break;
                    }
                    else if (2 == nErr)
                    {
                    }
                    else if (3 == nErr)
                    {
                        // 超时处理
                        if (clientInfo.Verified)
                        {
                            if (clientInfo.RecvTime.AddMinutes(15) < DateTime.Now)
                            {
                                String str = string.Format("{0}时连接上来的{1}客户端已超时断开", clientInfo.ConnectTime.ToString("yyyy-MM-dd HH:mm:ss"), clientInfo.Client.RemoteEndPoint.ToString());
                                Log.RecvLog("Error", "Error", str);
                                clientInfo.CloseClient();
                                break;
                            }
                            else
                            {
                            }
                        }
                        else
                        {
                            if (clientInfo.VerifiedTimeout())
                            {
                                // 未经身份验证
                                if (bVerify)
                                {
                                    String str = string.Format("{0}时连接上来的{1}客户端失去心跳响应", clientInfo.ConnectTime.ToString("yyyy-MM-dd HH:mm:ss"), clientInfo.Sim);
                                    Log.RecvLog("Error", "Error", str);
                                }
                                else
                                {
                                    String str = string.Format("{0}时连接上来的{1}是非授权客户端", clientInfo.ConnectTime.ToString("yyyy-MM-dd HH:mm:ss"), clientInfo.Client.RemoteEndPoint.ToString());
                                    Log.RecvLog("Error", "Error", str);
                                }
                                clientInfo.CloseClient();
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string strErr = ex.Message;
                    if (clientInfo != null)
                    {
                        if (clientInfo.Client != null)
                        {
                            String str = string.Format("{0}时连接上来的{1}客户端异常断开,{2}", clientInfo.ConnectTime.ToString("yyyy-MM-dd HH:mm:ss"), clientInfo.Client.RemoteEndPoint.ToString(), strErr);
                            Log.RecvLog("Error", "Error", str);
                        }
                        else
                        {
                            String str = string.Format("{0}时连接上来的{1}客户端异常断开,{2}", clientInfo.ConnectTime.ToString("yyyy-MM-dd HH:mm:ss"), clientInfo.Sim, strErr);
                            Log.RecvLog("Error", "Error", ex.Message);
                        }
                        clientInfo.CloseClient();
                    }
                    else
                    {
                    }
                    break;
                }
            }

            ClientInfoCommon.RemoveClientInfo(clientInfo);
        }