Exemplo n.º 1
0
 private void Client_OnReadMessage(ClientInfo ci, uint code, byte[] bytes, int len)
 {
     if (code == 0) //String
     {
         Console.WriteLine("Received msg from Client[" + ci.ID + "]: " + Encoding.UTF8.GetString(bytes));
         if (ReceiveMessage != null)
         {
             ReceiveMessage.Invoke(Encoding.UTF8.GetString(bytes), EventArgs.Empty);
         }
     }
     else if (code == 1) //File
     {
         File.WriteAllBytes("file.txt", bytes);
     }
     else if (code == 2) //Projection
     {
         string        ret        = string.Empty;
         ProjectionObj projection = ProjectionObj.Deserialize(bytes);
         ReceiveProjection.Invoke(projection, EventArgs.Empty);
     }
     else if (code == 3) //ProjectionManger
     {
         ProjectionManager recManager = ProjectionManager.Deserialize(bytes);
         ReceiveProjectionManager.Invoke(recManager, EventArgs.Empty);
     }
     else
     {
         Console.WriteLine("Unknown msg from server, disregarding...");
     }
 }
        public Projection_Server(bool debugMode = false)
        {
            InitializeComponent();
            lblIP.Parent    = pbProject;
            lblIP.Font      = new Font(lblIP.Font.FontFamily, 16.0f); // FontStyle.Bold, 32.0f);
            lblIP.TextAlign = ContentAlignment.MiddleCenter;
            IPAddress[] ipv4Addresses = Array.FindAll(Dns.GetHostEntry(string.Empty).AddressList,
                                                      a => a.AddressFamily == AddressFamily.InterNetwork);
            lblIP.BackColor = Color.White;
            lblIP.Text      = ipv4Addresses.Last().ToString();
            warmup          = true;

            transitionTimer = new Timer()
            {
                Interval = 25
            };
            transitionStep = 0;

            if (debugMode)
            {
                StartPosition = FormStartPosition.Manual;
                Left          = 0;
                Top           = 0;
                Left          = 2000;
                debugMode     = false;
            }

            intervalWatch = new Stopwatch();
            //saveProjectionManagerWatch = new Stopwatch();
            //intervalWatch.Start();
            //saveProjectionManagerWatch.Start();



            //Clear out base image
            pbProject.Image = null;

            if (!debugMode)
            {
                WindowState = FormWindowState.Maximized;
            }
            //pbProject.Dock = DockStyle.None;

            if (!File.Exists("collection.pm"))
            {
                projectionManager = new ProjectionManager(Properties.Resources.scenic1);
                //Projection defaultProj = new Projection(DateTime.Now, DateTime.MinValue, DateTime.MaxValue)
                //{
                //    isDefault = true
                //};
            }
            else //Serialized data found
            {
                try
                {
                    projectionManager = ProjectionManager.Deserialize("collection.pm");
                    if (projectionManager == null)
                    {
                        throw new Exception("Failed to deserialize ProjectionManager state :(");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.StackTrace);
                    MessageBox.Show(this, "There was an error loading previously scheduled projections.\nData may be corrupt.", "Projection", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    projectionManager = new ProjectionManager(Properties.Resources.scenic1);
                }
            }

            displayServer = new DisplayServer();
            displayServer.projectionManager   = projectionManager;
            displayServer.ReceivedProjection += ReceiveProjection;
            displayServer.Start();

            tmrUpdate.Start();
            intervalWatch.Start();
        }