Example #1
0
        private void ProcessPacket(Packet p)
        {
            if (p.Index != (short)ClientPacketIds.ServerList)
            {
                return;
            }

            S.ServerList serverList = new S.ServerList();

            GameServerInfo info = new GameServerInfo()
            {
                Name = "天域归来",
                Port = 7000,
            };

            serverList.servers.Add(info);
            Enqueue(serverList);
        }
Example #2
0
        private void RequestServerList()
        {
            TcpClient client = new TcpClient {
                NoDelay = true
            };

            client.Connect(Settings.IPAddress, 6500);

            NetworkStream ntwStream = client.GetStream();

            if (ntwStream.CanWrite)
            {
                ClientPackets.ServerList packet = new ClientPackets.ServerList();
                byte[] data = (byte[])packet.GetPacketBytes();
                ntwStream.Write(data, 0, data.Length);

                byte[] rawData  = null;
                byte[] readData = new Byte[256];
                while (true)
                {
                    Thread.Sleep(1);
                    Int32 dataRead = ntwStream.Read(readData, 0, readData.Length);

                    if (rawData == null)
                    {
                        rawData = new byte[readData.Length];
                        Buffer.BlockCopy(readData, 0, rawData, 0, dataRead);
                    }
                    else
                    {
                        byte[] temp = rawData;
                        rawData = new byte[dataRead + temp.Length];
                        Buffer.BlockCopy(temp, 0, rawData, 0, temp.Length);
                        Buffer.BlockCopy(readData, 0, rawData, temp.Length, dataRead);
                    }

                    Packet p = Packet.ReceivePacket(rawData, out rawData);
                    if (p != null)
                    {
                        switch (p.Index)
                        {
                        case (short)ServerPacketIds.ServerList:
                        {
                            S.ServerList serverlist = (S.ServerList)p;

                            TreeNode node1 = new TreeNode("服务器列表");
                            serverTreeView.Nodes.Add(node1);
                            for (int j = 0; j < serverlist.servers.Count; ++j)
                            {
                                node1.Nodes.Add(serverlist.servers[j].Name);
                            }
                            Launch_pb.Enabled = true;
                        }
                        break;
                        }
                    }

                    if (Launch_pb.Enabled)
                    {
                        break;
                    }
                }

                ntwStream.Close();
                client.Close();
            }
        }