コード例 #1
0
            protected override void OnMessage(MessageEventArgs e)
            {
                if (!e.IsBinary)
                {
                    CloseSocket();
                    return;
                }

                byte[] buf = e.RawData;
                if (buf.Length == 0)
                {
                    CloseSocket();
                    return;
                }

                Command cmd = (Command)buf[0];

                try
                {
                    switch (cmd)
                    {
                    case Command.StartStreaming:
                        if (buf.Length < 3)
                        {
                            SyntaxError(buf[0], buf);
                            break;
                        }
                        StopStreaming();
                        Console.WriteLine(ID + " Command.StartStreaming " + (byte)Interlocked.Read(ref activeStreamNumber));
                        Send(new byte[] { buf[0], (byte)Interlocked.Read(ref activeStreamNumber) });
                        StartStreaming(buf);
                        break;

                    case Command.StopStreaming:
                        Console.WriteLine(ID + " Command.StopStreaming");
                        StopStreaming();
                        break;

                    case Command.AcknowledgeFrame:
                        if (buf.Length < 2)
                        {
                            SyntaxError(buf[0], buf);
                            break;
                        }
                        AcknowledgeFrame(buf[1]);
                        break;

                    case Command.ReproduceUserInput:
                        if (buf.Length < 2)
                        {
                            SyntaxError(buf[0], buf);
                            break;
                        }
                        InputType inputType = (InputType)buf[1];
                        if (inputType == InputType.KeyDown || inputType == InputType.KeyUp)
                        {
                            if (buf.Length < 10)
                            {
                                SyntaxError(buf[0], buf, inputType);
                                break;
                            }
                            int          keyCode   = ByteUtil.ReadInt32(buf, 2);
                            ModifierKeys modifiers = (ModifierKeys)ByteUtil.ReadUInt32(buf, 6);
                            streamerController.DoKeyboardInput(keyCode, modifiers, inputType == InputType.KeyUp);
                        }
                        else if (inputType == InputType.MouseMove)
                        {
                            if (buf.Length < 10)
                            {
                                SyntaxError(buf[0], buf, inputType);
                                break;
                            }
                            float x = ByteUtil.ReadFloat(buf, 2);
                            float y = ByteUtil.ReadFloat(buf, 6);
                            streamerController.DoMouseMove(x, y);
                        }
                        else if (inputType == InputType.MouseButtonDown || inputType == InputType.MouseButtonUp)
                        {
                            if (buf.Length < 3)
                            {
                                SyntaxError(buf[0], buf, inputType);
                                break;
                            }
                            MouseButton button = (MouseButton)buf[2];
                            streamerController.DoMouseButton(button, inputType == InputType.MouseButtonUp);
                        }
                        else if (inputType == InputType.MouseWheel)
                        {
                            if (buf.Length < 6)
                            {
                                SyntaxError(buf[0], buf, inputType);
                                break;
                            }
                            short deltaX = ByteUtil.ReadInt16(buf, 2);
                            short deltaY = ByteUtil.ReadInt16(buf, 4);
                            streamerController.DoMouseWheel(deltaX, deltaY);
                        }
                        break;

                    case Command.GetDesktopInfo:
                        DesktopInfo desktopInfo = streamerController.GetDesktopInfo();
                        using (MemoryDataStream mds = new MemoryDataStream())
                        {
                            desktopInfo.WriteToDataStream(mds);
                            Send(mds.ToArray());
                        }
                        break;

                    case Command.SetStreamSettings:
                        if (buf.Length < 5)
                        {
                            SyntaxError(buf[0], buf);
                            break;
                        }

                        imgFlags                = (ImgFlags)buf[1];
                        jpegQuality             = BPMath.Clamp <byte>(buf[2], 1, 100);
                        maxFPS                  = BPMath.Clamp <byte>(buf[3], 1, byte.MaxValue);
                        maxUnacknowledgedFrames = BPMath.Clamp <byte>(buf[4], 1, byte.MaxValue);
                        break;

                    case Command.GetStreamSettings:
                        byte[] response = new byte[4];
                        response[0] = buf[0];
                        response[1] = (byte)imgFlags;
                        response[2] = jpegQuality;
                        response[3] = maxFPS;
                        Send(response);
                        break;

                    case Command.KeepAlive:
                        break;

                    default:
                        // CloseSocket();
                        Send(new byte[] { (byte)Command.Error_CommandCodeUnknown });
                        break;
                    }
                }
                catch (ThreadAbortException) { throw; }
                catch (Exception ex)
                {
                    Logger.Debug(ex, "WebSocketServer");
                }
            }
コード例 #2
0
        private static void mainThreadRunner()
        {
            try
            {
                screenCapturer = new AdvScreenCapture();
                dxgiDuplicator = new DxgiOutputDuplicator(0, 0);
                inputEmulator  = new InputEmulator();
                Logger.Info("Application start shared memory id " + streamerArgs.SharedMemoryId);
                int ownerPID = streamerArgs.ServiceProcessId == null ? 0 : streamerArgs.ServiceProcessId.Value;
                using (SharedMemoryStream sm = SharedMemoryStream.OpenSharedMemoryStream(streamerArgs.SharedMemoryId, ownerPID))
                {
                    try
                    {
                        static_sm = sm;
                        thrDesktopCapture.Start();
                        while (!isExiting)
                        {
                            Command commandCode = (Command)sm.ReadByte();
                            // Handle
                            switch (commandCode)
                            {
                            case Command.GetScreenCapture:
                                ImgFlags imgFlags    = (ImgFlags)sm.ReadByte();
                                byte     jpegQuality = (byte)sm.ReadByte();
                                desktopCaptureTasks.Enqueue(new DesktopCaptureTask(imgFlags, jpegQuality));
                                break;

                            //case Command.CaptureCompressedDesktopImage:
                            //	CaptureCompressedDesktopImage(sm);
                            //	break;
                            case Command.ReproduceUserInput:
                                inputEmulator.EmulateInput(sm);
                                break;

                            case Command.GetDesktopInfo:
                                lock (sm)
                                {
                                    desktopInfo.WriteToDataStream(sm);
                                }
                                break;

                            case Command.KeepAlive:
                                break;

                            default:
                                Logger.Debug("Unsupported command code received: " + commandCode);
                                lock (sm)
                                {
                                    sm.WriteByte((byte)Command.Error_CommandCodeUnknown);
                                }
                                break;
                            }
                        }
                    }
                    finally
                    {
                        static_sm = null;
                    }
                }
            }
            catch (ThreadAbortException) { }
            catch (StreamDisconnectedException ex)
            {
                Logger.Info("Exiting because: " + ex.Message);
            }
            catch (Exception ex)
            {
                Logger.Debug(ex);
                Logger.Info("Exiting due to main thread runner exception");
            }
            finally
            {
                Try.Catch(() => { dxgiDuplicator?.Dispose(); });
                Try.Catch(() => { screenCapturer?.Dispose(); });
                //Try.Catch(() => { inputEmulator?.Dispose(); });
                RobustExit();
            }
        }