private void ReceiveNextInstruction() { Buffer = new byte[BufferSize]; int received = ClientSocket.Receive(Buffer, SocketFlags.None); if (received == 0) { return; } byte[] data = new byte[received]; Array.Copy(Buffer, data, received); string text = Encoding.UTF8.GetString(data); Console.ForegroundColor = ConsoleColor.Green; Debug("Received Message: " + text, DebugParams); Console.ForegroundColor = ConsoleColor.White; NetComInstruction[] instructionList = NetComInstruction.Parse(this, text).ToArray(); foreach (NetComInstruction instr in instructionList) { IncommingInstructions.Add(instr, null); } }
private void ProcessNextQueueItem() { if (IncommingInstructions.Count > 0) { Debug($"Processing next instruction...: {IncommingInstructions[0].Key}", DebugParams); Debug(LibraryExec(ParseMessage(IncommingInstructions[0].Key), IncommingInstructions[0].Value)[0].ToString(), DebugParams); IncommingInstructions.RemoveAt(0); } }
private void ProcessNextInstruction() { if (IncommingInstructions.Count > 0) { IncommingInstructions[0].Instruction.Execute(); IncommingInstructions.RemoveAt(0); } // TODO // Check if User is Authenticated // Else Reply to Client with the same message as sent, // and the error that the user is not authenticated }
/// <summary> /// Gets called when a message is received /// </summary> /// <param name="AR"></param> private void OnClientReceive(IAsyncResult AR) { Socket clientSocket = (Socket)AR.AsyncState; int received = clientSocket.EndReceive(AR); byte[] tmpBuffer = new byte[received]; Array.Copy(Buffer, tmpBuffer, received); Debug($"Incomming instruction from {clientSocket.GetHashCode()}: [{Encoding.ASCII.GetString(tmpBuffer)}]", DebugParams); IncommingInstructions.Add(Encoding.ASCII.GetString(tmpBuffer), clientSocket); Start(); }
private void ListenCycle() { while (true) { Debug("Start Listening...", DebugParams); byte[] recBuffer = new byte[BufferSize]; int rec = ClientSocket.Receive(recBuffer); byte[] data = new byte[rec]; Array.Copy(recBuffer, data, rec); IncommingInstructions.Add(Encoding.ASCII.GetString(data), null); Debug("Received: " + Encoding.ASCII.GetString(data), DebugParams); } }
/// <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); }