Exemplo n.º 1
0
        private void RegisterQuitMessage(SimConnect connection)
        {
            void OnRecvQuit(SimConnect s, SIMCONNECT_RECV e)
            {
                connection.OnRecvQuit -= OnRecvQuit;
                CommandTarget          = new NullSimulatorCommandTarget();
                LostConnection?.Invoke(this, EventArgs.Empty);
            }

            connection.OnRecvQuit += OnRecvQuit;
        }
Exemplo n.º 2
0
 private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs ev)
 {
     try
     {
         SerialPort serial = (SerialPort)sender;
         string     indata = serial.ReadLine();
         DataReceived?.Invoke(this, new StrDataEventArgs(indata));
     }
     catch (Exception ex)
     {
         logger.Error(ex, "DataReceivedHandler.");
         LostConnection?.Invoke(this, new StrDataEventArgs("Can't read data."));
         Stop();
     }
 }
Exemplo n.º 3
0
 public void Send(string data)
 {
     try
     {
         if (com.IsOpen)
         {
             com.Write(data);
         }
     }
     catch (Exception ex)
     {
         logger.Error(ex, "Can't send: {0}", data);
         LostConnection?.Invoke(this, new StrDataEventArgs("Can't write data."));
         Stop();
     }
 }
Exemplo n.º 4
0
 private void Loop()
 {
     while (!_shouldStop)
     {
         try
         {
             Thread.Sleep(1);
         }
         catch (Exception ex)
         {
             logger.Error(ex);
             LostConnection?.Invoke(this, new StrDataEventArgs("Error in main loop."));
             Stop();
         }
     }
 }
Exemplo n.º 5
0
        public void Start()
        {
            try
            {
                if (com != null)
                {
                    _shouldStop = false;

                    if (!com.IsOpen)
                    {
                        com.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                        com.Open();
                    }

                    thrd = new Thread(Loop);
                    thrd.Start();
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex, "port: {0}", com.PortName);
                LostConnection?.Invoke(this, new StrDataEventArgs("Can't open com port."));
            }
        }
Exemplo n.º 6
0
 private void Client_ThisPlayerDisconnected(object sender, EventArgs e)
 {
     UnsubscribeFromEvents();
     LostConnection?.Invoke(this, EventArgs.Empty);
 }
Exemplo n.º 7
0
 protected virtual void LostConnectionEvent(MessageEventArgs e)
 {
     LostConnection?.Invoke(this, e);
 }
Exemplo n.º 8
0
 private void Disconnected()
 {
     LostConnection?.Invoke(null, EventArgs.Empty);
 }
Exemplo n.º 9
0
 private void OnLostConnection()
 {
     LostConnection?.Invoke(this, new EventArgs());
 }
    private void Server_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
    {
        if (sender == commandSocket)
        {
            LoggingManager.Log("Command connection received");
            Connected?.Invoke();
            _ = Task.Run(async() =>
            {
                try
                {
                    var commandStreamIn = args.Socket.InputStream.AsStreamForRead();
                    var reader          = new StreamReader(commandStreamIn, Encoding.UTF8);

                    var commandStreamOut = args.Socket.OutputStream.AsStreamForWrite();
                    commandOut           = new StreamWriter(commandStreamOut, Encoding.UTF8);

                    while (true)
                    {
                        string message = await reader.ReadLineAsync();
                        if (message.Length > 0)
                        {
                            TextReceived?.Invoke(message);
                            //LoggingManager.Log("Received message: " + message);
                        }
                    }
                }
                catch { LostConnection?.Invoke(); connected = false; }
            });
        }
        else if (sender == videoSocket)
        {
            LoggingManager.Log("Video connection received");
            _ = Task.Run(async() =>
            {
                try
                {
                    var videoStreamIn       = new DataReader(args.Socket.InputStream);
                    videoStreamIn.ByteOrder = ByteOrder.LittleEndian; // need to set this or the bytes will be read in the wrong order

                    // load the bytes for the first five integers (4 bytes each), the image size and four values for the time (h, m, s, ms)
                    var count = await videoStreamIn.LoadAsync(20);

                    double avgLag = 0, avgLagCount = 0;
                    double avgFps = 0, frameCount = 0;
                    Queue <Tuple <DateTime, double> > fpsQueue = new Queue <Tuple <DateTime, double> >();
                    double prevFps = 0;
                    double minFps  = 0;

                    while (true)
                    {
                        int length = videoStreamIn.ReadInt32(); // first read the image size (in # of bytes)

                        int h  = videoStreamIn.ReadInt32();     // hours
                        int m  = videoStreamIn.ReadInt32();     // minutes
                        int s  = videoStreamIn.ReadInt32();     // seconds
                        int ms = videoStreamIn.ReadInt32();     // milliseconds

                        DateTime timeReceived = DateTime.Now;
                        DateTime timeSent     = new DateTime(timeReceived.Year, timeReceived.Month, timeReceived.Day, h, m, s, ms);

                        double elapsed = (timeReceived - timeSent).TotalMilliseconds;
                        //LoggingManager.Log("Frame Lag: " + elapsed.ToString("0") + " ms");

                        // average the first several frames to measure base lag
                        if (avgLagCount < 10)
                        {
                            avgLag += elapsed / 10;
                            avgLagCount++;
                        }

                        //LoggingManager.Log("Attempting to read image of length " + length);

                        // read the image
                        DateTime start = DateTime.Now;
                        byte[] buffer  = new byte[length];
                        count          = await videoStreamIn.LoadAsync((uint)length);
                        videoStreamIn.ReadBytes(buffer);
                        //ImageReceived?.Invoke(buffer);
                        //ImageDecoded?.Invoke(buffer, 720, 1280);

                        // Fixes lag getting worse over time, but causes image freezing issue
                        //if(videoStreamIn.UnconsumedBufferLength > 0 || (elapsed > avgLag + 200))
                        //{
                        //    LoggingManager.Log("Frame Dropped (" + (elapsed > avgLag + 200 ? "excess lag" : "next frame ready") + ")");

                        //    // clean up used memory
                        //    buffer = null;
                        //    GC.GetTotalMemory(true);

                        //    // start to read the next image
                        //    count = await videoStreamIn.LoadAsync(20);

                        //    // skip to next frame without procesing the current one
                        //    continue;
                        //}

                        // send confirmation that we received this frame along with the minimum fps over the past couple of seconds (rounded down to prevent lag)
                        if (commandOut != null)
                        {
                            int fps = (int)Math.Ceiling(minFps - 1);
                            if (fps != prevSentFps)
                            {
                                LoggingManager.Log("Set FPS: " + fps);
                                prevSentFps = fps;
                            }
                            string confirmationString = string.Format("{0},{1},{2},{3},{4}\n", h, m, s, ms, fps);
                            commandOut.Write(confirmationString);
                            commandOut.Flush();
                            //Debug.Log(confirmationString);
                        }

                        //LoggingManager.Log("Read Image: " + (DateTime.Now - start).TotalMilliseconds.ToString("0.0"));
                        //start = DateTime.Now;

                        // convert the bytes to a stream
                        var imgStream = buffer.AsBuffer().AsStream().AsRandomAccessStream();

                        //LoggingManager.Log("Read image, decoding");

                        // decode the image
                        var decoder = await BitmapDecoder.CreateAsync(imgStream);
                        imgStream.Seek(0);
                        int width  = (int)decoder.PixelWidth;
                        int height = (int)decoder.PixelHeight;

                        //LoggingManager.Log("Create Decoder: " + (DateTime.Now - start).TotalMilliseconds.ToString("0.0"));
                        //start = DateTime.Now;

                        var data = await decoder.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, new BitmapTransform(), ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage);

                        //LoggingManager.Log("Get Pixel Data: " + (DateTime.Now - start).TotalMilliseconds.ToString("0.0"));
                        //start = DateTime.Now;

                        var decodedBytes = data.DetachPixelData();

                        //LoggingManager.Log("Detach Pixel Data: " + (DateTime.Now - start).TotalMilliseconds.ToString("0.0"));
                        //start = DateTime.Now;

                        //LoggingManager.Log("Decode Image: " + (DateTime.Now - start).TotalMilliseconds.ToString("0.0"));
                        //start = DateTime.Now;

                        // display the image
                        ImageDecoded?.Invoke(decodedBytes, width, height);
                        //LoggingManager.Log("Received image (" + width + " x " + height + "), " + (DateTime.Now - start).TotalMilliseconds.ToString("0.0") + "ms");
                        //LoggingManager.Log("Received Image: " + (DateTime.Now - start).TotalMilliseconds.ToString("0.0"));

                        // clean up memory
                        buffer       = null;
                        data         = null;
                        decodedBytes = null;
                        GC.GetTotalMemory(true);

                        // update fps
                        double currFps = 1.0 / (DateTime.Now - start).TotalSeconds;
                        if (frameCount > 0)
                        {
                            avgFps *= frameCount;
                        }
                        avgFps += currFps;
                        frameCount++;
                        avgFps /= frameCount;
                        prevFps = currFps;
                        fpsQueue.Enqueue(new Tuple <DateTime, double>(DateTime.Now, currFps));
                        while (fpsQueue.Count > 20 || (DateTime.Now - fpsQueue.Peek().Item1).TotalSeconds > 2)
                        {
                            fpsQueue.Dequeue();
                        }
                        minFps = double.MaxValue;
                        foreach (var val in fpsQueue)
                        {
                            if (val.Item2 < minFps)
                            {
                                minFps = val.Item2;
                            }
                        }

                        // start to read the next image
                        count = await videoStreamIn.LoadAsync(20);
                    }
                }
                catch { LostConnection?.Invoke(); connected = false; }
            });
        }
    }
Exemplo n.º 11
0
    private async void Socket_MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
    {
        Stream   streamIn = args.GetDataStream().AsStreamForRead();
        HostName serverAddressFull;

        using (var reader = new StreamReader(streamIn))
        {
            serverName = await reader.ReadToEndAsync();

            serverAddressFull = args.RemoteAddress;
            serverAddress     = serverAddressFull.ToString();
            LoggingManager.Log("Detected " + serverName + " (" + serverAddress + "), attempting to establish tcp connection");
        }

        commandSocket = new StreamSocket();
        await commandSocket.ConnectAsync(serverAddressFull, commandPort.ToString());

        LoggingManager.Log("Tcp connection established, sending handshake");

        var commandStreamOut = commandSocket.OutputStream.AsStreamForWrite();

        commandOut = new StreamWriter(commandStreamOut, Encoding.UTF8);
        commandOut.WriteLine(deviceName);
        commandOut.Flush();

        LoggingManager.Log("Handshake sent, establishing video connection");

        videoSocket = new StreamSocket();
        await videoSocket.ConnectAsync(serverAddressFull, videoPort.ToString());

        Connected?.Invoke();

        connected = true;

        LoggingManager.Log("Video connection established, starting new threads");

        _ = Task.Run(async() =>
        {
            try
            {
                var commandStreamIn = commandSocket.InputStream.AsStreamForRead();
                var reader          = new StreamReader(commandStreamIn, Encoding.UTF8);

                while (true)
                {
                    string message = await reader.ReadLineAsync();
                    if (message.Length > 0)
                    {
                        TextReceived?.Invoke(message);
                        //LoggingManager.Log("Received message: " + message);
                    }
                }
            }
            catch { LostConnection?.Invoke(); connected = false; }
        });

        _ = Task.Run(async() =>
        {
            try
            {
                var videoStreamIn       = new DataReader(videoSocket.InputStream);
                videoStreamIn.ByteOrder = ByteOrder.LittleEndian; // need to set this or the bytes will be read in the wrong order

                videoOut           = new DataWriter(videoSocket.OutputStream);
                videoOut.ByteOrder = ByteOrder.LittleEndian;

                //byte[] buffer = new byte[640 * 640 * 3];

                // load the bytes for the first integer, the image size
                var count = await videoStreamIn.LoadAsync(4);

                while (true)
                {
                    int length = videoStreamIn.ReadInt32(); // first read the image size (in # of bytes)

                    //Debug.Log("Attempting to read image of length " + length);

                    // read the image
                    byte[] buffer = new byte[length];
                    count         = await videoStreamIn.LoadAsync((uint)length);
                    videoStreamIn.ReadBytes(buffer);
                    //ImageReceived?.Invoke(buffer);

                    // convert the bytes to a stream
                    var imgStream = buffer.AsBuffer().AsStream().AsRandomAccessStream();

                    //Debug.Log("Read image, decoding");

                    // decode the image
                    var decoder = await BitmapDecoder.CreateAsync(imgStream);
                    imgStream.Seek(0);
                    int width        = (int)decoder.PixelWidth;
                    int height       = (int)decoder.PixelHeight;
                    var data         = await decoder.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, new BitmapTransform(), ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage);
                    var decodedBytes = data.DetachPixelData();

                    // display the image
                    ImageReceived?.Invoke(decodedBytes, width, height);
                    //Debug.Log("Received image");

                    // clean up memory
                    buffer       = null;
                    data         = null;
                    decodedBytes = null;
                    GC.GetTotalMemory(true);

                    // start to read the next image
                    count = await videoStreamIn.LoadAsync(4);
                }
            }
            catch { LostConnection?.Invoke(); connected = false; }
        });
    }