// Accept private void AcceptSocket(IAsyncResult State) { try { CommanderObject Accept = new AcceptClient(State); Handler Handle = new Handler(Accept); Client mClient = Handle.Invoke(); //We accepting new connection again ServerSocket.BeginAccept(new AsyncCallback(AcceptSocket), ServerSocket); //Begin Receive in Current Client Stream mClient.SOCKET.BeginReceive(_buff, 0, _buff.Length, SocketFlags.None, new AsyncCallback(ReceiveData), mClient.SOCKET); } catch (Exception Ex) { ConsoleMessage.WriteException(Ex.Message, "Listener.cs", "AcceptSocket"); ServerSocket.BeginAccept(new AsyncCallback(AcceptSocket), ServerSocket); } }
// //TCP STARTER METHOD // void StartListenTCP() { try { // //PERMISSION TRUE/FALSE CONTROL // if (!AcceptClient_Permission) { throw new Exception("Accept Client Permission Is False"); } ServerSocket.Bind(localEP); ServerSocket.Listen(2); SocketStatePrivate = true; ServerSocket.BeginAccept(new AsyncCallback(AcceptSocket), ServerSocket); } catch (Exception Ex) { ConsoleMessage.WriteException(Ex.Message, "Listener.cs", "StartListenTCP"); } }
// Receive private void ReceiveData(IAsyncResult State) { try { if (!ReceiveData_Permission) { throw new Exception("Receive Data Permission Is False"); } CurrentSocket = (Socket)State.AsyncState; int ReceivedInt = CurrentSocket.EndReceive(State); copyBuff = new byte[ReceivedInt]; Array.Copy(_buff, copyBuff, ReceivedInt); String Data = Encoding.UTF8.GetString(copyBuff); _buff = new byte[1024]; if (!string.IsNullOrEmpty(Data)) { DataParser parser = new DataParser(Data, CurrentSocket, m_initializeType); parser.Parse(); Thread reciveTask = new Thread(new ThreadStart(() => { ReceiverTask(Data, GetClientBySocket(CurrentSocket)); })); if (ReceiverTask != null) { reciveTask.Start(); } CurrentSocket.BeginReceive(_buff, 0, _buff.Length, SocketFlags.None, new AsyncCallback(ReceiveData), CurrentSocket); } else { Message.Write(CurrentSocket.RemoteEndPoint.ToString(), ConsoleMessage.MessageType.DISCONNECT); } } catch (Exception Ex) { ConsoleMessage.WriteException(Ex.Message, "Listener.cs", "ReceiveData"); SocketList[FoundSocketID(CurrentSocket)].CLOSEDSTATE = Client.ClosedStates.FORCIBLY; } }