//=================================================================================================================== //=================================================================================================================== //= CALLBACK METHODS = //=================================================================================================================== //=================================================================================================================== #region -=[- CALLBACK METHODS -]=- /// <summary> /// Gets called when a connection is established /// </summary> /// <param name="AR">IAsyncResult</param> private void AcceptCallback(IAsyncResult AR) { Socket socket; try { socket = serverSocket.EndAccept(AR); } catch (ObjectDisposedException) { return; } LClientList.Add(socket); socket.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, ReceiveCallback, socket); Debug("New client connected!", DebugParams); SendToClient(socket, new NCILib.PreAuth(this)); serverSocket.BeginAccept(AcceptCallback, null); }
/// <summary> /// Gets called when a message is received /// </summary> /// <param name="AR">IAsyncResult</param> private void ReceiveCallback(IAsyncResult AR) { Socket current = (Socket)AR.AsyncState; int received; try { received = current.EndReceive(AR); } catch (SocketException) { Debug("Client forcefully disconnected.", DebugParams); // Don't shutdown because the socket may be disposed and its disconnected anyway. current.Close(); LClientList.RemoveAt(current); return; } byte[] recBuf = new byte[received]; Array.Copy(Buffer, recBuf, received); string text = Encoding.UTF8.GetString(recBuf); Console.ForegroundColor = ConsoleColor.Cyan; Debug("Received message: " + text, DebugParams); Console.ForegroundColor = ConsoleColor.White; NetComInstruction[] instructionList = NetComInstruction.Parse(this, text).ToArray(); foreach (NetComInstruction instr in instructionList) { IncommingInstructions.Add(instr, LClientList[current]); } current.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, ReceiveCallback, current); }