Example #1
0
        private static void SetupMessageHandlers()
        {
            connection.AppendIncomingPacketHandler <byte>("GetCameraDevices", (header, connection, data) =>
            {
                string[] names       = WebcamHelper.GetDeviceNames();
                VideoDevices devices = new VideoDevices()
                {
                    videoDevices = new VideoDevice[names.Length]
                };
                for (int i = 0; i < devices.videoDevices.Length; i++)
                {
                    VideoCapabilities[] capabilities = WebcamHelper.GetDeviceCapabilities(i);
                    devices.videoDevices[i]          = new VideoDevice()
                    {
                        name         = names[i],
                        capabilities = new VideoDeviceCapabilities[capabilities.Length]
                    };
                    for (int j = 0; j < capabilities.Length; j++)
                    {
                        devices.videoDevices[i].capabilities[j] = new VideoDeviceCapabilities()
                        {
                            averageFramerate = capabilities[j].AverageFrameRate,
                            maxFramerate     = capabilities[j].MaximumFrameRate,
                            height           = capabilities[j].FrameSize.Height,
                            width            = capabilities[j].FrameSize.Width
                        };
                    }
                }
                connection.SendObject <byte[]>("CameraDevices", Serializer.Serialize <VideoDevices>(devices, encryptionKey));
            });

            connection.AppendIncomingPacketHandler <byte[]>("GetCameraImage", (header, connection, data) =>
            {
                GetImage image = Serializer.Deserialize <GetImage>(data, encryptionKey);
                WebcamHelper.GetWebcamImage(image.deviceIndex, image.width, image.height, (bitmapImage) =>
                {
                    connection.SendObject <byte[]>("CameraImage", Serializer.Serialize <ByteData>(new ByteData()
                    {
                        data = WebcamHelper.GetImagePNGBytes(bitmapImage)
                    }, encryptionKey));
                });
            });

            connection.AppendIncomingPacketHandler <byte>("GetDesktopImage", (header, connection, data) =>
            {
                connection.SendObject <byte[]>("DesktopImage", Serializer.Serialize <ByteData>(new ByteData()
                {
                    data = WebcamHelper.GetImagePNGBytes(WebcamHelper.GetDesktopScreenshot())
                }, encryptionKey));
            });

            connection.AppendIncomingPacketHandler <byte>("GetLocation", (header, connection, data) =>
            {
                LocationHelper.GetPosition((location) =>
                {
                    connection.SendObject <byte[]>("Location", Serializer.Serialize <DeviceLocation>(location, encryptionKey));
                });
            });
        }