Пример #1
0
        public MainWindow()
        {
            InitializeComponent();

            m_hostList = new Dictionary<long, IPEndPoint[]>();

            NetPeerConfiguration config = new NetPeerConfiguration("game");
            config.EnableMessageType(NetIncomingMessageType.UnconnectedData);
            m_client = new NetClient(config);
            m_client.Start();

            Thread thread = new Thread(delegate() {
                while(true) {
                    NetIncomingMessage inc;
                    while ((inc = m_client.ReadMessage()) != null) {
                        switch (inc.MessageType) {
                            case NetIncomingMessageType.VerboseDebugMessage:
                            case NetIncomingMessageType.DebugMessage:
                            case NetIncomingMessageType.WarningMessage:
                            case NetIncomingMessageType.ErrorMessage:
                                break;
                            case NetIncomingMessageType.UnconnectedData:
                                if (inc.SenderEndPoint.Equals(m_masterServer)) {
                                    // it's from the master server - must be a host
                                    var id = inc.ReadInt64();
                                    var hostInternal = inc.ReadIPEndPoint();
                                    var hostExternal = inc.ReadIPEndPoint();

                                    string serverName = inc.ReadString();
                                    string scriptName = inc.ReadString();
                                    int players = inc.ReadInt32();
                                    int maxPlayers = inc.ReadInt32();

                                    m_hostList[id] = new IPEndPoint[] { hostInternal, hostExternal };

                                    Application.Current.Dispatcher.Invoke(delegate {
                                        ServerEntry serverEntry = null;
                                        foreach(ServerEntry entry in spServerList.Children) {
                                            if(Convert.ToInt64(entry.Tag) == id) {
                                                serverEntry = entry;
                                            }
                                        }

                                        if(serverEntry == null) {
                                            serverEntry = new ServerEntry("v0.1", serverName, players, maxPlayers, -1, scriptName);
                                            serverEntry.Tag = id;
                                            serverEntry.UpdateView();
                                            spServerList.Children.Add(serverEntry);
                                        }
                                        else {
                                            serverEntry.ServerName = serverName;
                                            serverEntry.Players = players;
                                            serverEntry.Script = scriptName;
                                            serverEntry.MaxPlayers = maxPlayers;
                                            serverEntry.UpdateView();
                                        }
                                    });
                                }
                                break;
                        }
                    }
                }
            });
            thread.IsBackground = true;
            thread.Start();
        }
Пример #2
0
 private void TestButtonClick(object sender, RoutedEventArgs e)
 {
     ServerEntry serverEntry = new ServerEntry("v0.1", "dev server", 0, 32, -1, "Test script");
     serverEntry.UpdateView();
     spServerList.Children.Add(serverEntry);
 }