Ejemplo n.º 1
0
 public PutBytesMessageV3(PutBytesState State)
     : base(Endpoint.PutBytes)
 {
     Data       = new List <byte>();
     this.State = State;
     AddSize    = true;
 }
Ejemplo n.º 2
0
 public PutBytesMessageV3(PutBytesState State, List <byte> Id, List <byte> Data)
     : base(Endpoint.PutBytes)
 {
     this.Data  = Data;
     this.State = State;
     AddSize    = true;
 }
Ejemplo n.º 3
0
 public PutBytesMessage(PutBytesTransferType transferType, byte[] buffer, InstallProgressHandler progressHandler, uint index = 0)
     : base(Endpoint.PutBytes)
 {
     this._transferType    = transferType;
     this._buffer          = new List <byte>(buffer);
     this._progressHandler = progressHandler;
     this._index           = index;
     this._state           = PutBytesState.NotStarted;
 }
Ejemplo n.º 4
0
 public PutBytesClient(Pebble pebble, uint index, PutBytesType transferType, byte[] buffer)
 {
     pebble_ = pebble;
     index_ = index;
     transferType_ = transferType;
     buffer_ = buffer;
     state_ = PutBytesState.NotStarted;
     token_ = 0;
     left_ = 0;
 }
Ejemplo n.º 5
0
 public PutBytesClient(Pebble pebble, uint index, PutBytesType transferType, byte[] buffer)
 {
     pebble_       = pebble;
     index_        = index;
     transferType_ = transferType;
     buffer_       = buffer;
     state_        = PutBytesState.NotStarted;
     token_        = 0;
     left_         = 0;
 }
Ejemplo n.º 6
0
            public void init()
            {
                if (state_ != PutBytesState.NotStarted)
                {
                    HasError = true;
                    throw new Exception("Already init()ed");
                }
                byte[] data = Util.Pack("!bIbb", 1, buffer_.Length, transferType_, index_);
                pebble_.RegisterEndpointCallback(Endpoints.PUT_BYTES, PutBytesReceived);
                pebble_.sendMessage(Endpoints.PUT_BYTES, data);
                var wait = new EndpointSync <AppbankInstallMessageEventArgs>(pebble_, Endpoints.PUT_BYTES);

                state_ = PutBytesState.WaitForToken;
            }
Ejemplo n.º 7
0
        protected override void AddContentToMessage(List<byte> payload)
        {
            ServiceLocator.Logger.WriteLine("PutBytes <<< outgoing message for " + this._state.ToString());

            switch (this._state)
            {
                case PutBytesState.NotStarted:
                    {
                        // another magic number!...
                        payload.Add((byte)PutBytesState.WaitForToken);

                        byte[] length = BitConverter.GetBytes(this._buffer.Count);
                        if (BitConverter.IsLittleEndian)
                        {
                            Array.Reverse(length);
                        }

                        payload.AddRange(length);
                        payload.Add((byte)this._transferType);
                        payload.Add((byte)this._index);

                        // Move to next state...
                        this._state = PutBytesState.WaitForToken;
                    }

                    break;

                case PutBytesState.InProgress:
                    {
                        // equivalent of python PutBytesClient.send()
                        this._lastChunkSize = Math.Min(this._leftToSend, 2000);
                        int offset = this._buffer.Count - this._leftToSend;

                        // another magic number!...
                        payload.Add((byte)this._state);

                        byte[] tokenBytes = BitConverter.GetBytes(this._token & 0xFFFFFFFF);
                        byte[] dataToSendBytes = BitConverter.GetBytes(this._lastChunkSize);

                        if (BitConverter.IsLittleEndian)
                        {
                            Array.Reverse(tokenBytes);
                            Array.Reverse(dataToSendBytes);
                        }

                        payload.AddRange(tokenBytes);
                        payload.AddRange(dataToSendBytes);

                        byte[] data = new byte[this._lastChunkSize];
                        this._buffer.CopyTo(offset, data, 0, this._lastChunkSize);
                        payload.AddRange(data);
                        this._leftToSend -= this._lastChunkSize;

                        ServiceLocator.Logger.WriteLine("PutBytes - sending " + this._lastChunkSize.ToString() + " byte(s), " + this._leftToSend.ToString() + " byte(s) remain");
                    }

                    break;

                case PutBytesState.Abort:
                    {
                        // equivalent of python PutBytesClient.abort()
                        payload.Add((byte)this._state);
                        byte[] tokenBytes = BitConverter.GetBytes(this._token & 0xFFFFFFFF);

                        if (BitConverter.IsLittleEndian)
                        {
                            Array.Reverse(tokenBytes);
                        }

                        payload.AddRange(tokenBytes);
                    }

                    break;

                case PutBytesState.Commit:
                    {
                        payload.Add((byte)this._state);
                        byte[] tokenBytes = BitConverter.GetBytes(this._token & 0xFFFFFFFF);

                        uint crc = this._buffer.Crc32();
                        byte[] crcBytes = BitConverter.GetBytes(crc);
                        ServiceLocator.Logger.WriteLine(string.Format("Sending CRC of {0:X}", crc));

                        if (BitConverter.IsLittleEndian)
                        {
                            Array.Reverse(tokenBytes);
                            Array.Reverse(crcBytes);
                        }

                        payload.AddRange(tokenBytes);
                        payload.AddRange(crcBytes);
                    }

                    break;

                case PutBytesState.Complete:
                    {
                        payload.Add((byte)this._state);
                        byte[] tokenBytes = BitConverter.GetBytes(this._token & 0xFFFFFFFF);

                        if (BitConverter.IsLittleEndian)
                        {
                            Array.Reverse(tokenBytes);
                        }

                        payload.AddRange(tokenBytes);
                    }

                    break;
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Handles the state message.
        /// </summary>
        /// <param name="message">The message response.</param>
        /// <returns>True when the PutBytes process has completed (either successfully or not), false if further processing is required</returns>
        internal bool HandleStateMessage(PutBytesMessage message)
        {
            if (message.Result[0] != 1)
            {
                this.Errored = true;
            }

            ServiceLocator.Logger.WriteLine("PutBytes >>> incoming message for " + this._state.ToString());

            switch (this._state)
            {
                case PutBytesState.WaitForToken:
                    if (this.Errored)
                    {
                        return true;
                    }

                    byte[] tokenArray = new byte[message.Result.Count - 1];
                    message.Result.CopyTo(1, tokenArray, 0, tokenArray.Length);
                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(tokenArray);
                    }

                    this._token = BitConverter.ToUInt32(tokenArray, 0);

                    this._leftToSend = this._buffer.Count;

                    // next step is we start sending data - like PutBytesClient.send()
                    // the continuation is in AddContentToMessage
                    this._state = PutBytesState.InProgress;
                    break;

                case PutBytesState.InProgress:
                    if (this.Errored)
                    {
                        this._state = PutBytesState.Abort;
                    }
                    else
                    {
                        if (this._leftToSend > 0)
                        {
                            // Still more to send, so we return false and the next chunk will go
                            ServiceLocator.Logger.WriteLine(string.Format("Sent {0} of {1} bytes", this._buffer.Count - this._leftToSend, this._buffer.Count));
                            if (this._progressHandler != null)
                            {
                                // From PutBytes, we send the number of bytes we just sent rather than a percentage...
                                this._progressHandler(this._lastChunkSize);
                            }

                            return false;
                        }
                        else
                        {
                            // next step is we send the commit message like PutBytesClient.commit()
                            // the continuation is in AddContentToMessage
                            this._state = PutBytesState.Commit;
                        }
                    }

                    break;

                case PutBytesState.Commit:
                    if (this.Errored)
                    {
                        this._state = PutBytesState.Abort;
                    }
                    else
                    {
                        // next step is we send the commit message like PutBytesClient.commit()
                        // the continuation is in AddContentToMessage
                        this._state = PutBytesState.Complete;
                    }

                    break;

                case PutBytesState.Complete:
                    if (this.Errored)
                    {
                        this._state = PutBytesState.Abort;
                    }
                    else
                    {
                        this.Completed = true;
                        return true;
                    }

                    break;
            }

            return false;
        }
Ejemplo n.º 9
0
 public PutBytesMessage(PutBytesTransferType transferType, byte[] buffer, InstallProgressHandler progressHandler, uint index = 0)
     : base(Endpoint.PutBytes)
 {
     this._transferType = transferType;
     this._buffer = new List<byte>(buffer);
     this._progressHandler = progressHandler;
     this._index = index;
     this._state = PutBytesState.NotStarted;
 }
Ejemplo n.º 10
0
 public void init()
 {
     if (state_ != PutBytesState.NotStarted)
     {
         HasError = true;
         throw new Exception("Already init()ed");
     }
     byte[] data = Util.Pack("!bIbb", 1, buffer_.Length, transferType_, index_);
     pebble_.RegisterEndpointCallback(Endpoints.PUT_BYTES, PutBytesReceived);
     pebble_.sendMessage(Endpoints.PUT_BYTES, data);
     var wait = new EndpointSync<AppbankInstallMessageEventArgs>(pebble_, Endpoints.PUT_BYTES);
     state_ = PutBytesState.WaitForToken;
 }
Ejemplo n.º 11
0
            void PutBytesReceived(object sender, MessageReceivedEventArgs e)
            {
                switch (state_)
                {
                    case PutBytesState.WaitForToken:
                        {
                            var unpacked = Util.Unpack("!bI", e.Payload);
                            byte res = Convert.ToByte(unpacked[0]);
                            if (res != 1)
                            {
                                //throw new Exception("failed to wait");
                                HasError = true;
                            }
                            token_ = (uint)unpacked[1];
                            left_ = buffer_.Length;
                            state_ = PutBytesState.InProgress;
                            send();
                        }
                        break;
                    case PutBytesState.InProgress:
                        {
                            var unpacked = Util.Unpack("!b", e.Payload);
                            byte res = Convert.ToByte(unpacked[0]);
                            if (res != 1)
                            {
                                abort();
                                return;
                            }
                            if (left_ > 0)
                                send();
                            else
                            {
                                state_ = PutBytesState.Commit;
                                commit();
                            }
                        }
                        break;
                    case PutBytesState.Commit:
                        {
                            var unpacked = Util.Unpack("!b", e.Payload);
                            byte res = Convert.ToByte(unpacked[0]);
                            if (res != 1)
                            {
                                abort();
                                return;
                            }
                            state_ = PutBytesState.Complete;
                            complete();
                        }
                        break;
                    case PutBytesState.Complete:
                        {
                            var unpacked = Util.Unpack("!b", e.Payload);
                            byte res = Convert.ToByte(unpacked[0]);
                            if (res != 1)
                            {
                                abort();
                                return;
                            }
                            IsDone = true;
                        }
                        break;

                }
            }
Ejemplo n.º 12
0
        protected override void AddContentToMessage(List <byte> payload)
        {
            ServiceLocator.Logger.WriteLine("PutBytes <<< outgoing message for " + this._state.ToString());

            switch (this._state)
            {
            case PutBytesState.NotStarted:
            {
                // another magic number!...
                payload.Add((byte)PutBytesState.WaitForToken);

                byte[] length = BitConverter.GetBytes(this._buffer.Count);
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(length);
                }

                payload.AddRange(length);
                payload.Add((byte)this._transferType);
                payload.Add((byte)this._index);

                // Move to next state...
                this._state = PutBytesState.WaitForToken;
            }

            break;

            case PutBytesState.InProgress:
            {
                // equivalent of python PutBytesClient.send()
                this._lastChunkSize = Math.Min(this._leftToSend, 2000);
                int offset = this._buffer.Count - this._leftToSend;

                // another magic number!...
                payload.Add((byte)this._state);

                byte[] tokenBytes      = BitConverter.GetBytes(this._token & 0xFFFFFFFF);
                byte[] dataToSendBytes = BitConverter.GetBytes(this._lastChunkSize);

                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(tokenBytes);
                    Array.Reverse(dataToSendBytes);
                }

                payload.AddRange(tokenBytes);
                payload.AddRange(dataToSendBytes);

                byte[] data = new byte[this._lastChunkSize];
                this._buffer.CopyTo(offset, data, 0, this._lastChunkSize);
                payload.AddRange(data);
                this._leftToSend -= this._lastChunkSize;

                ServiceLocator.Logger.WriteLine("PutBytes - sending " + this._lastChunkSize.ToString() + " byte(s), " + this._leftToSend.ToString() + " byte(s) remain");
            }

            break;

            case PutBytesState.Abort:
            {
                // equivalent of python PutBytesClient.abort()
                payload.Add((byte)this._state);
                byte[] tokenBytes = BitConverter.GetBytes(this._token & 0xFFFFFFFF);

                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(tokenBytes);
                }

                payload.AddRange(tokenBytes);
            }

            break;

            case PutBytesState.Commit:
            {
                payload.Add((byte)this._state);
                byte[] tokenBytes = BitConverter.GetBytes(this._token & 0xFFFFFFFF);

                uint   crc      = this._buffer.Crc32();
                byte[] crcBytes = BitConverter.GetBytes(crc);
                ServiceLocator.Logger.WriteLine(string.Format("Sending CRC of {0:X}", crc));

                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(tokenBytes);
                    Array.Reverse(crcBytes);
                }

                payload.AddRange(tokenBytes);
                payload.AddRange(crcBytes);
            }

            break;

            case PutBytesState.Complete:
            {
                payload.Add((byte)this._state);
                byte[] tokenBytes = BitConverter.GetBytes(this._token & 0xFFFFFFFF);

                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(tokenBytes);
                }

                payload.AddRange(tokenBytes);
            }

            break;
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Handles the state message.
        /// </summary>
        /// <param name="message">The message response.</param>
        /// <returns>True when the PutBytes process has completed (either successfully or not), false if further processing is required</returns>
        internal bool HandleStateMessage(PutBytesMessage message)
        {
            if (message.Result[0] != 1)
            {
                this.Errored = true;
            }

            ServiceLocator.Logger.WriteLine("PutBytes >>> incoming message for " + this._state.ToString());

            switch (this._state)
            {
            case PutBytesState.WaitForToken:
                if (this.Errored)
                {
                    return(true);
                }

                byte[] tokenArray = new byte[message.Result.Count - 1];
                message.Result.CopyTo(1, tokenArray, 0, tokenArray.Length);
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(tokenArray);
                }

                this._token = BitConverter.ToUInt32(tokenArray, 0);

                this._leftToSend = this._buffer.Count;

                // next step is we start sending data - like PutBytesClient.send()
                // the continuation is in AddContentToMessage
                this._state = PutBytesState.InProgress;
                break;

            case PutBytesState.InProgress:
                if (this.Errored)
                {
                    this._state = PutBytesState.Abort;
                }
                else
                {
                    if (this._leftToSend > 0)
                    {
                        // Still more to send, so we return false and the next chunk will go
                        ServiceLocator.Logger.WriteLine(string.Format("Sent {0} of {1} bytes", this._buffer.Count - this._leftToSend, this._buffer.Count));
                        if (this._progressHandler != null)
                        {
                            // From PutBytes, we send the number of bytes we just sent rather than a percentage...
                            this._progressHandler(this._lastChunkSize);
                        }

                        return(false);
                    }
                    else
                    {
                        // next step is we send the commit message like PutBytesClient.commit()
                        // the continuation is in AddContentToMessage
                        this._state = PutBytesState.Commit;
                    }
                }

                break;

            case PutBytesState.Commit:
                if (this.Errored)
                {
                    this._state = PutBytesState.Abort;
                }
                else
                {
                    // next step is we send the commit message like PutBytesClient.commit()
                    // the continuation is in AddContentToMessage
                    this._state = PutBytesState.Complete;
                }

                break;

            case PutBytesState.Complete:
                if (this.Errored)
                {
                    this._state = PutBytesState.Abort;
                }
                else
                {
                    this.Completed = true;
                    return(true);
                }

                break;
            }

            return(false);
        }
Ejemplo n.º 14
0
            void PutBytesReceived(object sender, MessageReceivedEventArgs e)
            {
                switch (state_)
                {
                case PutBytesState.WaitForToken:
                {
                    var  unpacked = Util.Unpack("!bI", e.Payload);
                    byte res      = Convert.ToByte(unpacked[0]);
                    if (res != 1)
                    {
                        //throw new Exception("failed to wait");
                        HasError = true;
                    }
                    token_ = (uint)unpacked[1];
                    left_  = buffer_.Length;
                    state_ = PutBytesState.InProgress;
                    send();
                }
                break;

                case PutBytesState.InProgress:
                {
                    var  unpacked = Util.Unpack("!b", e.Payload);
                    byte res      = Convert.ToByte(unpacked[0]);
                    if (res != 1)
                    {
                        abort();
                        return;
                    }
                    if (left_ > 0)
                    {
                        send();
                    }
                    else
                    {
                        state_ = PutBytesState.Commit;
                        commit();
                    }
                }
                break;

                case PutBytesState.Commit:
                {
                    var  unpacked = Util.Unpack("!b", e.Payload);
                    byte res      = Convert.ToByte(unpacked[0]);
                    if (res != 1)
                    {
                        abort();
                        return;
                    }
                    state_ = PutBytesState.Complete;
                    complete();
                }
                break;

                case PutBytesState.Complete:
                {
                    var  unpacked = Util.Unpack("!b", e.Payload);
                    byte res      = Convert.ToByte(unpacked[0]);
                    if (res != 1)
                    {
                        abort();
                        return;
                    }
                    IsDone = true;
                }
                break;
                }
            }