Пример #1
0
        /// <summary>
        /// 发送数据通知函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SocketSended(object sender, SocketAsyncEventArgs e)
        {
            //SocketListener sl = sender as SocketListener;
            //Socket s = (e.UserToken as AsyncUserToken).CurrentSocket;
            TCPOutPacket tcpOutPacket = (e.UserToken as AsyncUserToken).Tag as TCPOutPacket;

            tcpOutPacketPool.Push(tcpOutPacket);
        }
Пример #2
0
        public void ThreadFuncation2()
        {
            int szOfest = 11;

            //autoEvent.WaitOne();
            while (true)
            {
                if (m_MoveDataList.Count > 0)
                {
                    mutex.WaitOne();
                    try
                    {
                        TCPOutPacket szTCPOutPacket = null;

                        szTCPOutPacket = m_MoveDataList.Dequeue();

                        string[] fields = null;
                        if (szTCPOutPacket.PacketCmdID < 30000)
                        {
                            fields = Encoding.Default.GetString(szTCPOutPacket.GetPacketBytes(), szOfest, szTCPOutPacket.PacketDataSize - szOfest).Trim().Split(':');
                        }
                        switch (szTCPOutPacket.PacketCmdID)
                        {
                        case (int)TCPServerCmds.CMD_LOGIN_ON1:
                        {
                            m_LNetClient.Disconnect();
                            m_LNetClient.Destroy();

                            if (!fields[0].Equals("0"))
                            {
                                g_Thread1.Start();
                                g_Acc = fields[0];
                                SendRoleData(fields[0]);
                            }
                            else
                            {
                                SysConOut.WriteLine("登陆失败");
                            }
                            break;
                        }

                        case (int)TCPServerCmds.CMD_NTF_EACH_ROLE_ALLOW_CHANGE_NAME:
                        {
                            SysConOut.WriteLine("==============CHANGE_NAME===============");
                            foreach (var s in fields)
                            {
                                SysConOut.WriteLine(s);
                            }
                            break;
                        }

                        case (int)CommandID.CMD_GET_ROLE_LIST:
                        {
                            SysConOut.WriteLine("==============CMD_ROLE_LIST 30003===============");
                            //反序列化
                            MemoryStream        msRecvData            = new MemoryStream(szTCPOutPacket.GetPacketBytes(), szOfest, szTCPOutPacket.PacketDataSize - szOfest);
                            GetRolesListReponse szGetRolesListReponse = Serializer.Deserialize <GetRolesListReponse>(msRecvData);
                            SysConOut.WriteLine("get Role state = " + szGetRolesListReponse.State.ToString());
                            foreach (var i in szGetRolesListReponse.SubRoleinfo)
                            {
                                SysConOut.WriteLine("get Role Role = " + i.RoleID.ToString());
                            }
                            if (szGetRolesListReponse.State == (int)ErrorCode.ERROR_INVALID_ROLE)
                            {
                                var         szRandom      = new Random().Next(100);
                                CreateRoles szCreateRoles = new CreateRoles();
                                szCreateRoles.Acc      = g_Acc;
                                szCreateRoles.UserName = g_Acc + szRandom.ToString();
                                szCreateRoles.Sex      = 0;
                                szCreateRoles.occup    = 0;
                                szCreateRoles.ZoneID   = 0;
                                m_tcpOutPacketPool.Push(szTCPOutPacket);

                                MemoryStream msResult = new MemoryStream();
                                Serializer.Serialize <CreateRoles>(msResult, szCreateRoles);
                                byte[] msSendData = msResult.ToArray();
                                szTCPOutPacket = TCPOutPacket.MakeTCPOutPacket(m_GSNetClient.OutPacketPool, msSendData, 0, msSendData.Length, 30002);
                                if (m_GSNetClient.SendData(szTCPOutPacket))
                                {
                                    SysConOut.WriteLine("发送创建角色列表数据成功");
                                }
                                else
                                {
                                    SysConOut.WriteLine("发送创建角色列表数据失败");
                                }

                                m_GSNetClient.OutPacketPool.Push(szTCPOutPacket);
                            }
                            else
                            {
                                foreach (var Roleinfo in szGetRolesListReponse.SubRoleinfo)
                                {
                                    g_RoleID = Roleinfo.RoleID.ToString();
                                    break;
                                }
                                SendInitGame(g_Acc, g_RoleID);
                                Console.Title = "机器人---" + g_RoleID;
                            }
                            break;
                        }

                        case (int)TCPServerCmds.CMD_ROLE_LIST:
                        {
                            SysConOut.WriteLine("==============CMD_ROLE_LIST===============");
                            if (fields[0].Equals("0"))
                            {
                                var szRandom = new Random().Next(100);
                                var result   = StringUtil.substitute("{0}:{1}:{2}:{3}:{4}:{5}:{6}", g_Acc, g_Acc + szRandom.ToString(), 0, 0,
                                                                     String.Format("{0}${1}", g_Acc + szRandom.ToString(), 0), 0, 1);

                                TCPOutPacket _TCPOutPacket = TCPOutPacket.MakeTCPOutPacket(m_GSNetClient.OutPacketPool, result,
                                                                                           (int)TCPServerCmds.CMD_CREATE_ROLE);
                                //创建角色
                                if (m_GSNetClient.SendData(_TCPOutPacket))
                                {
                                    SysConOut.WriteLine("发送创建角色数据成功");
                                }
                                else
                                {
                                    SysConOut.WriteLine("发送创建角色数据失败");
                                }
                                m_GSNetClient.OutPacketPool.Push(_TCPOutPacket);
                            }
                            else
                            {
                                string[] fieldsData = fields[1].Split('$');
                                g_RoleID = fieldsData[0];
                                SendInitGame(g_Acc, g_RoleID);
                                Console.Title = "机器人---" + g_RoleID;
                            }
                            break;
                        }

                        case (int)CommandID.CMD_CREATE_ROLE:
                        {
                            SysConOut.WriteLine("====CMD_CREATE_ROLE  30002=====");
                            MemoryStream       msRecvData           = new MemoryStream(szTCPOutPacket.GetPacketBytes(), szOfest, szTCPOutPacket.PacketDataSize - szOfest);
                            CreateRolesReponse szCreateRolesReponse = Serializer.Deserialize <CreateRolesReponse>(msRecvData);
                            if (szCreateRolesReponse.State == (int)ErrorCode.ERROR_OK)
                            {
                                SendRoleData(g_Acc);
                            }
                            m_tcpOutPacketPool.Push(szTCPOutPacket);
                            break;
                        }

                        case (int)TCPServerCmds.CMD_CREATE_ROLE:
                        {
                            SysConOut.WriteLine("====CMD_CREATE_ROLE=====");

                            if (!fields[0].Equals("0"))
                            {
                                SendRoleData(g_Acc);
                            }
                            break;
                        }

                        case (int)CommandID.CMD_INIT_GAME:
                        {
                            SysConOut.WriteLine("==============CMD_INIT_GAME 30004===============");

                            MemoryStream       msRecvData           = new MemoryStream(szTCPOutPacket.GetPacketBytes(), szOfest, szTCPOutPacket.PacketDataSize - szOfest);
                            InitialGameReponse szInitialGameReponse = Serializer.Deserialize <InitialGameReponse>(msRecvData);

                            SysConOut.WriteLine("CMD_INIT_GAME RoleID = " + szInitialGameReponse.SubRoleinfo.RoleID.ToString() + "  Role = " + g_RoleID.ToString());
                            SendPlayGame(g_RoleID);
                            m_tcpOutPacketPool.Push(szTCPOutPacket);
                            break;
                        }

                        case (int)TCPServerCmds.CMD_INIT_GAME:
                        {
                            SysConOut.WriteLine("==============CMD_INIT_GAME===============");

                            RoleData szRoleData = DataHelper.BytesToObject <RoleData>(szTCPOutPacket.GetPacketBytes(), szOfest, szTCPOutPacket.PacketDataSize - szOfest);
                            SysConOut.WriteLine("CMD_INIT_GAME RoleID = " + szRoleData.RoleID.ToString());
                            SendPlayGame(g_RoleID);
                            break;
                        }

                        case (int)CommandID.CMD_PLAY_GAME:
                        {
                            SysConOut.WriteLine("==============CMD_PLAY_GAME 30005===============");
                            MemoryStream    msRecvData        = new MemoryStream(szTCPOutPacket.GetPacketBytes(), szOfest, szTCPOutPacket.PacketDataSize - szOfest);
                            PlayGameReponse szPlayGameReponse = Serializer.Deserialize <PlayGameReponse>(msRecvData);

                            SysConOut.WriteLine("CMD_PLAY_GAME RoleID = " + szPlayGameReponse.RoleID.ToString());

                            if (g_Thread == null)
                            {
                                g_Thread = new Thread(new ThreadStart(ThreadFuncation));
                            }
                            g_Thread.Start();
                            m_tcpOutPacketPool.Push(szTCPOutPacket);
                            break;
                        }

                        case (int)TCPServerCmds.CMD_PLAY_GAME:
                        {
                            SysConOut.WriteLine("==============CMD_PLAY_GAME===============");

                            if (g_Thread == null)
                            {
                                g_Thread = new Thread(new ThreadStart(ThreadFuncation));
                            }
                            g_Thread.Start();
                            break;
                        }

                        case (int)CommandID.CMD_GAME_MOVE:
                        {
                            SysConOut.WriteLine("==============CMD_GAME_MOVE 30006===============");
                            MemoryStream msRecvData   = new MemoryStream(szTCPOutPacket.GetPacketBytes(), szOfest, szTCPOutPacket.PacketDataSize - szOfest);
                            SpriteMove   szSpriteMove = Serializer.Deserialize <SpriteMove>(msRecvData);


                            SysConOut.WriteLine("CMD_SPR_MOVE Role = " + szSpriteMove.RoleID.ToString()
                                                + " Mapcode = " + szSpriteMove.MapCode.ToString()
                                                + " action = " + szSpriteMove.action.ToString()
                                                + " pathString = " + szSpriteMove.pathString);
                            m_tcpOutPacketPool.Push(szTCPOutPacket);
                            break;
                        }

                        case (int)TCPServerCmds.CMD_SPR_MOVE:
                        {
                            SpriteNotifyOtherMoveData moveData = DataHelper.BytesToObject <SpriteNotifyOtherMoveData>(szTCPOutPacket.GetPacketBytes(), 11, szTCPOutPacket.PacketDataSize - 11);

                            SysConOut.WriteLine("CMD_SPR_MOVE Role = " + moveData.roleID.ToString()
                                                + " Mapcode = " + moveData.mapCode.ToString()
                                                + " action = " + moveData.action.ToString()
                                                + " pathString = " + moveData.pathString);
                            m_tcpOutPacketPool.Push(szTCPOutPacket);
                            break;
                        }
                        }
                    }
                    catch (Exception ex)
                    {
                        SysConOut.WriteLine(ex.Message);
                    }


                    mutex.Release();
                }
                Thread.Sleep(100);
            }
        }