protected override void OnInit(IServerConfig config) { Debug.Log("开始连接数据库~~~"); long time = ServiceTime.GetServiceTime(); DbConfig dConfig = new DbConfig(); dConfig.Server = config.Options.Get("DataBaseURL"); dConfig.User = config.Options.Get("DataBaseUser"); dConfig.Password = config.Options.Get("DataBasePassword"); dConfig.Database = config.Options.Get("DataBaseName"); database = DatabaseFactory.CreateDatabase(dConfig, DbConfig.DbType.MYSQL); try { database.Open(); time = ServiceTime.GetServiceTime() - time; Debug.Log("数据库连接成功 用时" + time + "ms"); } catch (DatabaseException e) { Debug.LogError("错误代码:" + e.GetErrorCode() + ",错误信息:" + e.GetErrorMsg()); } }
public static void Init() { Debug.Log("开始连接数据库~~~"); long time = ServiceTime.GetServiceTime(); DbConfig config = new DbConfig(); config.Server = "54.191.174.49"; config.User = "******"; config.Password = "******"; config.Database = "ElementCraft"; database = DatabaseFactory.CreateDatabase(config, DbConfig.DbType.MYSQL); try { database.Open(); time = ServiceTime.GetServiceTime() - time; Debug.Log("数据库连接成功 用时" + time + "ms"); } catch (DatabaseException e) { Debug.LogError("错误代码:" + e.GetErrorCode() + ",错误信息:" + e.GetErrorMsg()); } }
public override void BeforeFixedUpdate(int deltaTime) { List <EntityBase> list = GetEntityList(); for (int i = 0; i < list.Count; i++) { ConnectionComponent comp = list[i].GetComp <ConnectionComponent>(); comp.m_isInframe = true; T cmd = (T)comp.GetCommand(m_world.FrameCount); cmd.id = list[i].ID; cmd.frame = m_world.FrameCount; list[i].ChangeComp(cmd); //Debug.Log("USE cmd id "+ list[i].ID + " frame " + cmd.frame + " content " + Serializer.Serialize(cmd)); //到了这一帧还没有发送命令的,给预测一个并广播给所有前端 //if (comp.LastInputFrame < m_world.FrameCount) { for (int j = 0; j < list.Count; j++) { cmd.time = ServiceTime.GetServiceTime(); ConnectionComponent conn = list[j].GetComp <ConnectionComponent>(); ProtocolAnalysisService.SendMsg(conn.m_session, cmd); //Debug.Log("预测并广播 " + cmd.frame); } } } }
static void ReceviceAffirmMsg(SyncSession session, AffirmMsg msg) { ConnectionComponent commandComp = session.m_connect; int nowTime = ServiceTime.GetServiceTime(); commandComp.rtt = nowTime - msg.time; //Debug.Log(" 收到确认消息 frame: " + msg.index + " id: " + commandComp.Entity.ID + " rtt " + commandComp.rtt); }
static void BroadcastSameCommand(WorldBase world, ConnectionComponent connectComp, SameCommand cmd, bool includeSelf) { cmd.time = ServiceTime.GetServiceTime(); List <EntityBase> list = world.GetEntiyList(new string[] { "ConnectionComponent" }); for (int i = 0; i < list.Count; i++) { ConnectionComponent cp = list[i].GetComp <ConnectionComponent>(); ProtocolAnalysisService.SendMsg(cp.m_session, cmd); } }
static void ReceviceAffirmMsg(SyncSession session, AffirmMsg msg) { ConnectionComponent commandComp = session.m_connect; commandComp.ClearForecast(msg.frame); int nowTime = ServiceTime.GetServiceTime(); commandComp.rtt = nowTime - msg.time; //Debug.Log("rtt " + commandComp.rtt); }
public static void SendMsg(this SyncSession session, string key, Dictionary <string, object> data) { if (session == null) { //Debug.LogError("Session 已经断开连接!"); return; } ByteArray ba = new ByteArray(); List <byte> message = GetSendByte(key, data); int len = 3 + message.Count; int method = GetMethodIndex(key); ba.WriteShort(len); ba.WriteByte((byte)(method / 100)); ba.WriteShort(method); if (message != null) { ba.bytes.AddRange(message); } else { ba.WriteInt(0); } byte[] buffer = ba.Buffer; //Debug.Log("消息头 " + method + " 消息名 " + key + " --> " + BitConverter.ToString(buffer)); try { int time = ServiceTime.GetServiceTime(); bool result = session.TrySend(buffer, 0, buffer.Length); if (!result) { session.Close(); } if (ServiceTime.GetServiceTime() - time > 10) { Debug.Log("发送时间 " + (ServiceTime.GetServiceTime() - time)); } } catch (Exception e) { Debug.LogError("Send Messge Exception " + e.ToString()); } }
static void BroadcastCommand(WorldBase world, ConnectionComponent connectComp, T cmd, bool includeSelf) { cmd.time = ServiceTime.GetServiceTime(); //TODO 与预测一致不广播节约带宽; List <EntityBase> list = world.GetEntityList(new string[] { "ConnectionComponent" }); for (int i = 0; i < list.Count; i++) { ConnectionComponent cp = list[i].GetComp <ConnectionComponent>(); if (cp.m_session != null) { ProtocolAnalysisService.SendMsg(cp.m_session, cmd); //Debug.Log("BroadcastCommand " + cmd.frame); } } }
void SendReconnectMsg(ConnectionComponent comp) { float time = ServiceTime.GetServiceTime(); //发送游戏全部数据 PushAllEnityData(comp); //发送单例数据 PushSingleComponentData(comp); //发送游戏开始消息 StartSyncMsg startMsg = CreateStartMsg(m_world); ProtocolAnalysisService.SendMsg(comp.m_session, startMsg); Debug.Log("重连时间 " + (ServiceTime.GetServiceTime() - time)); }
static void ReceviceSyncMsg(SyncSession session, T msg) { //Debug.Log("ReceviceSyncMsg " + msg.id); ConnectionComponent connectComp = session.m_connect; WorldBase world = session.m_connect.Entity.World; if (connectComp != null) { if (msg.frame > world.FrameCount) { //消息确认 AffirmMsg amsg = new AffirmMsg(); amsg.frame = msg.frame; amsg.time = msg.time; BroadcastCommand(world, connectComp, msg, false); ProtocolAnalysisService.SendMsg(session, amsg); connectComp.m_commandList.Add(msg); connectComp.lastInputFrame = msg.frame; } else { //把玩家的这次上报当做最新的操作并转发 Debug.Log("帧数落后 world.FrameCount: " + world.FrameCount + " msg frame:" + msg.frame + " 预测列表计数 " + connectComp.m_forecastList.Count); //Debug.Log("接收玩家数据 " + Serializer.Serialize(msg)); connectComp.m_lastInputCache = msg; connectComp.lastInputFrame = world.FrameCount; //并且让这个玩家提前 PursueMsg pmsg = new PursueMsg(); pmsg.id = msg.id; pmsg.recalcFrame = msg.frame; pmsg.frame = world.FrameCount; pmsg.advanceCount = CalcAdvanceFrame(connectComp); pmsg.serverTime = ServiceTime.GetServiceTime(); ProtocolAnalysisService.SendMsg(session, pmsg); } } }
static void UpdateLogic() { int time = ServiceTime.GetServiceTime(); int lastTime = ServiceTime.GetServiceTime(); while (true) { lastTime = ServiceTime.GetServiceTime(); UpdateWorld(s_intervalTime); time = ServiceTime.GetServiceTime(); int sleepTime = s_intervalTime - (time - lastTime); if (sleepTime > 0) { Thread.Sleep(sleepTime); } } }