Example #1
0
        protected void ReceiveCallBack(IAsyncResult ar)
        {
            Connection con = (Connection)ar.AsyncState;

            if (!con.ActiveCheck())
            {
                return;
            }
            try
            {
                lock (con)
                {
                    int length = con.socket.EndReceive(ar);
                    ServerLog.LogClient("receive:" + length, 1, con.clientId);
                    if (length <= 0)
                    {
                        ServerLog.LogClient("客户端断开连接:" + ClientPool[con.clientId].socket.LocalEndPoint + "ClientID:" + con.clientId, 0, con.clientId);

                        con.socket.Close();
                        ClientPool.Recover(con.clientId);

                        return;
                    }
                    con.length += length;
                    //Console.WriteLine("bool isNew = bufferList[con]:" + ClientPool[con.clientId].socket.LocalEndPoint + "ClientID:" + con.clientId);
                    //bool isNew = bufferList[con] == null;
                    ProcessData(con);

                    //{
                    //    //MessageList.Push(message);

                    //    bufferList[con] = null;
                    //}
                    //else
                    //{
                    //    bufferList[con] = message;
                    ////}
                    //Console.WriteLine("接收信息:" + message.Length + "ClientID:" + con.clientId);



                    con.socket.BeginReceive(con.readBuff, con.length, con.BuffRemain, SocketFlags.None, ReceiveCallBack, con);
                }
            }
            catch (Exception)
            {
                ServerLog.LogClient("客户端异常终止连接:" + ClientPool[con.clientId].socket.LocalEndPoint + "ClientID:" + con.clientId, 0, con.clientId);

                con.socket.Close();
                ClientPool.Recover(con.clientId);
                //throw;
            }
        }
        protected void ReceiveCallBack(IAsyncResult ar)
        {
            Connection con = (Connection)ar.AsyncState;

            if (!con.ActiveCheck())
            {
                return;
            }

            try
            {
                lock (con)
                {
                    int length = con.socket.EndReceive(ar);
                    ServerLog.LogClient("receive:" + length, 1, con.clientId);
                    if (length <= 0)
                    {
                        ServerLog.LogClient("客户端断开连接:" + ClientPool[con.clientId].socket.LocalEndPoint + "ClientID:" + con.clientId, 0, con.clientId);
                        con.socket.Close();
                        ClientPool.Recover(con.clientId);
                        return;
                    }

                    con.length += length;

                    ProcessData(con);

                    con.socket.BeginReceive(con.readBuff, con.length, con.BuffRemain, SocketFlags.None, ReceiveCallBack, con);
                }
            }
            catch (Exception)
            {
                ServerLog.LogClient("客户端异常终止连接:" + ClientPool[con.clientId].socket.LocalEndPoint + "ClientID:" + con.clientId, 0, con.clientId);

                con.socket.Close();
                ClientPool.Recover(con.clientId);
            }
        }