Ejemplo n.º 1
0
        public void Send(string dataToSend)
        {
            if (isClosed)
            {
                return;
            }
            if (socket == null || !socket.Connected)
            {
                Close();
                return;
            }

            dataToSend = QEncryption.Encrypt(dataToSend);
            dataToSend = PacketDatas.PACKET_HEADER + dataToSend;
            //We us UTF8 characterss so lets convert the bytes into utf8 string!
            byte[] dataBytes = Encoding.UTF8.GetBytes(dataToSend);
            //And then send it to our client.
            try
            {
                socket.Send(dataBytes, SocketFlags.None);
            }
            catch (Exception e)
            {
                Eutils.FileError(String.Format("{0} {1} \n", e.Message, e.StackTrace), "client_error.txt");
                Eutils.WriteLine("Error Sending! Message:{0}", Eutils.MESSSAGE_TYPE.ERROR, e.Message, e.StackTrace);
                Close();
            }
        }
Ejemplo n.º 2
0
 private void OnReceive(string packetString)
 {
     //Then we send this.
     if (packetString[0].ToString() != PacketDatas.PACKET_HEADER)
     {
         Close();
         return;
     }
     string[] packets = packetString.Split(new string[] { PacketDatas.PACKET_HEADER }, StringSplitOptions.None);
     for (int i = 1; i < packets.Length; i++)
     {
         //Eutils.WriteLine("received data:{0} at Time:{1} Length:{2}", Eutils.MESSSAGE_TYPE.NORMAL, packets[i], DateTime.Now, packets[i].Length);
         packets[i] = QEncryption.Decrypt(packets[i]);
         Eutils.WriteLine("decrypted data:{0} at Time:{1} Length:{2}", Eutils.MESSSAGE_TYPE.NORMAL, packets[i], DateTime.Now, packets[i].Length);
         if (packets[i] == "NULL")
         {
             Eutils.FileError(String.Format("{0} \n", "Error decrypting: packets[i] == NULL"), "client_error.txt");
             Eutils.WriteLine("Error decrypting", Eutils.MESSSAGE_TYPE.ERROR);
             //Close();
             break;
         }
         //Split the packet by our split character.
         string[] packetStrings = packets[i].Split(new string[] { PacketDatas.PACKET_SPLIT }, StringSplitOptions.None);
         received(this, packetStrings);
     }
 }