public static void OnRecv(Socket s, byte[] data, int nSize) { SocketInfo info = new SocketInfo(); info.type = SocketCallBack.TYPE_RECEIVE; info.s = s; info.data = new byte[nSize]; Buffer.BlockCopy(data, 0, info.data, 0, nSize); SocketCallBack.Instance().AddData(info); //try //{ // lock (m_DicSession) // { // if (m_DicSession.ContainsKey(s)) // { // GameBase.Network.GameSession session = m_DicSession[s]; // byte[] bydata = new byte[nSize]; // Buffer.BlockCopy(data, 0, bydata, 0, nSize); // session.m_GamePack.ProcessNetData(bydata); // session.m_nLastTime = System.Environment.TickCount; // } // } //} //catch (System.Exception ex) //{ // Log.Instance().WriteLog(ex.Message); // Log.Instance().WriteLog(ex.StackTrace); //} }
public static void OnClose(Socket s) { SocketInfo info = new SocketInfo(); info.type = SocketCallBack.TYPE_CLOSE; info.s = s; SocketCallBack.Instance().AddData(info); //try //{ // lock (m_DicSession) // { // if (m_DicSession.ContainsKey(s)) // { // GameBase.Network.GameSession session = m_DicSession[s]; // m_DicSession.Remove(s); // session = null; // } // } //} //catch (System.Exception ex) //{ // Log.Instance().WriteLog(ex.Message); // Log.Instance().WriteLog(ex.StackTrace); //} }
public void AddData(SocketInfo info) { //有些时候调试的时候会发生死锁现象- 待release版本测试- 2015.9.18- //已解决- 不是这个原因...是其他bug。。。2015.10.10 lock (mList) { mList.Add(info); } }
public SocketInfo GetInfo() { SocketInfo info = null; lock (mList) { if (mList.Count > 0) { info = mList[0]; mList.RemoveAt(0); } } return(info); }
public static void OnConnect(Socket s) { SocketInfo info = new SocketInfo(); info.type = SocketCallBack.TYPE_ONCONNECT; info.s = s; SocketCallBack.Instance().AddData(info); //try //{ // lock (m_DicSession) // { // GameBase.Network.GameSession session = new GameBase.Network.GameSession(s); // m_DicSession[s] = session; // } //} //catch (System.Exception ex) //{ // Log.Instance().WriteLog(ex.Message); // Log.Instance().WriteLog(ex.StackTrace); //} }
public void Run() { int runtime = System.Environment.TickCount; while (true) { if (System.Environment.TickCount - runtime > 300) { break; //超出三百毫秒下次再处理 } SocketInfo info = GetInfo(); if (info == null) { break; } if (info.s == null) { break; } Socket s = info.s; switch (info.type) { case TYPE_ONCONNECT: { // SessionManager.Instance().AddSession(s, GameServer.GetTcpServer()); break; } case TYPE_CLOSE: { // PlayerObject play = UserEngine.Instance().FindPlayerObjectToSocket(s); // SessionManager.Instance().RemoveSession(s); // UserEngine.Instance().RemovePlayObject(play); //if (play != null) // { // play.GetGameSession().Dispose(); // play.SetGameSession(null); //} break; } case TYPE_RECEIVE: { GameBase.Network.GameSession session = info.session; byte[] bydata = new byte[info.data.Length]; Buffer.BlockCopy(info.data, 0, bydata, 0, info.data.Length); session.m_GamePack.ProcessNetData(bydata); // session.m_nLastTime = System.Environment.TickCount; byte[] retdata = session.m_GamePack.GetData(); if (retdata != null) { GameBase.Network.PackIn packin = new GameBase.Network.PackIn(retdata); ushort tag = packin.ReadUInt16(); switch (tag) { case PacketProtoco.C_LOGINUSER: { GameBase.Network.PacketOut packout = new GameBase.Network.PacketOut(session.GetGamePackKeyEx()); byte[] sendbyte = { 0, 1, 59, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0,255, 255, 255, 255 }; packout.WriteBuff(sendbyte); sendbyte = packout.Flush(); Program.server.SendData(info.s, sendbyte); break; } } } // SessionManager.Instance().AddNetData(s, info.data, info.data.Length); break; } } } }