using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using System.Security.Cryptography; namespace SogClient { public class TestWorkThread { private Player[] m_players; private long m_lastStartTime; private long m_lastStatTime; // 初始化后的 private int m_playerNumAlloc = 0; private int m_threadSerial; private int m_playerCount; private bool m_loginOver = false; private Thread m_thread; private bool m_Over = false; private ConnectedClientSessionUpdate m_sessionUpdate = new ConnectedClientSessionUpdate(); public void Init(int threadSerial, int count) { m_threadSerial = threadSerial + 1; m_playerCount = count; m_players = new Player[m_playerCount]; } public void StartThread(bool useThead) { if (useThead) { m_thread = new Thread(this.TheadRunFun); m_thread.Start(); } else { TheadRunFun(); } } private void TheadRunFun() { while (true) { Update(); Thread.Sleep(1); } } public static string GetMd5(string text) { StringBuilder sb = new StringBuilder(); using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider()) { byte[] data = md5.ComputeHash(Encoding.UTF8.GetBytes(text)); int length = data.Length; for (int i = 0; i < length; i++) { sb.Append(data[i].ToString("x2")); } return (sb.ToString()); } } private void StartSomePlayer() { if(!m_loginOver && m_playerNumAlloc >= m_playerCount) { m_loginOver = true; } if (m_playerNumAlloc >= m_playerCount) { return; } long nowSecond = TimeUtils.GetTimeSecond(); //if (nowSecond - m_lastStartTime < 1) //{ // return; //} //TraceLog.Debug("{0}号线程 -----------------m_playerNumAlloc={1}-----------------", m_threadSerial, m_playerNumAlloc); m_lastStartTime = nowSecond; int accountType = Config.AccountType; string accountIDPre = Config.ClientTestConfig.playerAccountIDPre; int count = 0; for (int i = m_playerNumAlloc; i < m_playerCount; i++) { string accountID = accountIDPre + "_" + m_threadSerial.ToString() + "_" + i.ToString() + "_" + "@Guest"; //string accountID = GetMd5(accountIDStr) + "@Guest"; string token = GuestCrypto.GetAccountToken(accountID); m_players[i] = new Player(accountType, accountID, token, m_sessionUpdate, i, m_threadSerial); m_players[i].Start(); count++; m_playerNumAlloc++; if (count >= 5) { break; } } } private void Update() { StartSomePlayer(); long nowMs = TimeUtils.GetTime(); int inGameNum = 0; for(int i = 0; i < m_playerNumAlloc; i++) { Player player = m_players[i]; if (player != null) { player.Update(nowMs); if(player.State == PlayerState.PlayerState_InGame ) { inGameNum++; } } } if(nowMs - m_lastStatTime > 5000) { m_lastStatTime = nowMs; string msg = string.Format("{0} thread,player num {1}, in game {2}", m_threadSerial, m_playerCount, inGameNum); Console.WriteLine(msg); TraceLog.Debug(msg); } //统一select m_sessionUpdate.Update(); } } }