public void ReceiveCb(IAsyncResult ar) { TCP conn = (TCP)ar.AsyncState; if (conn.isUse == false) { return; } try { int count = conn.socket.EndReceive(ar); if (count <= 0) { Console.WriteLine("Received [" + conn.GetAddress() + "] Disconnection"); NMC.instance.CloseTCP(conn); return; } conn.buffercount += count; ProcessData(conn); if (conn.isUse && conn.socket != null) { conn.socket.BeginReceive(conn.readbuffer, conn.buffercount, conn.BuffRmain(), SocketFlags.None, ReceiveCb, conn); } } catch (Exception e) { NMC.instance.CloseTCP(conn); Console.WriteLine("【Abnormal】 ReceiveCb " + e.Message); } }
private void AcceptCb(IAsyncResult ar) { try { Socket sock = listenfd.EndAccept(ar); int index = NewIndex(); tcps[index] = new TCP(); if (index < 0) { sock.Close(); Console.WriteLine("【Warning】The Maximum number of connections has been reached"); } else { TCP conn = tcps[index]; conn.Init(sock); string adr = conn.GetAddress(); Console.WriteLine("Client connection [" + adr + "] ConnectionPool ConnectionID= " + index); conn.socket.BeginReceive(conn.readbuffer, conn.buffercount, conn.BuffRmain(), SocketFlags.None, ReceiveCb, conn); listenfd.BeginAccept(AcceptCb, null); } } catch (Exception e) { Console.WriteLine("AcceptCb Defeated" + e.Message); } }