Example #1
0
        public void TestSocketParse()
        {
            byte[]      data       = ("这是一条测试数据").ToBytes();
            SendPacket  sendPacket = new SendPacket(data);
            List <byte> dataList   = new List <byte>();

            dataList.AddRange(sendPacket.PackData());
            dataList.AddRange(sendPacket.PackData());
            data = dataList.ToArray();
            RecievePacket recievePacket = new RecievePacket();

            while (data.Length > 0)
            {
                if (recievePacket.ParseData(ref data))
                {
                    string str = recievePacket.GetBytes().ToStringUnicode();
                    Console.WriteLine(str);
                    Assert.AreEqual("这是一条测试数据", str);
                    recievePacket = new RecievePacket();
                }
                else
                {
                    Assert.Fail();
                    break;
                }
            }
        }
Example #2
0
        public void Fillin(RecievePacket Param)
        {
            if (Param != null)
            {
                    if (Param.Map.StartsWith("workshop"))
                {
                    Param.Map = Param.Map.Substring(19);
                }

                    dataGrid.Rows.Add(Param.Name, Param.Players.ToString() + "/" + Param.MaxPlayers + "(" + Param.Bots + ")", Param.Map, Param.PW,Param.Port);
                }
            else
            {
                MessageBox.Show("Error Param of method Fillin equals NULL");
            }
        }
Example #3
0
        /// <summary>
        ///     匹配对应的Command命令
        /// </summary>
        /// <param name="tcpClient"></param>
        /// <param name="packet"></param>
        /// <returns></returns>
        public static P2PCommand FindCommand(P2PTcpClient tcpClient, RecievePacket packet)
        {
            P2PCommand command = null;

            if (Global.AllowAnonymous.Contains(packet.CommandType) || tcpClient.IsAuth)
            {
                if (Global.CommandDict.ContainsKey(packet.CommandType))
                {
                    Type type = Global.CommandDict[packet.CommandType];
                    command = Activator.CreateInstance(type, tcpClient, packet.GetBytes()) as P2PCommand;
                }
            }
            else
            {
                throw new Exception("没有权限");
            }
            return(command);
        }
Example #4
0
        public static void ListenTcp <T>(P2PTcpClient tcpClient) where T : RecievePacket
        {
            byte[]        buffer     = new byte[P2PGlobal.P2PSocketBufferSize];
            NetworkStream tcpStream  = tcpClient.GetStream();
            RecievePacket msgRecieve = Activator.CreateInstance(typeof(T)) as RecievePacket;

            while (tcpClient.Connected)
            {
                int curReadLength = tcpStream.ReadSafe(buffer, 0, buffer.Length);
                if (curReadLength > 0)
                {
                    byte[] refData = buffer.Take(curReadLength).ToArray();
                    while (msgRecieve.ParseData(ref refData))
                    {
                        Debug.WriteLine($"命令类型:{msgRecieve.CommandType}");
                        //todo:执行command
                        P2PCommand command = FindCommand(tcpClient, msgRecieve);
                        if (command != null)
                        {
                            command.Excute();
                        }
                        //重置msgRecieve
                        msgRecieve = Activator.CreateInstance(typeof(T)) as RecievePacket;
                        if (refData.Length <= 0)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    //如果tcp已关闭,需要关闭相关tcp
                    if (tcpClient.ToClient != null && tcpClient.ToClient.Connected)
                    {
                        Debug.WriteLine("tcp已关闭");
                        tcpClient.ToClient.Close();
                    }
                    break;
                }
            }
        }
Example #5
0
 public void handlePacket(short header, RecievePacket packet)
 {
     if (header == (short)RecieveHeaders.SONG_START)
     {
         User u = Server.userList[userName];
         if (u.Name.Equals((string)packet.info[0]))
         {
             u.CurrentChart = (string)packet.info[1];
             u.CurrentSong  = (string)packet.info[2];
             u.Mode         = (User.PlayMode)packet.info[3];
             u.ModFlags     = (int)packet.info[4];
             u.Scroll       = (double)packet.info[5];
             u.Speed        = (double)packet.info[6];
         }
         if (specs.Count > 0 && !u.CurrentChart.Equals("") && (u.Mode == User.PlayMode.INGAME || u.Mode == User.PlayMode.MULTI))
         {
             foreach (KeyValuePair <string, ClientHandler> h in specs)
             {
                 PacketWriter.sendSpectateStart(h.Value, userName, Server.userList[userName].CurrentChart, Server.userList[userName].ModFlags, Server.userList[userName].Scroll);
                 specChart[h.Key] = false;
             }
         }
     }
     else if (header == (short)RecieveHeaders.LOGIN && packet != null)
     {
         User temp = (User)packet.info[0];
         userName      = temp.Name.ToLower(); //tolower unnecessary, but more explicit that userName is lowercase
         temp.Handler  = this;
         myThread.Name = userName;
     }
     //should have some sort of packet converter that converts packet to some subclass with typed fields parsed from the list that's properly named to increase readability
     else if (header == (short)RecieveHeaders.SPECTATE_HOOK)
     {
         if (Server.userList.ContainsKey((string)packet.info[0]))
         {
             try
             {
                 if (speccing != null)
                 {
                     speccing.unspectate(this);
                 }
             }
             catch
             {
             }
             Server.userList[(string)packet.info[0]].Handler.spectate(this);
             if (!Server.userList[(string)packet.info[0]].CurrentChart.Equals(""))
             {
                 PacketWriter.sendSpectateStart(this, (string)packet.info[0], Server.userList[(string)packet.info[0]].CurrentChart,
                                                Server.userList[(string)packet.info[0]].ModFlags, Server.userList[(string)packet.info[0]].Scroll);
             }
             speccing = Server.userList[(string)packet.info[0]].Handler;
         }
     }
     else if (header == (short)RecieveHeaders.SPECTATE_HIT)
     {
         foreach (KeyValuePair <string, ClientHandler> h in specs)
         {
             PacketWriter.sendHit(h.Value, (int)packet.info[0], (int)packet.info[1], (int)packet.info[2], (int)packet.info[3]);
         }
     }
     else if (header == (short)RecieveHeaders.SPECTATE_RELEASE)
     {
         foreach (KeyValuePair <string, ClientHandler> h in specs)
         {
             PacketWriter.sendSpectateRelease(h.Value, (int)packet.info[0], (int)packet.info[1]);
         }
     }
     else if (header == (short)RecieveHeaders.SPECTATE_CANCEL)
     {
         if (Server.userList.ContainsKey((string)packet.info[0]))
         {
             Server.userList[(string)packet.info[0]].Handler.unspectate(this);
             speccing = null;
         }
     }
     else if (header == (short)RecieveHeaders.SPECTATE_PRESS)
     {
         foreach (KeyValuePair <string, ClientHandler> h in specs)
         {
             PacketWriter.sendSpectatePress(h.Value, (int)packet.info[0], (int)packet.info[1]);
         }
     }
     else if (header == (short)RecieveHeaders.SPECTATE_HEARTBEAT)
     {
         foreach (KeyValuePair <string, ClientHandler> h in specs)
         {
             PacketWriter.sendSpectateHeartbeat(h.Value, (int)packet.info[0], (int)packet.info[1], (int)packet.info[2], (int)packet.info[3], (double)packet.info[4]);
         }
     }
     else if (header == (short)RecieveHeaders.SPECTATE_FINISH)
     {
         foreach (KeyValuePair <string, ClientHandler> h in specs)
         {
             PacketWriter.sendSpectateFinish(h.Value, (int)packet.info[0], (int)packet.info[1], (int)packet.info[2], (int)packet.info[3],
                                             (int)packet.info[4], (int)packet.info[5], (int)packet.info[6], (double)packet.info[7]);
         }
     }
     else if (header == (short)RecieveHeaders.SPECTATE_FAIL)
     {
         foreach (KeyValuePair <string, ClientHandler> h in specs)
         {
             PacketWriter.sendSpectateFail(h.Value);
         }
     }
     else if (header == (short)RecieveHeaders.SPECTATE_GOT_CHART)
     {
         if (speccing != null && speccing.specs.Keys.Contains(userName))
         {
             speccing.specChart[userName] = true;
         }
     }
 }
Example #6
0
        public void handlePacket(short header, RecievePacket packet)
        {
            if (header == (short)RecieveHeaders.SONG_START)
            {
                User u = Server.userList[userName];
                if (u.Name.Equals((string)packet.info[0]))
                {
                    u.CurrentChart = (string)packet.info[1];
                    u.CurrentSong = (string)packet.info[2];
                    u.Mode = (User.PlayMode)packet.info[3];
                    u.ModFlags = (int)packet.info[4];
                    u.Scroll = (double)packet.info[5];
                    u.Speed = (double)packet.info[6];
                }
                if (specs.Count > 0 && !u.CurrentChart.Equals("") && (u.Mode == User.PlayMode.INGAME || u.Mode == User.PlayMode.MULTI))
                {
                    foreach (KeyValuePair<string, ClientHandler> h in specs)
                    {
                        PacketWriter.sendSpectateStart(h.Value, userName, Server.userList[userName].CurrentChart, Server.userList[userName].ModFlags, Server.userList[userName].Scroll);
                        specChart[h.Key] = false;
                    }
                }
            }
            else if (header == (short)RecieveHeaders.LOGIN && packet != null)
            {
                User temp = (User)packet.info[0];
                userName = temp.Name.ToLower(); //tolower unnecessary, but more explicit that userName is lowercase
                temp.Handler = this;
                myThread.Name = userName;
            }
            //should have some sort of packet converter that converts packet to some subclass with typed fields parsed from the list that's properly named to increase readability
            else if (header == (short)RecieveHeaders.SPECTATE_HOOK)
            {
                if (Server.userList.ContainsKey((string)packet.info[0]))
                {
                    try
                    {
                        if (speccing != null)
                        {
                            speccing.unspectate(this);
                        }
                    }
                    catch
                    {

                    }
                    Server.userList[(string)packet.info[0]].Handler.spectate(this);
                    if (!Server.userList[(string)packet.info[0]].CurrentChart.Equals(""))
                    {
                        PacketWriter.sendSpectateStart(this, (string)packet.info[0], Server.userList[(string)packet.info[0]].CurrentChart,
                            Server.userList[(string)packet.info[0]].ModFlags, Server.userList[(string)packet.info[0]].Scroll);
                    }
                    speccing = Server.userList[(string)packet.info[0]].Handler;
                }
            }
            else if (header == (short)RecieveHeaders.SPECTATE_HIT)
            {
                foreach (KeyValuePair<string, ClientHandler> h in specs)
                {
                    PacketWriter.sendHit(h.Value, (int)packet.info[0], (int)packet.info[1], (int)packet.info[2], (int)packet.info[3]);
                }
            }
            else if (header == (short)RecieveHeaders.SPECTATE_RELEASE)
            {
                foreach (KeyValuePair<string, ClientHandler> h in specs)
                {
                    PacketWriter.sendSpectateRelease(h.Value, (int)packet.info[0], (int)packet.info[1]);
                }
            }
            else if (header == (short)RecieveHeaders.SPECTATE_CANCEL)
            {
                if (Server.userList.ContainsKey((string)packet.info[0]))
                {
                    Server.userList[(string)packet.info[0]].Handler.unspectate(this);
                    speccing = null;
                }
            }
            else if (header == (short)RecieveHeaders.SPECTATE_PRESS)
            {
                foreach (KeyValuePair<string, ClientHandler> h in specs)
                {
                    PacketWriter.sendSpectatePress(h.Value, (int)packet.info[0], (int)packet.info[1]);
                }
            }
            else if (header == (short)RecieveHeaders.SPECTATE_HEARTBEAT)
            {
                foreach (KeyValuePair<string, ClientHandler> h in specs)
                {
                    PacketWriter.sendSpectateHeartbeat(h.Value, (int)packet.info[0], (int)packet.info[1], (int)packet.info[2], (int)packet.info[3], (double)packet.info[4]);
                }
            }
            else if (header == (short)RecieveHeaders.SPECTATE_FINISH)
            {
                foreach (KeyValuePair<string, ClientHandler> h in specs)
                {
                    PacketWriter.sendSpectateFinish(h.Value, (int)packet.info[0], (int)packet.info[1], (int)packet.info[2], (int)packet.info[3],
                        (int)packet.info[4], (int)packet.info[5], (int)packet.info[6], (double)packet.info[7]);
                }
            }
            else if (header == (short)RecieveHeaders.SPECTATE_FAIL)
            {
                foreach (KeyValuePair<string, ClientHandler> h in specs)
                {
                    PacketWriter.sendSpectateFail(h.Value);
                }
            }
            else if (header == (short)RecieveHeaders.SPECTATE_GOT_CHART)
            {
                if (speccing != null && speccing.specs.Keys.Contains(userName))
                {
                    speccing.specChart[userName] = true;
                }
            }
        }
Example #7
0
        public RecievePacket GetAnswer(int SERVERPORT)
        {
            int[] recoffsets = { 6, 0, 0, 0 };
            byte[] TempArray = new byte[100];
            RecievePacket rcv = new RecievePacket();
            try{
                udpclient.Send(QueryData, QueryData.Length, "37.120.164.114", SERVERPORT);
            }
            catch (Exception X)
            {
                MessageBox.Show("could not connect to mini's inc servers. try again or check your connection"+X.Message);
            }

                Byte[] receiveBytes = udpclient.Receive(ref RemoteIpEndPoint);
                int offsetInt = GetEndOfStringinPacket(receiveBytes, recoffsets[0]);

                for (int i = recoffsets[0]; i < offsetInt; i++)
                {
                    TempArray[i - recoffsets[0]] = receiveBytes[i];
                }
                char[] cArray = System.Text.Encoding.ASCII.GetString(TempArray).ToCharArray();
                rcv.Name = new string(cArray);//WORKS
                recoffsets[1] = offsetInt + 1;
                offsetInt = GetEndOfStringinPacket(receiveBytes, recoffsets[1]);
                Array.Clear(TempArray, 0, TempArray.Length);
                for (int i = recoffsets[1]; i < offsetInt; i++)
                {
                    TempArray[i - recoffsets[1]] = receiveBytes[i];
                }
                cArray = System.Text.Encoding.ASCII.GetString(TempArray).ToCharArray();
                rcv.Map = new string(cArray);//WORKS
                recoffsets[2] = offsetInt + 1;
                offsetInt = GetEndOfStringinPacket(receiveBytes, recoffsets[2]);
                Array.Clear(TempArray, 0, TempArray.Length);
                for (int i = recoffsets[2]; i < offsetInt; i++)
                {
                    TempArray[i - recoffsets[2]] = receiveBytes[i];
                }
                cArray = System.Text.Encoding.ASCII.GetString(TempArray).ToCharArray();
                rcv.Folder = new string(cArray);//WORKS
                recoffsets[3] = offsetInt + 1;
                offsetInt = GetEndOfStringinPacket(receiveBytes, recoffsets[3]);
                Array.Clear(TempArray, 0, TempArray.Length);
                for (int i = recoffsets[3]; i < offsetInt; i++)
                {
                    TempArray[i - recoffsets[3]] = receiveBytes[i];
                }
                cArray = System.Text.Encoding.ASCII.GetString(TempArray).ToCharArray();
                rcv.Game = new string(cArray);//WORKS
                //No Strings anymore
                byte[] PrivArray = { receiveBytes[offsetInt + 1], receiveBytes[offsetInt + 2] };
                rcv.ID = ByteToShort(PrivArray);
                offsetInt += 3;
                rcv.Players = receiveBytes[offsetInt];
                offsetInt += 1;
                rcv.MaxPlayers = receiveBytes[offsetInt];
                offsetInt += 1;
                rcv.Bots = receiveBytes[offsetInt];
                offsetInt += 1;
                rcv.ServerType = receiveBytes[offsetInt];
                offsetInt += 1;
                rcv.Environment = receiveBytes[offsetInt];
                offsetInt += 1;
                rcv.PW = receiveBytes[offsetInt];
                offsetInt += 1;
                rcv.VAC = receiveBytes[offsetInt];
                rcv.Port = SERVERPORT;
                //MessageBox.Show(rcv.Name);
                return rcv;
        }
Example #8
0
 public void Debug(RecievePacket Param)
 {
     MessageBox.Show("J");
 }