Exemplo n.º 1
0
        /// <summary>
        /// Sends a string to the server
        /// </summary>
        /// <param name="message">The message</param>
        /// <returns>Returns true if there was no error</returns>
        public bool sendMessage(String message)
        {
            if (socket == null)
            {
                log.Error("Not connected!");
                return(false);
            }
            byte[] msgFragAndLength     = new byte[FRAG_SIZE + 4];
            byte[] encodedMsgLengthByte = new byte[4];
            int    bytesSent            = 0;
            int    numBytes             = 0;

            byte[] messageByte        = Encoding.UTF8.GetBytes(message);
            uint   totalMessageLength = (uint)messageByte.Length;
            string oute;

            while (bytesSent < totalMessageLength)
            {
                if ((totalMessageLength - bytesSent) > FRAG_SIZE)
                {
                    encodedMsgLengthByte = System.BitConverter.GetBytes((int)XEncoder.encodeNatural((uint)FRAG_SIZE, 1));
                    //Convert to bigendian
                    Array.Reverse(encodedMsgLengthByte);

                    System.Buffer.BlockCopy(encodedMsgLengthByte, 0, msgFragAndLength, 0, encodedMsgLengthByte.Length);
                    System.Buffer.BlockCopy(messageByte, bytesSent, msgFragAndLength, encodedMsgLengthByte.Length, FRAG_SIZE);

                    oute     = System.Text.ASCIIEncoding.ASCII.GetString(msgFragAndLength);
                    numBytes = socket.Client.Send(msgFragAndLength, msgFragAndLength.Length, SocketFlags.None);
                }
                else
                {
                    encodedMsgLengthByte = System.BitConverter.GetBytes((int)XEncoder.encodeNatural(totalMessageLength - (uint)bytesSent, 0));
                    //Convert to bigendian
                    Array.Reverse(encodedMsgLengthByte);

                    System.Buffer.BlockCopy(encodedMsgLengthByte, 0, msgFragAndLength, 0, encodedMsgLengthByte.Length);
                    System.Buffer.BlockCopy(messageByte, bytesSent, msgFragAndLength, encodedMsgLengthByte.Length, (int)totalMessageLength - (int)bytesSent);

                    oute     = System.Text.ASCIIEncoding.ASCII.GetString(msgFragAndLength);
                    numBytes = socket.Client.Send(msgFragAndLength, (int)totalMessageLength - (int)bytesSent + 4, SocketFlags.None);
                }
                if (numBytes < 5)
                {
                    log.Error("Failed to send!");
                    return(false);
                }
                else
                {
                    bytesSent += numBytes - 4;
                }
            }
            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Waits to receive a string from the server
        /// </summary>
        /// <param name="responseMessage">Used to store the received message</param>
        /// <returns>Returns true if no error occurred during receiving</returns>
        public bool receiveMessage(out string responseMessage)
        {
            bool flag = false;

            byte[] encodedLengthByte = new byte[4];
            uint   encodedLength;
            uint   length;
            int    fragCounter = 0;
            int    numBytes;
            string message = "";

            byte[] msgBuf = new byte[FRAG_SIZE + 4];
            responseMessage = "";
            do
            {
                //Read Length
                numBytes = socket.Client.Receive(encodedLengthByte, 4, SocketFlags.None);
                if (numBytes != 4)
                {
                    log.Error("Could not receive 4 bytes for length.");
                    return(false);
                }
                //Convert to littleEndian
                Array.Reverse(encodedLengthByte);

                encodedLength = System.BitConverter.ToUInt32(encodedLengthByte, 0);
                length        = XEncoder.decodeNatural(encodedLength);
                flag          = XEncoder.decodeFlag(encodedLength);


                if (length < 1 || length > FRAG_SIZE)
                {
                    log.Error("Illegal msg length " + length);
                    return(false);
                }

                //Read Msg
                numBytes = socket.Client.Receive(msgBuf, (int)length, SocketFlags.None);
                message += System.Text.ASCIIEncoding.ASCII.GetString(msgBuf).Substring(0, numBytes);

                if (fragCounter > MAX_FRAGS)
                {
                    log.Error("Maximum fragments reached, aborting.");
                    return(false);
                }

                fragCounter++;
            } while (flag);
            responseMessage = message;
            return(true);
        }