Example #1
0
        private async Task <bool> ReadData(int receive)
        {
            byte[] bytes = new byte[receive];

            _state.AppendRead(receive);

            if (_state.Read > _receivingMessageLength)
            {
                int extraRead = _state.Read - _receivingMessageLength;

                var bytes2 = new byte[extraRead];
                Array.Copy(_state.Buffer, receive - extraRead, bytes2, 0, extraRead);

                bytes = new byte[receive - extraRead];
                Array.Copy(_state.Buffer, 0, bytes, 0, receive - extraRead);

                _state.SubtractRead(extraRead);

                _state.ChangeBuffer(bytes2);
                _state.Flag = -3;
            }
            else if (_state.Read == _receivingMessageLength)
            {
                _state.Flag = -2;
                Array.Copy(_state.Buffer, 0, bytes, 0, bytes.Length);
                _state.ChangeBuffer(new byte[0]);
            }
            else
            {
                if (_state.Buffer.Length == _state.BufferSize)
                {
                    bytes = _state.Buffer;
                }
                else
                {
                    bytes = new byte[_state.Buffer.Length];
                    Array.Copy(_state.Buffer, 0, bytes, 0, bytes.Length);
                }
                _state.ChangeBuffer(new byte[0]);
            }

            if (MessageType == MessageType.File || MessageType == MessageType.Folder)
            {
                if (_socket.AllowReceivingFiles)
                {
                    var file = Encoding.UTF8.GetString(Header);
                    file = GetTempPath(file);
                    CreateDeleteFile(file);

                    if (_partNumber == 0)
                    {
                        _socket.RaiseFileReceiver(_state, _partNumber, _totalParts, file, MessageState.Beginning);
                    }

                    using (BinaryWriter writer = new BinaryWriter(File.Open(file, FileMode.Append)))
                    {
                        writer.Write(bytes, 0, bytes.Length);
                        writer.Close();
                    }
                }
            }
            else
            {
                _state.AppendBytes(bytes);
            }

            if (_state.Flag == -2)
            {
                bytes = null;
                MessageHasBeenReceived();
                _state.Reset();
            }

            if (_state.Flag == -3)
            {
                bytes = null;
                MessageHasBeenReceived();
                _state.Reset();
                return(await ReadBytesAndBuildMessage(_state.Buffer.Length));
            }


            return(true);
            //_socket.Receive(_state, 0);
        }
Example #2
0
        //Returns true if there is more data to read.
        private bool ReadData(int receive)
        {
            if (receive < 0)
            {
                return(false);
            }

            byte[] bytes = new byte[receive];
            _readBytes += receive;

            if (_readBytes > _receivingMessageLength)
            {
                int extraRead = _readBytes - _receivingMessageLength;

                var bytes2 = new byte[extraRead];
                Array.Copy(_state.Buffer, receive - extraRead, bytes2, 0, extraRead);

                bytes = new byte[receive - extraRead];
                Array.Copy(_state.Buffer, 0, bytes, 0, receive - extraRead);

                _readBytes -= extraRead;
                // _state.SubtractRead(extraRead);

                _state.ChangeBuffer(bytes2);
                _state.Flag = MessageFlag.MessageReceivedExtraData;
            }
            else if (_readBytes == _receivingMessageLength)
            {
                _state.Flag = MessageFlag.MessageReceivedNoExtraData;
                bytes       = new byte[receive];
                Array.Copy(_state.Buffer, 0, bytes, 0, bytes.Length);
                _state.ChangeBuffer(new byte[0]);
            }
            else
            {
                bytes = new byte[receive];
                Array.Copy(_state.Buffer, 0, bytes, 0, bytes.Length);
                _state.ChangeBuffer(new byte[0]);
            }

            if (MessageType == MessageType.File || MessageType == MessageType.Folder)
            {
                if (_socket.AllowReceivingFiles)
                {
                    var file = Encoding.UTF8.GetString(Header);
                    file = GetTempPath(file);
                    CreateDeleteFile(file);

                    if (_partNumber == 0)
                    {
                        _socket.RaiseFileReceiver(_state, _partNumber, _totalParts, file, MessageState.Beginning);
                    }

                    using (BinaryWriter writer = new BinaryWriter(File.Open(file, FileMode.Append)))
                    {
                        writer.Write(bytes, 0, bytes.Length);
                        writer.Close();
                    }
                }
            }
            else
            {
                var temp = new byte[_receivedBytes.Length + bytes.Length];
                _receivedBytes.CopyTo(temp, 0);
                bytes.CopyTo(temp, _receivedBytes.Length);
                _receivedBytes = temp;
                // _state.AppendBytes(bytes);
            }

            if (_state.Flag == MessageFlag.MessageReceivedNoExtraData)
            {
                bytes = null;
                MessageHasBeenReceived();
                _state.Reset();
            }

            if (_state.Flag == MessageFlag.MessageReceivedExtraData)
            {
                bytes = null;
                MessageHasBeenReceived();
                _state.Reset();
                return(true);
            }

            return(false);
        }