Exemplo n.º 1
0
        byte[] getBytes(puffer str)
        {
            //create big enough byte array
            int size = Marshal.SizeOf(str);

            byte[] arr = new byte[size];

            //pointer to unorganized memory
            IntPtr ptr = Marshal.AllocHGlobal(size);

            //struct to block of unorganized memory
            Marshal.StructureToPtr(str, ptr, true);
            //copies to organized memory array
            Marshal.Copy(ptr, arr, 0, size);
            //clear unorganized memory space
            Marshal.FreeHGlobal(ptr);
            return(arr);
        }
Exemplo n.º 2
0
        puffer fromBytes(byte[] arr)
        {
            puffer str = new puffer();

            int size = Marshal.SizeOf(str);
            //pointer to unorganized memory
            IntPtr ptr = Marshal.AllocHGlobal(size);

            //byte array to unorganized ptr memory
            Marshal.Copy(arr, 0, ptr, size);

            //unorganized memory to specified structure
            str = (puffer)Marshal.PtrToStructure(ptr, str.GetType());
            //clear unorganized memory space
            Marshal.FreeHGlobal(ptr);

            return(str);
        }
Exemplo n.º 3
0
        public void ReadCallback(IAsyncResult ar)
        {
            try
            {
                int bytesRead = cd1.s.EndReceive(ar);
                if (bytesRead > 0)
                {
                    //handling of byte message
                    puf = this.fromBytes(buffer);
                    switch (puf.id)
                    {
                    case 0:
                        z.FillRectangle(new SolidBrush(Color.White), 0, 0, ClientSize.Width, ClientSize.Height);
                        this.Refresh();
                        this.Text = "Wurde Refreshed!";
                        break;

                    case 1:
                        z.DrawString(puf.textmsg, myFont, /* new SolidBrush(Color.FromArgb(puf.color))*/ myBrush, puf.x1, puf.y1);
                        this.Refresh();
                        this.Text = "Text gesetzt!";
                        break;

                    case 2:
                        this.Invoke((MethodInvoker)(() => z.DrawLine(myPen, new Point(puf.x2, puf.y2), new Point(puf.x1, puf.y1))));
                        count++;
                        if (count % 20 == 0)
                        {
                            this.Refresh();
                            this.Text = "Wurde gemalt!";
                        }
                        break;

                    case 3:
                        z.DrawLine(myPen, new Point(puf.x2, puf.y2), new Point(puf.x1, puf.y1));
                        this.Refresh();
                        this.Text = "Linie gezogen!";
                        break;

                    case 4:
                        z.DrawRectangle(myPen, puf.x1, puf.y1, puf.x2, puf.y2);
                        this.Refresh();
                        this.Text = "Rechteck gezogen!!";
                        break;
                    }

                    //End of Byte handling
                    if (cd1.connected)
                    {
                        cd1.s.BeginReceive(buffer, 0, buffer.Length, 0,
                                           new AsyncCallback(ReadCallback), cd1.s);
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                cd1.statusField.BackColor = Color.Red;
                cd1.s.Close();
            }
        }