示例#1
0
        public void TransformFromEmptyProtocolTest()
        {
            EriverProtocol ep = new EriverProtocol();
            byte[] ans = EriverStreamReaderWriter.Transform(ep);

            Assert.AreEqual<int>(1, ans.Length);
            CollectionAssert.AreEqual(new byte[] { 0 }, ans);
        }
示例#2
0
 /// <summary>
 /// Transforms a message into its byte array form.
 /// </summary>
 /// <param name="proto">The message to be transformed</param>
 /// <returns>The resulting array</returns>
 public static byte[] Transform(EriverProtocol proto)
 {
     if (proto == null) return new byte[0];
     int length = CommandConvert.ToLength(proto.Kind) + 1;
     byte[] buf = new byte[length];
     Stream tempStream = new MemoryStream(buf);
     serializer.Serialize(tempStream, proto);
     return buf;
 }
 static Trackers.TrackerCallback defaultAction(ConnectionHandler ch, EriverProtocol packet)
 {
     return delegate(int error, int result)
     {
         if (error != 0 || result == 0)
         {
             packet.Kind = Command.Unavailable;
         }
         ch.Send(packet);
     };
 }
示例#4
0
        /// <summary>
        /// Transforms a message into its byte array form.
        /// </summary>
        /// <param name="proto">The message to be transformed</param>
        /// <returns>The resulting array</returns>
        public static byte[] Transform(EriverProtocol proto)
        {
            if (proto == null)
            {
                return(new byte[0]);
            }
            int length = CommandConvert.ToLength(proto.Kind) + 1;

            byte[] buf        = new byte[length];
            Stream tempStream = new MemoryStream(buf);

            serializer.Serialize(tempStream, proto);
            return(buf);
        }
 public void Send(EriverProtocol proto)
 {
     logger.Debug("Writing packet: " + proto);
     byte[] buf = EriverStreamReaderWriter.Transform(proto);
     try
     {
         stream.Write(buf, 0, buf.Length);
     }
     catch (IOException e)
     {
         logger.Error("IOException while writing to stream. Closing handler.");
         logger.Debug(e);
         stop.Set();
     }
 }
示例#6
0
        /// <summary>
        /// Determines if this object is by value equal to another.
        /// </summary>
        /// <param name="obj">A object to compare with.</param>
        /// <returns>If the two objects are considered equal, true. False otherwise</returns>
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            // If parameter cannot be cast to Point return false.
            EriverProtocol other = obj as EriverProtocol;

            if ((System.Object)other == null)
            {
                return(false);
            }

            if (this.Kind != other.Kind)
            {
                return(false);
            }

            switch (Kind)
            {
            case Command.GetPoint: return(GetPoint.Equals(other.GetPoint));

            case Command.StartCalibration: return(StartCalibration.Equals(other.StartCalibration));

            case Command.EndCalibration: return(true);

            case Command.ClearCalibration: return(true);

            case Command.AddPoint: return(AddPoint.Equals(other.AddPoint));

            case Command.Unavailable: return(true);

            case Command.Name: return(Name.Equals(other.Name));

            case Command.Fps: return(Fps.Equals(other.Fps));

            case Command.KeepAlive: return(true);

            default: return(false);
            }
        }
示例#7
0
 /// <summary>
 /// Writes a message to the stream
 /// </summary>
 /// <param name="proto">The message to be written</param>
 public void Write(EriverProtocol proto)
 {
     serializer.Serialize(stream, proto);
 }
        private void Run()
        {
            EriverProtocol prot = new EriverProtocol();

            // Sending name first according to spec.
            prot.Kind = Command.Name;
            prot.Name = new Name();
            prot.Name.Value = name;
            Send(prot);

            while (!stop.WaitOne(0))
            {
                try
                {
                    prot = readerWriter.Read();
                    using (log4net.ThreadContext.Stacks["NDC"].Push("ConnHandler_Handlers"))
                    {
                        logger.Debug("Read packet: " + prot);
                        try
                        {
                            var m = ConnHandler_Handlers.Messages[CommandConvert.ToByte(prot.Kind)]();
                            m.Accept(this, prot);
                        }
                        catch (KeyNotFoundException)
                        {
                            logger.Error("Package type not supported. Different versions?");
                            stop.Set();
                            break;
                        }
                    }
                }
                catch (InvalidOperationException e)
                {
                    using (log4net.ThreadContext.Stacks["NDC"].Push("InvalidOperationException"))
                    {
                        stop.Set();
                        logger.Error("Error in stream.");
                        logger.Debug(e);
                    }
                }

            }
            stream.Close();
        }
示例#9
0
        private EriverProtocol CreateTestProtocol(Command command)
        {
            EriverProtocol ep = new EriverProtocol();
            ep.Kind = command;

            ep.GetPoint = new GetPoint();
            ep.GetPoint.X = 0.314;
            ep.GetPoint.Y = 0.431;
            ep.GetPoint.Timestamp = 1;

            ep.AddPoint = new AddPoint();
            ep.AddPoint.X = 0.431;
            ep.AddPoint.Y = 0.314;

            ep.StartCalibration = new StartCalibration();
            ep.StartCalibration.Angle = 13.37;

            ep.Fps = new Fps();
            ep.Fps.Value = 4041;

            ep.Name = new Name();
            ep.Name.Value = 33;

            return ep;
        }
 public override void Accept(ConnectionHandler ch, EriverProtocol packet)
 {
     ch.GetTracker().AddPoint(packet.AddPoint.X, packet.AddPoint.Y , defaultAction(ch, packet));
 }
 public override void Accept(ConnectionHandler ch, EriverProtocol packet)
 {
     XConfSettings xconf = new XConfSettings(
     Properties.Settings.Default.ScreenHeight,
     Properties.Settings.Default.ScreenWidth,
     Properties.Settings.Default.TrackerDeltaX,
     Properties.Settings.Default.TrackerDeltaY,
     Properties.Settings.Default.TrackerDeltaZ,
     Properties.Settings.Default.TrackerDeltaAngle);
     //ch.GetTracker().SetXConfig(xconf, packet.StartCalibration.Angle);
     ch.GetTracker().StartCalibration(defaultAction(ch, packet));
 }
 public override void Accept(ConnectionHandler ch, EriverProtocol packet)
 {
     ch.Send(packet);
 }
 public abstract void Accept(ConnectionHandler ch, EriverProtocol packet);
 public override void Accept(ConnectionHandler ch, EriverProtocol packet)
 {
     ch.GetTracker().GetName(delegate(int error, int result)
     {
         packet.Name.Value = (error != 0) ? (byte) result : (byte) 0;
         ch.Send(packet);
     });
 }
 public override void Accept(ConnectionHandler ch, EriverProtocol packet)
 {
     ch.GetTracker().EndCalibration(defaultAction(ch, packet));
 }
 public override void Accept(ConnectionHandler ch, EriverProtocol packet)
 {
     ch.Listen = !ch.Listen;
 }
        public void Start()
        {
            tracker.RegisterOnETEvent(delegate(GetPoint point)
            {
                if (stop.WaitOne(0))
                {
                    OnStatusChanged(this, new EventArgs());
                    tracker.Disable(null);
                    Thread.CurrentThread.Abort(); // Shutting down this thread.
                    return;
                }
                EriverProtocol proto = new EriverProtocol();
                proto.Kind = Command.GetPoint;
                proto.GetPoint = point;
                Send(proto);
            });

            tracker.Enable(null);
            using (log4net.ThreadContext.Stacks["NDC"].Push("Run"))
            {
                Run();
            }
        }
        public void Start()
        {
            tracker.RegisterOnETEvent(delegate(GetPoint point)
            {
                if (stop.WaitOne(0))
                {
                    OnStatusChanged(this, new EventArgs());
                    tracker.Disable(null);
                    Thread.CurrentThread.Abort(); // Shutting down this thread.
                    return;
                }
                if (Listen) {
                    EriverProtocol proto = new EriverProtocol();
                    proto.Kind = Command.GetPoint;
                    proto.GetPoint = point;
                    Send(proto);
                }
            });

            tracker.Enable(null);
            using (log4net.ThreadContext.Stacks["NDC"].Push("Run"))
            {
                Run();
            }

            //Finalize calibration if disconnecting during calibration.
            tracker.GetState(delegate(int state, int error)
            {
                if ((state & 2) != 0)
                {
                    tracker.ClearCalibration(delegate(int res, int err)
                    {
                        tracker.EndCalibration(null);
                    });

                }
            });
        }
示例#19
0
 /// <summary>
 /// Writes a message to the stream
 /// </summary>
 /// <param name="proto">The message to be written</param>
 public void Write(EriverProtocol proto)
 {
     serializer.Serialize(stream, proto);
 }
 public override void Accept(ConnectionHandler ch, EriverProtocol packet)
 {
     ch.Listen = !ch.Listen;
     packet.GetPoint.X = -1;
     packet.GetPoint.Y = 2;
     ch.Send(packet);
 }
 public override void Accept(ConnectionHandler ch, EriverProtocol packet)
 {
     ch.GetTracker().StartCalibration(packet.StartCalibration.Angle, defaultAction(ch, packet));
 }