internal void onCreated() { try { this.OnCreated(); } catch (Exception e) { GoWorldLogger.Error(this.ToString(), e.ToString()); } }
private void disconnectTCPClient() { if (this.tcpClient != null) { this.tcpClient.Close(); this.tcpClient = null; this.packetReceiver = null; this.startConnectTime = DateTime.MinValue; GoWorldLogger.Warn(this.ToString(), "Disconnected"); } }
private void sendPacket(Packet pkt) { if (this.tcpClient == null) { GoWorldLogger.Warn("GameClient", "Game Client Is Not Connected, Send Packet Failed: " + pkt); } Debug.Assert(pkt.writePos >= sizeof(UInt16)); this.sendAll(BitConverter.GetBytes((UInt32)pkt.writePos), sizeof(UInt32)); this.sendAll(pkt.payload, pkt.writePos); }
internal void becomeClientOwner() { try { this.OnBecomeClientOwner(); } catch (Exception e) { GoWorldLogger.Error(this.ToString(), e.ToString()); } }
internal void tick() { try { this.Tick(); } catch (Exception e) { GoWorldLogger.Error(this.ToString(), e.ToString()); } }
internal void enterSpace() { try { this.OnEnterSpace(); } catch (Exception e) { GoWorldLogger.Error(this.ToString(), e.ToString()); } }
private void onConnected(IAsyncResult ar) { if (this.tcpClient.Connected) { GoWorldLogger.Info(this.ToString(), "Connected " + this.tcpClient.Connected); } else { GoWorldLogger.Warn(this.ToString(), "Connect Failed!"); this.disconnectTCPClient(); } }
internal void RegisterEntity(Type entityType) { //ClientEntity entity = gameObject.GetComponent<ClientEntity>(); //UnityEngine.Debug.Assert(entity != null); GoWorldLogger.Assert(entityType.IsSubclassOf(typeof(ClientEntity))); //Type entityType = entity.GetType(); string entityTypeName = entityType.Name; GoWorldLogger.Assert(!this.entityTypes.ContainsKey(entityTypeName)); this.entityTypes[entityTypeName] = entityType; }
private void handleCallEntityMethodOnClient(Packet pkt) { string entityID = pkt.ReadEntityID(); string method = pkt.ReadVarStr(); object[] args = pkt.ReadArgs(); GoWorldLogger.Debug("GameClient", "Handle Call Entity Method On Client: {0}.{1}({2})", entityID, method, args); if (OnCallEntityMethodOnClient != null) { this.OnCallEntityMethodOnClient(entityID, method, args); } //manager.OnCallEntityMethod(entityID, method, args); }
public Int64 GetInt(string key) { object val = this.get(key); try { return((Int64)val); } catch (InvalidCastException) { GoWorldLogger.Error("MapAttr", "Can Not Convert From Type {0} To Int64", val.GetType()); return(0); } }
internal void DestroyEntity(string entityID) { GoWorldLogger.Debug("EntityManager", "Destroy Entity {0}", entityID); ClientEntity entity; if (this.entities.TryGetValue(entityID, out entity)) { entity.Destroy(); } else { GoWorldLogger.Error("EntityManager", "Destroy Entity {0} Failed: Entity Not Found", entityID); } }
internal void OnSyncEntityInfo(string entityID, float x, float y, float z, float yaw) { ClientEntity entity; try { entity = this.entities[entityID]; } catch (KeyNotFoundException) { GoWorldLogger.Warn("EntityManager", "Entity {0} Sync Entity Info Failed: Entity Not Found", entityID); return; } entity.OnSyncEntityInfo(x, y, z, yaw); }
internal void OnMapAttrClear(string entityID, ListAttr path) { ClientEntity entity; try { entity = this.entities[entityID]; } catch (KeyNotFoundException) { GoWorldLogger.Warn("EntityManager", "Entity {0} Sync Entity Info Failed: Entity Not Found", entityID); return; } entity.OnMapAttrClear(path); }
internal Packet RecvPacket() { if (this.tcpClient.Available == 0) { return(null); } int nr; if (this.recvState == RecvState.receivingPayloadLen) { if (tcpClient.Available < Proto.SIZE_FIELD_SIZE) { return(null); } nr = tcpClient.Client.Receive(this.recvPayloadLenBuff); Debug.Assert(nr == Proto.SIZE_FIELD_SIZE); this.recvPayloadLen = BitConverter.ToUInt32(recvPayloadLenBuff, 0); if (this.recvPayloadLen < 2 || this.recvPayloadLen > Proto.MAX_PAYLOAD_LEN) { GoWorldLogger.Error("PacketReceiver", "Invalid Packet Payload Length: " + this.recvPayloadLen); this.tcpClient.Close(); return(null); } this.recvState = RecvState.receivingPayload; this.recvPayloadBuff = new byte[this.recvPayloadLen]; } if (tcpClient.Available < this.recvPayloadLen) { return(null); } nr = tcpClient.Client.Receive(this.recvPayloadBuff); Debug.Assert(nr == this.recvPayloadLen); this.recvState = RecvState.receivingPayloadLen; byte[] payload = this.recvPayloadBuff; this.recvPayloadBuff = null; return(new Packet(payload)); }
internal void CallEntity(string entityID, string method, object[] args) { ClientEntity entity; if (this.entities.TryGetValue(entityID, out entity)) { System.Reflection.MethodInfo methodInfo = entity.GetType().GetMethod(method); if (methodInfo == null) { GoWorldLogger.Error("EntityManager", "Call Entity {0}.{1}({2} Args) Failed: Public Method Not Found", entity, method, args.Length); return; } GoWorldLogger.Debug("EntityManager", "Call Entity {0}: {1}({2} Args)", entity, method, args.Length); methodInfo.Invoke(entity, args); } else { // entity not found GoWorldLogger.Error("EntityManager", "Call Entity {0}.{1}({2} Args) Failed: Entity Not Found", entityID, method, args.Length); } }
public void Destroy() { if (this.IsDestroyed) { return; } EntityManager.Instance.delEntity(this); try { this.OnDestroy(); } catch (Exception e) { GoWorldLogger.Error(this.ToString(), e.ToString()); } this.IsDestroyed = true; this.Attrs = null; GameObject.Destroy(gameObject); }
internal object getAttrByPath(ListAttr path) { object attr = this.Attrs; if (path == null) { return(attr); } foreach (object key in path) { if (key.GetType() == typeof(string)) { attr = (attr as MapAttr).get((string)key); } else { attr = (attr as ListAttr).get((int)key); } } GoWorldLogger.Debug(this.ToString(), "Get Attr By Path: {0} = {1}", path.ToString(), attr); return(attr); }
private void assureTCPClientConnected() { if (this.tcpClient != null) { if (!this.tcpClient.Connected) { this.checkConnectTimeout(); } return; } // no tcpClient == not connecting, start new connection ... GoWorldLogger.Info(this.ToString(), "Connecting ..."); this.tcpClient = new TcpClient(AddressFamily.InterNetwork); this.tcpClient.NoDelay = true; this.tcpClient.SendTimeout = 5000; this.tcpClient.ReceiveBufferSize = Proto.MAX_PAYLOAD_LEN + Proto.SIZE_FIELD_SIZE; this.startConnectTime = DateTime.Now; this.packetReceiver = new PacketReceiver(this.tcpClient); this.tcpClient.Connect(this.Host, this.Port); this.tcpClient.Client.Blocking = false; //this.tcpClient.BeginConnect(this.Host, this.Port, this.onConnected, null); // TODO: BeginConnect fail in Unity3D this.onConnected(null); }
private static void OnCreateEntityOnClient(string typeName, string entityID, bool isClientOwner, float x, float y, float z, float yaw, MapAttr attrs) { GoWorldLogger.Debug("GoWorld", "OnCreateEntityOnClient {0}<{1}>, IsClientOwner={2}, Attrs={3}, Position={4},{5},{6} ...", typeName, entityID, isClientOwner, attrs, x, y, z); ClientEntity e = EntityManager.CreateEntity(typeName, entityID, isClientOwner, x, y, z, yaw, attrs); }
internal ClientEntity CreateEntity(string typeName, string entityID, bool isClientOwner, float x, float y, float z, float yaw, MapAttr attrs) { if (typeName == SPACE_ENTITY_NAME) { typeName = "ClientSpace"; } GoWorldLogger.Assert(this.entityTypes.ContainsKey(typeName), "Entity Type {0} Not Found", typeName); if (this.entities.ContainsKey(entityID)) { ClientEntity old = this.entities[entityID]; GoWorldLogger.Warn("EntityManager", "Entity {0} Already Exists, Destroying Old One: {1}", entityID, old); old.Destroy(); } // create new Game Object of specified type Type entityType = this.entityTypes[typeName]; System.Reflection.MethodInfo createGameObjectMethod = entityType.GetMethod("CreateGameObject"); GoWorldLogger.Assert(createGameObjectMethod != null, "CreateGameObject Method Not Found For Entity Type {0}", typeName); GameObject gameObject = createGameObjectMethod.Invoke(null, new object[1] { attrs }) as GameObject; if (gameObject == null) { GoWorldLogger.Error("EntityManager", "Fail To Create GameObject For Entity Type {0}, Please Define New CreateGameObject Method Like: public static new GameObject CreateGameObject(MapAttr attrs) { ... }", typeName); return(null); } ClientEntity e = gameObject.GetComponent <ClientEntity>(); if (e == null || e.GetType().Name != typeName) { // GameObject created, but wrong entity type GameObject.Destroy(gameObject); GoWorldLogger.Error("EntityManager", "Fail To Create GameObject For Entity Type {0}: wrong entity type {1}", typeName, e.GetType().Name); return(null); } GameObject.DontDestroyOnLoad(gameObject); e.init(entityID, isClientOwner, x, y, z, yaw, attrs); this.entities[entityID] = e; gameObject.transform.position = e.Position; gameObject.transform.rotation = Quaternion.Euler(new Vector3(0f, e.Yaw, 0f)); e.onCreated(); // new entity created if (e.IsSpace) { // enter new space if (this.Space != null) { this.Space.Destroy(); } this.Space = e as ClientSpace; this.onEnterSpace(); } else { if (e.IsClientOwner) { if (this.ClientOwner != null) { GoWorldLogger.Warn("EntityManager", "Replacing Existing Player: " + this.ClientOwner); this.ClientOwner.Destroy(); } this.ClientOwner = e; e.becomeClientOwner(); } if (this.Space != null) { e.enterSpace(); } } return(e); }