Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
 public void BroadcastRSA(NetComInstruction pInstruction)
 {
     foreach (NetComClientData client in LClientList)
     {
         Debug($"Queueing message for {client.Username}: {pInstruction}", DebugParams);
         OutgoingInstructions.AddRSA(pInstruction, client);
     }
 }
Ejemplo n.º 3
0
 public void SendToClientRSA(NetComClientData pClient, NetComInstruction pInstruction)
 {
     if (pClient != null)
     {
         Debug($"Queueing message for {pClient.Username}: {pInstruction}", DebugParams);
         OutgoingInstructions.AddRSA(pInstruction, pClient);
     }
 }
Ejemplo n.º 4
0
        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);
            }
        }
Ejemplo n.º 5
0
        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);
            }
        }
Ejemplo n.º 6
0
        /// <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);
        }
Ejemplo n.º 7
0
 public void SendToClientRSA(int pIndex, NetComInstruction pInstruction) => SendToClientRSA(LClientList[pIndex], pInstruction);
Ejemplo n.º 8
0
 public void SendToClientRSA(string pUsername, NetComInstruction pInstruction) => SendToClientRSA(LClientList[pUsername], pInstruction);
Ejemplo n.º 9
0
        //-------------------------------------------------------------------------------------------------------------------
        //-         SENDING (ENCRYPTED)                                                                                     -
        //-------------------------------------------------------------------------------------------------------------------

        #region -=[- SENDING ENCRYPTED -]=-

        public void SendToClientRSA(Socket pSocket, NetComInstruction pInstruction) => SendToClientRSA(LClientList[pSocket], pInstruction);
Ejemplo n.º 10
0
 public void SendRSA(NetComInstruction pInstruction)
 {
     OutgoingInstructions.AddRSA(pInstruction, null);
 }
Ejemplo n.º 11
0
 public NetComInstructionQueueElement(NetComInstruction pInstruction, NetComClientData pClient, bool pRSAEncrypted)
 {
     Instruction  = pInstruction;
     Client       = pClient;
     RSAEncrypted = pRSAEncrypted;
 }
Ejemplo n.º 12
0
 public NetComInstructionQueueElement(NetComInstruction pInstruction, NetComClientData pClient) : this(pInstruction, pClient, false)
 {
 }