public bool send(MemoryStream stream) { int dataLength = (int)stream.length(); if (dataLength <= 0) return true; if (0 == Interlocked.Add(ref _sending, 0)) { if (_wpos == _spos) { _wpos = 0; _spos = 0; } } int t_spos = Interlocked.Add(ref _spos, 0); int space = 0; int tt_wpos = _wpos % _buffer.Length; int tt_spos = t_spos % _buffer.Length; if(tt_wpos >= tt_spos) space = _buffer.Length - tt_wpos + tt_spos - 1; else space = tt_spos - tt_wpos - 1; if (dataLength > space) { Dbg.ERROR_MSG("PacketSender::send(): no space, Please adjust 'SEND_BUFFER_MAX'! data(" + dataLength + ") > space(" + space + "), wpos=" + _wpos + ", spos=" + t_spos); return false; } int expect_total = tt_wpos + dataLength; if(expect_total <= _buffer.Length) { Array.Copy(stream.data(), stream.rpos, _buffer, tt_wpos, dataLength); } else { int remain = _buffer.Length - tt_wpos; Array.Copy(stream.data(), stream.rpos, _buffer, tt_wpos, remain); Array.Copy(stream.data(), stream.rpos + remain, _buffer, 0, expect_total - _buffer.Length); } Interlocked.Add(ref _wpos, dataLength); if (Interlocked.CompareExchange(ref _sending, 1, 0) == 0) { _startSend(); } return true; }
static int length(IntPtr L) { try { ToLua.CheckArgsCount(L, 1); KBEngine.MemoryStream obj = (KBEngine.MemoryStream)ToLua.CheckObject <KBEngine.MemoryStream>(L, 1); uint o = obj.length(); LuaDLL.lua_pushnumber(L, o); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
/* 服务端通知一个实体进入了世界(如果实体是当前玩家则玩家第一次在一个space中创建了, 如果是其他实体则是其他实体进入了玩家的AOI) */ public void Client_onEntityEnterWorld(MemoryStream stream) { Int32 eid = stream.readInt32(); if(entity_id > 0 && entity_id != eid) _entityIDAliasIDList.Add(eid); UInt16 uentityType; if(EntityDef.idmoduledefs.Count > 255) uentityType = stream.readUint16(); else uentityType = stream.readUint8(); sbyte isOnGround = 1; if(stream.length() > 0) isOnGround = stream.readInt8(); string entityType = EntityDef.idmoduledefs[uentityType].name; // Dbg.DEBUG_MSG("KBEngine::Client_onEntityEnterWorld: " + entityType + "(" + eid + "), spaceID(" + KBEngineApp.app.spaceID + ")!"); Entity entity = null; if(!entities.TryGetValue(eid, out entity)) { MemoryStream entityMessage = null; if(!_bufferedCreateEntityMessage.TryGetValue(eid, out entityMessage)) { Dbg.ERROR_MSG("KBEngine::Client_onEntityEnterWorld: entity(" + eid + ") not found!"); return; } ScriptModule module = null; if(!EntityDef.moduledefs.TryGetValue(entityType, out module)) { Dbg.ERROR_MSG("KBEngine::Client_onEntityEnterWorld: not found module(" + entityType + ")!"); } Type runclass = module.script; if(runclass == null) return; entity = (Entity)Activator.CreateInstance(runclass); entity.id = eid; entity.className = entityType; entity.cellMailbox = new Mailbox(); entity.cellMailbox.id = eid; entity.cellMailbox.className = entityType; entity.cellMailbox.type = Mailbox.MAILBOX_TYPE.MAILBOX_TYPE_CELL; entities[eid] = entity; Client_onUpdatePropertys(entityMessage); _bufferedCreateEntityMessage.Remove(eid); entity.isOnGround = isOnGround > 0; entity.set_direction(entity.getDefinedPropterty("direction")); entity.set_position(entity.getDefinedPropterty("position")); entity.__init__(); entity.enterWorld(); } else { if(!entity.inWorld) { // 安全起见, 这里清空一下 // 如果服务端上使用giveClientTo切换控制权 // 之前的实体已经进入世界, 切换后的实体也进入世界, 这里可能会残留之前那个实体进入世界的信息 _entityIDAliasIDList.Clear(); clearEntities(false); entities[entity.id] = entity; entity.cellMailbox = new Mailbox(); entity.cellMailbox.id = eid; entity.cellMailbox.className = entityType; entity.cellMailbox.type = Mailbox.MAILBOX_TYPE.MAILBOX_TYPE_CELL; entity.set_direction(entity.getDefinedPropterty("direction")); entity.set_position(entity.getDefinedPropterty("position")); _entityServerPos = entity.position; entity.isOnGround = isOnGround > 0; entity.enterWorld(); } } }
/* 服务端通知当前玩家进入了一个新的space */ public void Client_onEntityEnterSpace(MemoryStream stream) { Int32 eid = stream.readInt32(); spaceID = stream.readUint32(); sbyte isOnGround = 1; if(stream.length() > 0) isOnGround = stream.readInt8(); Entity entity = null; if(!entities.TryGetValue(eid, out entity)) { Dbg.ERROR_MSG("KBEngine::Client_onEntityEnterSpace: entity(" + eid + ") not found!"); return; } entity.isOnGround = isOnGround > 0; _entityServerPos = entity.position; entity.enterSpace(); }
/* 服务端初始化客户端的spacedata, spacedata请参考API */ public void Client_initSpaceData(MemoryStream stream) { clearSpace(false); spaceID = stream.readUint32(); while(stream.length() > 0) { string key = stream.readString(); string val = stream.readString(); Client_setSpaceData(spaceID, key, val); } Dbg.DEBUG_MSG("KBEngine::Client_initSpaceData: spaceID(" + spaceID + "), size(" + _spacedatas.Count + ")!"); }
public void onUpdatePropertys_(Int32 eid, MemoryStream stream) { Entity entity = null; if(!entities.TryGetValue(eid, out entity)) { MemoryStream entityMessage = null; if(_bufferedCreateEntityMessage.TryGetValue(eid, out entityMessage)) { Dbg.ERROR_MSG("KBEngine::Client_onUpdatePropertys: entity(" + eid + ") not found!"); return; } MemoryStream stream1 = new MemoryStream(); stream1.wpos = stream.wpos; stream1.rpos = stream.rpos - 4; Array.Copy(stream.data(), stream1.data(), stream.data().Length); _bufferedCreateEntityMessage[eid] = stream1; return; } ScriptModule sm = EntityDef.moduledefs[entity.className]; Dictionary<UInt16, Property> pdatas = sm.idpropertys; while(stream.length() > 0) { UInt16 utype = 0; if(sm.usePropertyDescrAlias) { utype = stream.readUint8(); } else { utype = stream.readUint16(); } Property propertydata = pdatas[utype]; utype = propertydata.properUtype; System.Reflection.MethodInfo setmethod = propertydata.setmethod; object val = propertydata.utype.createFromStream(stream); object oldval = entity.getDefinedProptertyByUType(utype); // Dbg.DEBUG_MSG("KBEngine::Client_onUpdatePropertys: " + entity.className + "(id=" + eid + " " + // propertydata.name + "=" + val + "), hasSetMethod=" + setmethod + "!"); entity.setDefinedProptertyByUType(utype, val); if(setmethod != null) { if(propertydata.isBase() || entity.inWorld) setmethod.Invoke(entity, new object[]{oldval}); } } }
public void onImportClientEntityDef(MemoryStream stream) { createDataTypeFromStreams(stream, true); while(stream.length() > 0) { string scriptmethod_name = stream.readString(); UInt16 scriptUtype = stream.readUint16(); UInt16 propertysize = stream.readUint16(); UInt16 methodsize = stream.readUint16(); UInt16 base_methodsize = stream.readUint16(); UInt16 cell_methodsize = stream.readUint16(); Dbg.DEBUG_MSG("KBEngine::Client_onImportClientEntityDef: import(" + scriptmethod_name + "), propertys(" + propertysize + "), " + "clientMethods(" + methodsize + "), baseMethods(" + base_methodsize + "), cellMethods(" + cell_methodsize + ")!"); ScriptModule module = new ScriptModule(scriptmethod_name); EntityDef.moduledefs[scriptmethod_name] = module; EntityDef.idmoduledefs[scriptUtype] = module; Type Class = module.script; while(propertysize > 0) { propertysize--; UInt16 properUtype = stream.readUint16(); UInt32 properFlags = stream.readUint32(); Int16 ialiasID = stream.readInt16(); string name = stream.readString(); string defaultValStr = stream.readString(); KBEDATATYPE_BASE utype = EntityDef.id2datatypes[stream.readUint16()]; System.Reflection.MethodInfo setmethod = null; if(Class != null) { try{ setmethod = Class.GetMethod("set_" + name); } catch (Exception e) { string err = "KBEngine::Client_onImportClientEntityDef: " + scriptmethod_name + ".set_" + name + ", error=" + e.ToString(); throw new Exception(err); } } Property savedata = new Property(); savedata.name = name; savedata.utype = utype; savedata.properUtype = properUtype; savedata.properFlags = properFlags; savedata.aliasID = ialiasID; savedata.defaultValStr = defaultValStr; savedata.setmethod = setmethod; savedata.val = savedata.utype.parseDefaultValStr(savedata.defaultValStr); module.propertys[name] = savedata; if(ialiasID >= 0) { module.usePropertyDescrAlias = true; module.idpropertys[(UInt16)ialiasID] = savedata; } else { module.usePropertyDescrAlias = false; module.idpropertys[properUtype] = savedata; } //Dbg.DEBUG_MSG("KBEngine::Client_onImportClientEntityDef: add(" + scriptmethod_name + "), property(" + name + "/" + properUtype + ")."); }; while(methodsize > 0) { methodsize--; UInt16 methodUtype = stream.readUint16(); Int16 ialiasID = stream.readInt16(); string name = stream.readString(); Byte argssize = stream.readUint8(); List<KBEDATATYPE_BASE> args = new List<KBEDATATYPE_BASE>(); while(argssize > 0) { argssize--; args.Add(EntityDef.id2datatypes[stream.readUint16()]); }; Method savedata = new Method(); savedata.name = name; savedata.methodUtype = methodUtype; savedata.aliasID = ialiasID; savedata.args = args; if(Class != null) { try{ savedata.handler = Class.GetMethod(name); } catch (Exception e) { string err = "KBEngine::Client_onImportClientEntityDef: " + scriptmethod_name + "." + name + ", error=" + e.ToString(); throw new Exception(err); } } module.methods[name] = savedata; if(ialiasID >= 0) { module.useMethodDescrAlias = true; module.idmethods[(UInt16)ialiasID] = savedata; } else { module.useMethodDescrAlias = false; module.idmethods[methodUtype] = savedata; } //Dbg.DEBUG_MSG("KBEngine::Client_onImportClientEntityDef: add(" + scriptmethod_name + "), method(" + name + ")."); }; while(base_methodsize > 0) { base_methodsize--; UInt16 methodUtype = stream.readUint16(); Int16 ialiasID = stream.readInt16(); string name = stream.readString(); Byte argssize = stream.readUint8(); List<KBEDATATYPE_BASE> args = new List<KBEDATATYPE_BASE>(); while(argssize > 0) { argssize--; args.Add(EntityDef.id2datatypes[stream.readUint16()]); }; Method savedata = new Method(); savedata.name = name; savedata.methodUtype = methodUtype; savedata.aliasID = ialiasID; savedata.args = args; module.base_methods[name] = savedata; module.idbase_methods[methodUtype] = savedata; //Dbg.DEBUG_MSG("KBEngine::Client_onImportClientEntityDef: add(" + scriptmethod_name + "), base_method(" + name + ")."); }; while(cell_methodsize > 0) { cell_methodsize--; UInt16 methodUtype = stream.readUint16(); Int16 ialiasID = stream.readInt16(); string name = stream.readString(); Byte argssize = stream.readUint8(); List<KBEDATATYPE_BASE> args = new List<KBEDATATYPE_BASE>(); while(argssize > 0) { argssize--; args.Add(EntityDef.id2datatypes[stream.readUint16()]); }; Method savedata = new Method(); savedata.name = name; savedata.methodUtype = methodUtype; savedata.aliasID = ialiasID; savedata.args = args; module.cell_methods[name] = savedata; module.idcell_methods[methodUtype] = savedata; //Dbg.DEBUG_MSG("KBEngine::Client_onImportClientEntityDef: add(" + scriptmethod_name + "), cell_method(" + name + ")."); }; if(module.script == null) { Dbg.ERROR_MSG("KBEngine::Client_onImportClientEntityDef: module(" + scriptmethod_name + ") not found!"); } foreach(string name in module.methods.Keys) { // Method infos = module.methods[name]; if(module.script != null && module.script.GetMethod(name) == null) { Dbg.WARNING_MSG(scriptmethod_name + "(" + module.script + "):: method(" + name + ") no implement!"); } }; } onImportEntityDefCompleted(); }
public override void onUpdatePropertys(MemoryStream stream) { ScriptModule sm = EntityDef.moduledefs["SpaceRoom"]; Dictionary <UInt16, Property> pdatas = sm.idpropertys; while (stream.length() > 0) { UInt16 _t_utype = 0; UInt16 _t_child_utype = 0; { if (sm.usePropertyDescrAlias) { _t_utype = stream.readUint8(); _t_child_utype = stream.readUint8(); } else { _t_utype = stream.readUint16(); _t_child_utype = stream.readUint16(); } } Property prop = null; if (_t_utype == 0) { prop = pdatas[_t_child_utype]; } else { Property pComponentPropertyDescription = pdatas[_t_utype]; switch (pComponentPropertyDescription.properUtype) { default: break; } return; } switch (prop.properUtype) { case 40001: Vector3 oldval_direction = direction; direction = stream.readVector3(); if (prop.isBase()) { if (inited) { onDirectionChanged(oldval_direction); } } else { if (inWorld) { onDirectionChanged(oldval_direction); } } break; case 40000: Vector3 oldval_position = position; position = stream.readVector3(); if (prop.isBase()) { if (inited) { onPositionChanged(oldval_position); } } else { if (inWorld) { onPositionChanged(oldval_position); } } break; case 40002: stream.readUint32(); break; default: break; } ; } }
public override void onUpdatePropertys(MemoryStream stream) { ScriptModule sm = EntityDef.moduledefs["Avatar"]; Dictionary <UInt16, Property> pdatas = sm.idpropertys; while (stream.length() > 0) { UInt16 _t_utype = 0; UInt16 _t_child_utype = 0; { if (sm.usePropertyDescrAlias) { _t_utype = stream.readUint8(); _t_child_utype = stream.readUint8(); } else { _t_utype = stream.readUint16(); _t_child_utype = stream.readUint16(); } } Property prop = null; if (_t_utype == 0) { prop = pdatas[_t_child_utype]; } else { Property pComponentPropertyDescription = pdatas[_t_utype]; switch (pComponentPropertyDescription.properUtype) { case 16: component1.onUpdatePropertys(_t_child_utype, stream, -1); break; case 21: component2.onUpdatePropertys(_t_child_utype, stream, -1); break; case 22: component3.onUpdatePropertys(_t_child_utype, stream, -1); break; default: break; } return; } switch (prop.properUtype) { case 47001: Int32 oldval_HP = HP; HP = stream.readInt32(); if (prop.isBase()) { if (inited) { onHPChanged(oldval_HP); } } else { if (inWorld) { onHPChanged(oldval_HP); } } break; case 47002: Int32 oldval_HP_Max = HP_Max; HP_Max = stream.readInt32(); if (prop.isBase()) { if (inited) { onHP_MaxChanged(oldval_HP_Max); } } else { if (inWorld) { onHP_MaxChanged(oldval_HP_Max); } } break; case 47003: Int32 oldval_MP = MP; MP = stream.readInt32(); if (prop.isBase()) { if (inited) { onMPChanged(oldval_MP); } } else { if (inWorld) { onMPChanged(oldval_MP); } } break; case 47004: Int32 oldval_MP_Max = MP_Max; MP_Max = stream.readInt32(); if (prop.isBase()) { if (inited) { onMP_MaxChanged(oldval_MP_Max); } } else { if (inWorld) { onMP_MaxChanged(oldval_MP_Max); } } break; case 16: component1.createFromStream(stream); break; case 21: component2.createFromStream(stream); break; case 22: component3.createFromStream(stream); break; case 40001: Vector3 oldval_direction = direction; direction = stream.readVector3(); if (prop.isBase()) { if (inited) { onDirectionChanged(oldval_direction); } } else { if (inWorld) { onDirectionChanged(oldval_direction); } } break; case 47005: Int32 oldval_forbids = forbids; forbids = stream.readInt32(); if (prop.isBase()) { if (inited) { onForbidsChanged(oldval_forbids); } } else { if (inWorld) { onForbidsChanged(oldval_forbids); } } break; case 41002: UInt16 oldval_level = level; level = stream.readUint16(); if (prop.isBase()) { if (inited) { onLevelChanged(oldval_level); } } else { if (inWorld) { onLevelChanged(oldval_level); } } break; case 41006: UInt32 oldval_modelID = modelID; modelID = stream.readUint32(); if (prop.isBase()) { if (inited) { onModelIDChanged(oldval_modelID); } } else { if (inWorld) { onModelIDChanged(oldval_modelID); } } break; case 41007: Byte oldval_modelScale = modelScale; modelScale = stream.readUint8(); if (prop.isBase()) { if (inited) { onModelScaleChanged(oldval_modelScale); } } else { if (inWorld) { onModelScaleChanged(oldval_modelScale); } } break; case 11: Byte oldval_moveSpeed = moveSpeed; moveSpeed = stream.readUint8(); if (prop.isBase()) { if (inited) { onMoveSpeedChanged(oldval_moveSpeed); } } else { if (inWorld) { onMoveSpeedChanged(oldval_moveSpeed); } } break; case 41003: string oldval_name = name; name = stream.readUnicode(); if (prop.isBase()) { if (inited) { onNameChanged(oldval_name); } } else { if (inWorld) { onNameChanged(oldval_name); } } break; case 6: UInt16 oldval_own_val = own_val; own_val = stream.readUint16(); if (prop.isBase()) { if (inited) { onOwn_valChanged(oldval_own_val); } } else { if (inWorld) { onOwn_valChanged(oldval_own_val); } } break; case 40000: Vector3 oldval_position = position; position = stream.readVector3(); if (prop.isBase()) { if (inited) { onPositionChanged(oldval_position); } } else { if (inWorld) { onPositionChanged(oldval_position); } } break; case 40002: stream.readUint32(); break; case 41001: UInt32 oldval_spaceUType = spaceUType; spaceUType = stream.readUint32(); if (prop.isBase()) { if (inited) { onSpaceUTypeChanged(oldval_spaceUType); } } else { if (inWorld) { onSpaceUTypeChanged(oldval_spaceUType); } } break; case 47006: SByte oldval_state = state; state = stream.readInt8(); if (prop.isBase()) { if (inited) { onStateChanged(oldval_state); } } else { if (inWorld) { onStateChanged(oldval_state); } } break; case 47007: Byte oldval_subState = subState; subState = stream.readUint8(); if (prop.isBase()) { if (inited) { onSubStateChanged(oldval_subState); } } else { if (inWorld) { onSubStateChanged(oldval_subState); } } break; case 41004: UInt32 oldval_uid = uid; uid = stream.readUint32(); if (prop.isBase()) { if (inited) { onUidChanged(oldval_uid); } } else { if (inWorld) { onUidChanged(oldval_uid); } } break; case 41005: UInt32 oldval_utype = utype; utype = stream.readUint32(); if (prop.isBase()) { if (inited) { onUtypeChanged(oldval_utype); } } else { if (inWorld) { onUtypeChanged(oldval_utype); } } break; default: break; } ; } }
public override void onUpdatePropertys(UInt16 propUtype, MemoryStream stream, int maxCount) { ScriptModule sm = EntityDef.moduledefs["TestNoBase"]; Dictionary <UInt16, Property> pdatas = sm.idpropertys; while (stream.length() > 0 && maxCount-- != 0) { UInt16 _t_utype = 0; UInt16 _t_child_utype = propUtype; if (_t_child_utype == 0) { if (sm.usePropertyDescrAlias) { _t_utype = stream.readUint8(); _t_child_utype = stream.readUint8(); } else { _t_utype = stream.readUint16(); _t_child_utype = stream.readUint16(); } } Property prop = null; prop = pdatas[_t_child_utype]; switch (prop.properUtype) { case 24: Int32 oldval_own = own; own = stream.readInt32(); if (prop.isBase()) { if (owner.inited) { onOwnChanged(oldval_own); } } else { if (owner.inWorld) { onOwnChanged(oldval_own); } } break; case 23: Int32 oldval_state = state; state = stream.readInt32(); if (prop.isBase()) { if (owner.inited) { onStateChanged(oldval_state); } } else { if (owner.inWorld) { onStateChanged(oldval_state); } } break; default: break; } ; } }
public override void onUpdatePropertys(MemoryStream stream) { ScriptModule sm = EntityDef.moduledefs["Avatar"]; Dictionary <UInt16, Property> pdatas = sm.idpropertys; while (stream.length() > 0) { UInt16 _t_utype = 0; UInt16 _t_child_utype = 0; { if (sm.usePropertyDescrAlias) { _t_utype = stream.readUint8(); _t_child_utype = stream.readUint8(); } else { _t_utype = stream.readUint16(); _t_child_utype = stream.readUint16(); } } Property prop = null; if (_t_utype == 0) { prop = pdatas[_t_child_utype]; } else { Property pComponentPropertyDescription = pdatas[_t_utype]; switch (pComponentPropertyDescription.properUtype) { default: break; } return; } switch (prop.properUtype) { case 40001: Vector3 oldval_direction = direction; direction = stream.readVector3(); if (prop.isBase()) { if (inited) { onDirectionChanged(oldval_direction); } } else { if (inWorld) { onDirectionChanged(oldval_direction); } } break; case 5: Byte oldval_level = level; level = stream.readUint8(); if (prop.isBase()) { if (inited) { onLevelChanged(oldval_level); } } else { if (inWorld) { onLevelChanged(oldval_level); } } break; case 4: Int32 oldval_mass = mass; mass = stream.readInt32(); if (prop.isBase()) { if (inited) { onMassChanged(oldval_mass); } } else { if (inWorld) { onMassChanged(oldval_mass); } } break; case 9: Byte oldval_modelID = modelID; modelID = stream.readUint8(); if (prop.isBase()) { if (inited) { onModelIDChanged(oldval_modelID); } } else { if (inWorld) { onModelIDChanged(oldval_modelID); } } break; case 7: float oldval_modelScale = modelScale; modelScale = stream.readFloat(); if (prop.isBase()) { if (inited) { onModelScaleChanged(oldval_modelScale); } } else { if (inWorld) { onModelScaleChanged(oldval_modelScale); } } break; case 6: float oldval_moveSpeed = moveSpeed; moveSpeed = stream.readFloat(); if (prop.isBase()) { if (inited) { onMoveSpeedChanged(oldval_moveSpeed); } } else { if (inWorld) { onMoveSpeedChanged(oldval_moveSpeed); } } break; case 2: string oldval_name = name; name = stream.readUnicode(); if (prop.isBase()) { if (inited) { onNameChanged(oldval_name); } } else { if (inWorld) { onNameChanged(oldval_name); } } break; case 40000: Vector3 oldval_position = position; position = stream.readVector3(); if (prop.isBase()) { if (inited) { onPositionChanged(oldval_position); } } else { if (inWorld) { onPositionChanged(oldval_position); } } break; case 40002: stream.readUint32(); break; case 8: SByte oldval_state = state; state = stream.readInt8(); if (prop.isBase()) { if (inited) { onStateChanged(oldval_state); } } else { if (inWorld) { onStateChanged(oldval_state); } } break; default: break; } ; } }