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); } }
public void BroadcastRSA(NetComInstruction pInstruction) { foreach (NetComClientData client in LClientList) { Debug($"Queueing message for {client.Username}: {pInstruction}", DebugParams); OutgoingInstructions.AddRSA(pInstruction, client); } }
public void SendToClientRSA(NetComClientData pClient, NetComInstruction pInstruction) { if (pClient != null) { Debug($"Queueing message for {pClient.Username}: {pInstruction}", DebugParams); OutgoingInstructions.AddRSA(pInstruction, pClient); } }
public void Broadcast(NetComInstruction pInstruction) { foreach (NetComClientData client in LClientList) { byte[] data; if (pInstruction.RSAEncrypted && pInstruction.Client.PublicKey != null) { data = Encoding.UTF8.GetBytes(pInstruction.Encode(true, pInstruction.Client.PublicKey)); } else { data = Encoding.UTF8.GetBytes(pInstruction.Encode(false)); } client.Send(data); Debug($"Sent Message to {pInstruction.Username}: {pInstruction.Instruction}.", DebugParams); } }
public void SendToClient(NetComClientData pClient, NetComInstruction pInstruction) { if (pClient != null) { byte[] data; if (pInstruction.RSAEncrypted && pInstruction.Client.PublicKey != null) { data = Encoding.UTF8.GetBytes(pInstruction.Encode(true, pInstruction.Client.PublicKey)); } else { data = Encoding.UTF8.GetBytes(pInstruction.Encode(false)); } pClient.Send(data); Debug($"Sent Message to {pInstruction.Username}: {pInstruction.Instruction}.", 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); }
public void SendToClientRSA(int pIndex, NetComInstruction pInstruction) => SendToClientRSA(LClientList[pIndex], pInstruction);
public void SendToClientRSA(string pUsername, NetComInstruction pInstruction) => SendToClientRSA(LClientList[pUsername], pInstruction);
//------------------------------------------------------------------------------------------------------------------- //- SENDING (ENCRYPTED) - //------------------------------------------------------------------------------------------------------------------- #region -=[- SENDING ENCRYPTED -]=- public void SendToClientRSA(Socket pSocket, NetComInstruction pInstruction) => SendToClientRSA(LClientList[pSocket], pInstruction);
public void SendRSA(NetComInstruction pInstruction) { OutgoingInstructions.AddRSA(pInstruction, null); }
public NetComInstructionQueueElement(NetComInstruction pInstruction, NetComClientData pClient, bool pRSAEncrypted) { Instruction = pInstruction; Client = pClient; RSAEncrypted = pRSAEncrypted; }
public NetComInstructionQueueElement(NetComInstruction pInstruction, NetComClientData pClient) : this(pInstruction, pClient, false) { }