void OnMsgActiveObject(TcpHost.SocketAccept socket, byte[] protoData) { ProtoIntArray p = new ProtoIntArray(); if (p.Parse(protoData)) { ActiveSceneObject(p.context); } }
void OnMsgEnterScene(TcpHost.SocketAccept socket, byte[] protoData) { ProtoPlayerInfo p; ProtoInt intP = new ProtoInt(); intP.Parse(protoData); int playerMapID = intP.context; intP.Recycle(); if (playerInfos.TryGetValue((byte)socket.id, out p)) { p.mapID = playerMapID; Debug.LogWarning(socket.id + "进入场景完成"); } }
void OnMsgLogin(TcpHost.SocketAccept socket, byte[] protoData) { ProtoPlayerInfo p; byte id = (byte)socket.id; if (playerInfos.TryGetValue((byte)id, out p)) { p.connectStatus = 1; host.RemoveClient(id); } else { byte loginPos = protoData[0]; bool[] inPosPlayers = new bool[10]; bool canUsePos = true; foreach (var player in playerInfos) { if (player.Value.pos == loginPos) { canUsePos = false; } inPosPlayers[player.Value.pos] = true; } if (!canUsePos) { for (int i = 1; i < inPosPlayers.Length; i++) { if (inPosPlayers[i] == false) { loginPos = (byte)i; break; } } } p = new ProtoPlayerInfo(); p.hp = PlayerCfg.HP; p.score = PlayerCfg.score; p.connectStatus = 1; p.id = id; p.pos = loginPos; playerInfos.Add(id, p); Debug.LogWarning("玩家加入" + id); SyncPlayerList(); } //返回角色数据 SendMsg(socket.id, ProtoIDCfg.LOGIN, p); }
/// <summary> /// 处理Proto /// </summary> void ParseMsg(TcpHost.SocketAccept socket, byte protoID, byte[] protoData, byte[] srcData) { switch (protoID) { //心跳 case ProtoIDCfg.HEARTBEAT: socket.heartbeatTime = Time.time; socket.heartbeatStatus = 0; break; //登入 case ProtoIDCfg.LOGIN: OnMsgLogin(socket, protoData); break; //进入场景 case ProtoIDCfg.ENTER_SCENE: OnMsgEnterScene(socket, protoData); break; //激活场景单位 case ProtoIDCfg.ACTIVE_OBJECTS: OnMsgActiveObject(socket, protoData); break; //创建场景单位 case ProtoIDCfg.CREATE_OBJECTS: OnMsgCreateObject(socket, protoData); break; //销毁对象 case ProtoIDCfg.REMOVE_OBJECTS: RemoveSceneObject(socket, protoData); Broadcast(srcData); break; //同步输入 case ProtoIDCfg.SYNC_INPUT: Broadcast(srcData); break; //对象行为 case ProtoIDCfg.OBJECT_ACTION: Broadcast(srcData); break; } }
/// <summary> /// 销毁场景单位 /// </summary> /// <param name="indexs"></param> void RemoveSceneObject(TcpHost.SocketAccept socket, byte[] protoData) { int[] indexs = null; ProtoIntArray proto = new ProtoIntArray(); proto.Parse(protoData); indexs = proto.context; if (indexs == null || indexs.Length < 1) { return; } for (int i = 0; i < indexs.Length; i++) { if (sceneObjes.ContainsKey(indexs[i])) { sceneObjes.Remove(indexs[i]); Debug.LogWarning("销毁:" + indexs[i]); } } }
void OnMsgCreateObject(TcpHost.SocketAccept socket, byte[] protoData) { ProtoSyncObjectList list = new ProtoSyncObjectList(); if (list.Parse(protoData)) { for (int i = 0; i < list.objList.Length; i++) { SyncObject obj = list.objList[i]; obj.serverID = objectIDCounter; objectIDCounter++; //obj.objectIndex = indexs[i]; //proto.serverIDs[i] = obj.serverID; //proto.objectIndexs[i] = obj.objectIndex; if (!sceneObjes.ContainsKey(obj.serverID)) { sceneObjes.Add(obj.serverID, obj); Debug.LogWarning("创建对象:" + obj.serverID); } } } Broadcast(ProtoIDCfg.CREATE_OBJECTS, list); }
/// <summary> /// 主机处理消息 /// </summary> void HandleHostDataMsg() { TcpHost.SocketAccept[] clients = host.GetClientList(); for (int i = 0; i < clients.Length; i++) { TcpHost.SocketAccept client = clients[i]; if (client != null) { while (client.recvQueue.Count > 0) { byte[] data = client.recvQueue.Dequeue(); if (data != null && data.Length > 0 && data[0] == Connection.PACKER_HEAD) { int offset = Connection.PACKER_OFFSET; byte[] proto = data.Length == offset?null:SerializeUtil.GetContextData(data, offset, data.Length - offset); ParseMsg(client, data[1], proto, data); } } //心跳 float heartbeatInterval = Time.time - client.heartbeatTime; if (heartbeatInterval > Connection.HEART_BEAT_TIME) { if (client.heartbeatStatus > 0) { Debug.LogWarning(client.id + "心跳超时"); host.RemoveClient(client.id); } else { SendMsg(client.id, ProtoIDCfg.HEARTBEAT, null); client.heartbeatStatus++; } client.heartbeatTime = Time.time; } } } }