コード例 #1
0
ファイル: Form1.cs プロジェクト: mloty53/Picture-multiClient
        void watchForData()
        {
            try
            {
                while (connected == true)
                {
                    // Tutaj pobieramy dane rysownicze od serwera i coś tam robione jest
                    IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
                    byte[]     receivedBytes  = udpClient.Receive(ref remoteEndPoint);

                    byte id = receivedBytes[0];
                    switch (receivedBytes[1])
                    {
                    case 0x01:
                    {
                        byte[] color = new byte[4];
                        Buffer.BlockCopy(receivedBytes, 2, color, 0, color.Length);
                        udpClients[id] = new PaintData(Color.FromArgb(BitConverter.ToInt32(color, 0)), new Point(0));
                        break;
                    }

                    case 0x02:
                    {
                        try
                        {
                            var    client = udpClients[id];
                            byte[] pos    = new byte[4];
                            Buffer.BlockCopy(receivedBytes, 2, pos, 0, pos.Length);
                            Point newPos = new Point(BitConverter.ToInt32(pos, 0));

                            if (client.StartPos.IsEmpty == true)
                            {
                                client.StartPos = newPos;
                            }

                            using (Pen p = new Pen(client.Color, 5.0F))
                            {
                                // Działamy na osobnym wątku, dlatego rysowanie na kontrolce
                                // musimy albo zlecić, albo wykonać od razu.
                                // Jeśli wykonamy od razu, to jest szansa na "wpadkę" i wyjątek, dlatego
                                // zlecamy jeśli jest to potrzebne
                                if (pictureBox1.InvokeRequired)
                                {
                                    pictureBox1.Invoke(new Action(() =>
                                        {
                                            var graphics = Graphics.FromImage(pictureBox1.Image);
                                            graphics.DrawLine(p, client.StartPos, newPos);
                                            pictureBox1.Invalidate();
                                        }));
                                }
                                else
                                {
                                    var graphics = Graphics.FromImage(pictureBox1.Image);
                                    graphics.DrawLine(p, client.StartPos, newPos);
                                    pictureBox1.Invalidate();
                                }
                            }
                            client.StartPos = newPos;
                        }
                        catch (KeyNotFoundException) {}
                        break;
                    }

                    case 0x03:
                    {
                        try
                        {
                            var client = udpClients[id];
                            client.StartPos = new Point(0);
                        }
                        catch (KeyNotFoundException) {}
                        break;
                    }
                    }
                }
            }
            catch (SocketException e)
            {
                if (connected == true)
                {
                    MessageBox.Show(e.Message + "\n" + e.StackTrace);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                MessageBox.Show(e.Message + "\n" + e.StackTrace);
            }
        }
コード例 #2
0
        void ProcessMessage(Message message)
        {
            BinaryReader reader = message.Reader;

            switch (message.Type)
            {
            case MessageType.Pong:
            {
                ReceivePong(reader.ReadInt32());
                break;
            }

            case MessageType.ServerHello:
            {
                int port = reader.ReadInt32();
                serverIP = new IPEndPoint(serverIPConnection.Address, port);
                id       = reader.ReadInt32();

                if (form1.InvokeRequired)
                {
                    form1.Invoke(new Action(() =>
                        {
                            form1.txtStatus.Text = "connected";
                        }));
                }

                if (connectionTask != null && !connectionTask.IsCanceled && !connectionTask.IsFaulted && !connectionTask.IsCompleted)
                {
                    connectionTask.Dispose();
                }

                if (mainTask != null && !mainTask.IsCanceled && !mainTask.IsFaulted && !mainTask.IsCompleted)
                {
                    mainTask.Dispose();
                }

                mainTask = Task.Run(new Action(netClient.Start));

                break;
            }

            case MessageType.ServerUpdate:
                byte penType = reader.ReadByte();
                byte clientId;
                switch ((MessageType)penType)
                {
                case MessageType.ClientUpdateStarted:
                    clientId = reader.ReadByte();
                    int color = reader.ReadInt32();

                    udpClients[clientId] = new PaintData(Color.FromArgb(color), new Point(0));
                    break;


                case MessageType.ClientUpdateInProgress:
                {
                    try
                    {
                        clientId = reader.ReadByte();
                        var client1 = udpClients[clientId];

                        short pointX = reader.ReadInt16();
                        short pointY = reader.ReadInt16();

                        Point newPos = new Point(pointX, pointY);

                        if (client1.StartPos.IsEmpty == true)
                        {
                            client1.StartPos = newPos;
                        }

                        using (Pen p = new Pen(client1.Color, 5.0F))
                        {
                            // Działamy na osobnym wątku, dlatego rysowanie na kontrolce
                            // musimy albo zlecić, albo wykonać od razu.
                            // Jeśli wykonamy od razu, to jest szansa na "wpadkę" i wyjątek, dlatego
                            // zlecamy jeśli jest to potrzebne
                            if (form1.pictureBox1.InvokeRequired)
                            {
                                form1.pictureBox1.Invoke(new Action(() =>
                                    {
                                        var graphics = Graphics.FromImage(form1.pictureBox1.Image);
                                        graphics.DrawLine(p, client1.StartPos, newPos);
                                        form1.pictureBox1.Invalidate();
                                    }));
                            }
                            else
                            {
                                var graphics = Graphics.FromImage(form1.pictureBox1.Image);
                                graphics.DrawLine(p, client1.StartPos, newPos);
                                form1.pictureBox1.Invalidate();
                            }
                        }
                        client1.StartPos = newPos;
                    }
                    catch (KeyNotFoundException) { }
                    break;
                }

                case MessageType.ClientUpdateInEnd:
                    clientId = reader.ReadByte();
                    var client = udpClients[clientId];
                    client.StartPos = new Point(0);
                    break;
                }

                break;
            }



            message.Reader.Close();
            message.Stream.Close();
        }