Ejemplo n.º 1
0
 protected void OnPacketReceived(SyrusPacket pak)
 {
     if (PacketReceived != null)
     {
         PacketReceived(pak);
     }
 }
Ejemplo n.º 2
0
        private async void Recieved(SyrusPacket packet)
        {
            switch (packet.id)
            {
            case 24: {
                await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => {
                        Text.Text = System.Text.ASCIIEncoding.ASCII.GetString(packet.data);
                    });

                break;
            }

            case 20:
            {
                float x = BitConverter.ToSingle(packet.data, 0);
                float y = BitConverter.ToSingle(packet.data, 4);
                float z = BitConverter.ToSingle(packet.data, 8);

                await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => {
                        Text.Text = "X: " + x + " Y: " + y + " Z: " + z;
                    });

                break;
            }
            }
        }
Ejemplo n.º 3
0
        private void SendBtn_Click(object sender, RoutedEventArgs e)
        {
            SyrusPacket pak = new SyrusPacket();

            pak.id = 23;
            pak.n  = 0;

            cbm.SendPacket(pak);
        }
Ejemplo n.º 4
0
        public async void SendPacket(SyrusPacket pak)
        {
            if (!isConnected)
            {
                throw new System.InvalidOperationException("Controller not connected");
            }

            writer.WriteByte(Constants.StartCode);

            SendByte(pak.id);
            SendByte(pak.n);

            if (pak.data != null)
            {
                foreach (byte b in pak.data)
                {
                    SendByte(b);
                }
            }

            writer.WriteByte(Constants.EndCode);
            await writer.StoreAsync();
        }
Ejemplo n.º 5
0
        private async void mainLoop()
        {
            bool        escaped = false;
            int         index   = -1;
            SyrusPacket packet  = new SyrusPacket();

            while (true)
            {
                try {
                    uint size = await reader.LoadAsync(sizeof(byte));

                    if (size < sizeof(byte))
                    {
                        Disconnect("Remote device terminated connection - make sure only one instance of server is running on remote device");
                        return;
                    }

                    byte b = reader.ReadByte();

                    if (!escaped)
                    {
                        if (b == Constants.StartCode)
                        {
                            index  = 0;
                            packet = new SyrusPacket();
                        }
                        else if (b == Constants.EndCode)
                        {
                            // Check if the data matches what the packet said
                            if (index >= 3 && packet.n == index - 3)
                            {
                                OnPacketReceived(packet);
                            }
                            index = -1;
                        }
                        else if (b == Constants.EscCode)
                        {
                            escaped = true;
                            index--;
                        }
                        else if (index - 3 >= packet.n)
                        {
                            // Error with the packet
                            index = -1;
                        }
                        else if (index == 1)
                        {
                            packet.id = b;
                        }
                        else if (index == 2)
                        {
                            packet.n    = b;
                            packet.data = new byte[b];
                        }
                        else if (index > 2)
                        {
                            packet.data[index - 3] = b;
                        }
                    }
                    else
                    {
                        if (index - 3 >= packet.n)
                        {
                            // Error with the packet
                            index = -1;
                        }
                        else if (index == 1)
                        {
                            packet.id = b;
                        }
                        else if (index == 2)
                        {
                            packet.n    = b;
                            packet.data = new byte[b];
                        }
                        else if (index > 2)
                        {
                            packet.data[index - 3] = b;
                        }
                        escaped = false;
                    }

                    if (index >= 0)
                    {
                        index++;
                    }
                } catch (Exception ex) {
                    lock (this) {
                        if (socket == null)
                        {
                            // Do not print anything here -  the user closed the socket.
                            if ((uint)ex.HResult == 0x80072745)
                            {
                                System.Diagnostics.Debug.WriteLine("Disconnect triggered by remote device");
                            }
                            else if ((uint)ex.HResult == 0x800703E3)
                            {
                                System.Diagnostics.Debug.WriteLine("The I/O operation has been aborted because of either a thread exit or an application request.");
                            }
                        }
                        else
                        {
                            Disconnect("Read stream failed with error: " + ex.Message);
                        }
                    }
                }
            }
        }