Пример #1
0
 protected void OnPacketReceived(SyrusPacket pak)
 {
     if (PacketReceived != null)
     {
         PacketReceived(pak);
     }
 }
Пример #2
0
        private void SendBtn_Click(object sender, RoutedEventArgs e)
        {
            SyrusPacket pak = new SyrusPacket();

            pak.id = 24;

            pak.data = System.Text.Encoding.ASCII.GetBytes(TextBox.Text);
            pak.n    = (byte)pak.data.Length;

            sbm.SendPacket(pak);
        }
Пример #3
0
        private void SendFrame(object source, ElapsedEventArgs e)
        {
            if (sbm.GetConnectedStatus())
            {
                SyrusPacket pak = new SyrusPacket();
                pak.id = 20;
                Dispatcher.BeginInvoke(new Action(() => {
                    Leap.Vector v = lpm.GetHandPosition();
                    Text.Text     = "X: " + v.x + " Y: " + v.y + " Z: " + v.z;
                }), DispatcherPriority.SystemIdle);


                byte[] arr = lpm.getFrameInfo();
                pak.data = arr;
                pak.n    = (byte)pak.data.Length;

                sbm.SendPacket(pak);
            }
        }
Пример #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();
        }
Пример #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))
                    {
                        System.Diagnostics.Debug.WriteLine("Client Disconnected");
                        break;
                    }

                    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 HRESULT_FROM_WIN32(ERROR_OPERATION_ABORTED).
                catch (Exception ex) when((uint)ex.HResult == 0x800703E3)
                {
                    System.Diagnostics.Debug.WriteLine("Client Disconnected Successfully");
                    break;
                }
            }

            Disconnect();
        }