예제 #1
0
        /// <summary>
        /// Pairs between a client and a server
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        public void Pair(string ip, int port)
        {
            compressor = null;
            endpoint   = new IPEndPoint(IPAddress.Parse(ip), port);
            socket.Connect(endpoint);

            RISPImageStructure rispImage = OnRequestImage.Invoke(this);

            if (!rispImage.Validate)
            {
                throw new RispInvalidImageException();
            }

            NetBuffer buffer = new NetBuffer(512);

            socket.Send(buffer.Write(rispImage.width).Write(rispImage.height));
            socket.Receive(buffer);

            if (buffer.Length != 8)
            {
                throw new RispDimensionsTooLargeException();
            }
            socket.Send(buffer);

            socket.Receive(buffer);
            int code = buffer.GetInt(0);

            if (code == -2)
            {
                throw new RispDimensionMismatchException();
            }

            for (int i = 0; i < rispImage.data.Length; i++)
            {
                buffer.Write(rispImage.data[i]);
                if (buffer.Length >= 512)
                {
                    socket.Send(buffer);
                    buffer.Reset();
                }
            }
            buffer.Write(0x01000000);
            socket.Send(buffer);

            socket.Receive(buffer);
            code = buffer.GetInt(0);
            if (code != 2)
            {
                throw new RispPairDeclinedException();
            }
            compressor = new IndexCompressor((uint)rispImage.width, (uint)rispImage.height, 0x01000000);
        }
예제 #2
0
        /// <summary>
        /// Runs the server
        /// </summary>
        public void Start(object state)
        {
            NetBuffer         buffer = new NetBuffer(bufferSize);
            CancellationToken cts    = (CancellationToken)state;

            while (true)
            {
                // Listen for a new connection
                server.Listen(1);
                using (Socket socket = server.Accept())
                {
                    // Attempt pairing with the new client
                    socket.Receive(buffer);
                    // Receive the dimensions of the image
                    uint requestedWidth  = buffer.GetUInt(0);
                    uint requestedHeight = buffer.GetUInt(4);

                    // Dimesions are too large
                    if (requestedHeight > MAX_DIMENSION_SIZE || requestedWidth > MAX_DIMENSION_SIZE)
                    {
                        socket.Send(buffer.Reset().Write(DIMENSIONS_TOO_LARGE));
                        continue;
                    }

                    // Sends the dimensions to client to verify
                    socket.Send(buffer);
                    socket.Receive(buffer);
                    // Dimensions mismatch
                    if (requestedHeight != buffer.GetUInt(4) || requestedWidth != buffer.GetUInt(0))
                    {
                        socket.Send(buffer.Write(DIMENSIONS_MISMATCH));
                        continue;
                    }

                    StreamImage     image      = new StreamImage((int)requestedWidth, (int)requestedHeight);
                    IndexCompressor compressor = new IndexCompressor(requestedWidth, requestedHeight, 0x01000000);

                    // Initialize image
                    socket.Send(buffer.Reset().Write(REQUEST_IMAGE));

                    int  x     = 0;
                    int  y     = 0;
                    bool ended = false;

                    do
                    {
                        socket.Receive(buffer);

                        int i = 0;
                        while (i < buffer.Length - sizeof(int))
                        {
                            image.SetPixel(x, y, buffer.GetInt(i));
                            i += sizeof(int);
                            x++;
                            if (x >= requestedWidth)
                            {
                                x = 0;
                                y++;
                            }
                        }

                        if (buffer.GetUInt(i) == END_OF_IMAGE)
                        {
                            ended = true;
                        }
                        else
                        {
                            image.SetPixel(x, y, buffer.GetInt(i));
                            x++;
                            if (x >= requestedWidth)
                            {
                                x = 0;
                                y++;
                            }
                        }
                    } while (!ended);
                    OnConnectionAstablished?.Invoke(image.Width, image.Height, image.image, this);
                    socket.Send(buffer.Reset().Write(PAIR_SUCCESSFUL));

                    IIndexable pixel = new RISPImagePixel();
                    // Start streaming loop
                    while (true)
                    {
                        try
                        {
                            socket.Receive(buffer);

                            if (buffer.GetULong(0) == TERMINATE_CONNECTION)
                            {
                                break;
                            }

                            int index = 0;
                            RISPImagePixel[] diffrences = new RISPImagePixel[buffer.Length / sizeof(ulong)];
                            while (index < buffer.Length)
                            {
                                ulong compressedValue = buffer.GetULong(index);
                                compressor.Decompress(compressedValue, ref pixel);

                                RISPImagePixel iPixel = (RISPImagePixel)pixel;
                                System.Console.WriteLine(iPixel.color.ToString("X"));
                                diffrences[index / sizeof(ulong)] = iPixel;
                                image.SetPixel((int)iPixel.x, (int)iPixel.y, new RGB((int)iPixel.color));

                                index += sizeof(ulong);
                            }
                            // Call stream updates
                            if (diffrences.Length > 0)
                            {
                                OnStreamUpdate?.Invoke(diffrences, this);
                            }
                        }
                        catch (SocketException)
                        {
                            break;
                        }
                    }
                }
            }
        }