예제 #1
0
        /// <summary>
        /// Check for external updates.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void outputTimer_Tick(object sender, EventArgs e)
        {
            //Show console messages.
            while (Util.Terminal.output.Count != 0)
            {
                txtConsole.AppendText(Util.Terminal.output.Dequeue());
            }

            if (SyncServer != null)
            {
                //check for new clients.
                {
                    GameSocket gs = SyncServer.getSocket();
                    if (gs != null)
                    {
                        Util.Terminal.WriteLine("New Sync Client Connected [" + ((glSocket)gs).IPAddress + "]");
                        clientList.Add(gs);
                    }
                }

                //check for DC'd clients.
                Queue <GameSocket> toDestroy = new Queue <GameSocket>();
                foreach (GameSocket gs in clientList)
                {
                    if (!gs.isConnected)
                    {
                        toDestroy.Enqueue(gs);
                    }
                }

                //remove DC'd clients.
                while (toDestroy.Count != 0)
                {
                    clientList.Remove(toDestroy.Dequeue());
                }

                //Do we have any games to collect?
                lock (AchronWeb.features.consts.gameSendList)
                {
                    foreach (GameSocket gs in clientList)
                    {
                        if (gs.isConnected)
                        {
                            PacketData data = gs.GetData();
                            if (data != null)
                            {
                                data.beginRead();
                                AchronWeb.features.achronGame remoteGame = jsonToGame(JsonValue.Parse(data.readString()));

                                if (remoteGame.host == "127.0.0.1" || remoteGame.host == "::1")
                                {
                                    remoteGame.host = ((glSocket)gs).IPAddress;
                                }

                                //send the game to other clients too.
                                foreach (GameSocket gsN in clientList)
                                {
                                    if (gsN == gs)
                                    {
                                        continue;
                                    }
                                    else
                                    {
                                        PacketData outData = new PacketData(0x01, 0x02);
                                        outData.writeString(gameToJson(remoteGame).ToString());
                                        gsN.SendData(outData);
                                    }
                                }

                                lock (AchronWeb.features.consts.gameList)
                                {
                                    AchronWeb.features.consts.gameCount++;
                                    remoteGame.lastUpdate = AchronWeb.features.consts.GetTime();
                                    remoteGame.gameID     = AchronWeb.features.consts.gameCount;
                                    AchronWeb.features.consts.gameList.Add(remoteGame.gameID, remoteGame);
                                }

                                Util.Terminal.WriteLine("New Remote Game: " + remoteGame.gameName + " / " + remoteGame.level + " / " + remoteGame.host);
                            }
                        }
                    }
                }

                //Do we got any games to send?
                lock (AchronWeb.features.consts.gameSendList)
                {
                    if (AchronWeb.features.consts.gameSendList.Count != 0)
                    {
                        JsonValue toSend = gameToJson(AchronWeb.features.consts.gameSendList.Dequeue());
                        foreach (GameSocket gs in clientList)
                        {
                            if (gs.isConnected)
                            {
                                PacketData pd = new PacketData(0x01, 0x02);
                                pd.writeString(toSend.ToString());
                                gs.SendData(pd);
                            }
                        }
                    }
                }

                txtServerStatus.Text = "Clients: " + clientList.Count;
            }

            if (SyncClient != null)
            {
                if (!SyncClient.isConnected)
                {
                    txtClientStatus.Text  = "Connection Lost";
                    SyncClient            = null;
                    btnHost.Enabled       = true;
                    btnJoinServer.Enabled = true;
                }
                else
                {
                    PacketData data = SyncClient.GetData();
                    if (data != null) //we have a packet.
                    {
                        data.beginRead();
                        AchronWeb.features.achronGame remoteGame = jsonToGame(JsonValue.Parse(data.readString()));

                        if (remoteGame.host == "127.0.0.1" || remoteGame.host == "::1")
                        {
                            remoteGame.host = SyncClient.IPAddress;
                        }

                        lock (AchronWeb.features.consts.gameList)
                        {
                            AchronWeb.features.consts.gameCount++;
                            remoteGame.lastUpdate = AchronWeb.features.consts.GetTime();
                            remoteGame.gameID     = AchronWeb.features.consts.gameCount;
                            AchronWeb.features.consts.gameList.Add(remoteGame.gameID, remoteGame);
                        }

                        Util.Terminal.WriteLine("New Remote Game: " + remoteGame.gameName + " / " + remoteGame.level + " / " + remoteGame.host);
                    }

                    //Do we gots a game to send?
                    if (AchronWeb.features.consts.gameSendList.Count != 0)
                    {
                        JsonValue toSend = gameToJson(AchronWeb.features.consts.gameSendList.Dequeue());

                        if (SyncClient.isConnected)
                        {
                            PacketData pd = new PacketData(0x01, 0x02);
                            pd.writeString(toSend.ToString());
                            SyncClient.SendData(pd);
                        }
                    }

                    txtClientStatus.Text = "Connected";
                }
            }
        }