Ejemplo n.º 1
0
        /// <summary>
        /// Recevie的回调函数
        /// </summary>
        /// <param name="ar"></param>
        private void RecevieCb(IAsyncResult ar)
        {
            Conn conn = (Conn)ar.AsyncState;

            lock (conn)
            {
                try
                {
                    //获取接收的字节数
                    int count = conn.socket.EndReceive(ar);
                    //关闭信号
                    if (count <= 0)
                    {
                        WbeService.instance.WriteLine("收到 [" + conn.GetAddress() + "]退出连接");
                        conn.Close();
                        return;
                    }
                    //处理数据
                    conn.buffCount += count;
                    ProcessData(conn);
                    //继续接收
                    conn.socket.BeginReceive(conn.readBuff, conn.buffCount, conn.BufferRemain(), SocketFlags.None, RecevieCb, conn);
                }
                catch (Exception ex)
                {
                    //MessageBox.Show("接收错误: "+ex.Message);
                    WbeService.instance.WriteLine("RecevieCb接收错误: " + ex.Message);
                    //  Remove(conn);
                    conn.Close();
                }
            }
        }
Ejemplo n.º 2
0
    private void ReceiveCB(IAsyncResult ar)
    {
        Conn conn = (Conn)ar.AsyncState;

        lock (conn)
        {
            try
            {
                //  收到的字节数
                int count = conn.socket.EndReceive(ar);

                //  关闭信号
                if (count <= 0)
                {
                    Debug.Log("收到[" + conn.GetAdress() + "]断开连接");
                    conn.Close();
                    connsPool.Recycle(conn);
                }
                else
                {
                    //  数据处理
                    conn.bufferCount += count;
                    ProcessData(conn);

                    //  继续接收
                    conn.socket.BeginReceive(conn.readBuff, conn.bufferCount, conn.BufferRemain(), SocketFlags.None, ReceiveCB, conn);
                }
            }
            catch (Exception ex)
            {
                Debug.Log("收到[" + conn.GetAdress() + "]断开连接" + ex.Message);
                conn.Close();
            }
        }
    }
Ejemplo n.º 3
0
 /// <summary>
 /// Accept回调函数
 /// </summary>
 /// <param name="ar"></param>
 private void AcceptCb(IAsyncResult ar)
 {
     try
     {
         Socket socket = listenfd.EndAccept(ar);
         int    index  = NewIndex();
         if (index < 0)
         {
             WbeService.instance.WriteLine("连接已满");
             socket.Close();
         }
         else
         {
             Conn conn = conns[index];
             conn.Init(socket);
             //异步接收客户端数据
             conn.socket.BeginReceive(conn.readBuff, conn.buffCount, conn.BufferRemain(), SocketFlags.None, RecevieCb, conn);
             WbeService.instance.WriteLine(conn.GetAddress() + "加入连接 " + index);
             if (conn.isWriteDataTable == false)
             {
                 string   str      = conn.GetAddress();
                 string[] strSplit = str.Split(':');
                 DataRow  dr       = dt.NewRow();
                 dr[0] = index;
                 dr[1] = strSplit[0];
                 dr[2] = strSplit[1];
                 dr[3] = conn.time;
                 dt.Rows.Add(dr);
                 conn.isWriteDataTable = true;
             }
         }
     }
     catch (Exception ex)
     {
         WbeService.instance.WriteLine("AcceptCb失败" + ex.Message);
         Console.WriteLine("AcceptCb失败" + ex.Message);
     }
     finally
     {
         //继续异步监听连接
         listenfd.BeginAccept(AcceptCb, null);
     }
 }
Ejemplo n.º 4
0
 private void AcceptCB(IAsyncResult ar)
 {
     try
     {
         Socket socket = serverSocket.EndAccept(ar);
         Conn   conn   = connsPool.YieldConn(socket);
         if (conn == null)
         {
             socket.Close();
             Debug.Log("警告,链接已满");
         }
         else
         {
             Debug.Log("客户端连接 [" + conn.GetAdress() + "] conn池ID:" + conn.index);
             conn.socket.BeginReceive(conn.readBuff, conn.bufferCount, conn.BufferRemain(), SocketFlags.None, ReceiveCB, conn);
             serverSocket.BeginAccept(AcceptCB, null);
         }
     }
     catch (Exception ex)
     {
         Debug.Log("AcceptCB失败:" + ex.Message);
     }
 }