public void SERVER_ONLY_NATIVE_create_actor(String typename, String name, Action <DefaultErrCode, String, UInt32> callback, RpcContext context) { this.CreateActor(typename, name, callback, context); }
//移除actor public void RemoveActor(ulong actorId, Action <DefaultErrCode> callback, RpcContext __context) { var a = this.actorDic[actorId]; }
public void BindClientActor(string actorName, Action <DefaultErrCode> callback, RpcContext __context) { //首先这个actor一定是本地的 //如果actor不在本地,则把请求转到目标host上去 //TODO,等想到了应用场景再加 //find actor.server var actorId = Global.IdManager.GetActorId(actorName); //var hostAddr = Global.IdManager.GetHostAddrByActorId(actorId, false); Global.IdManager.RegisterClientActor(actorId, actorName, __context.Packet.FromHostId, __context.Peer.RemoteAddress.ToIPv4String()); //give actor.server hostId, ipaddr to client callback(DefaultErrCode.OK); //Set actor.server's client property Log.Info("binding_client_actor", actorId); var a = Global.Host.GetActor(actorId); a.OnClientEnable(); }
public async Task AddActorId(ulong hostId, ulong actorId, string actorName, string aTypeName, Action <bool> callback, RpcContext ctx) { var result = Global.IdManager.RegisterActor(actorId, actorName, aTypeName, hostId, false, noReg: true); var actorInfo = Global.IdManager.GetActorInfo(actorId); //notify all hosts foreach (var h in HostRefDic.Values) { if (h.toHostId != hostId) { await h.OnAddActorIdAsync(actorInfo, (code) => { Log.Info("Notify(AddActorId):", h.toHostId); }); } } callback(result); }
public void CreateActor(string typename, string name, Action <DefaultErrCode, string, ulong> callback, RpcContext __context) { if (name == "" || name == null) { callback(DefaultErrCode.ERROR, "", 0); return; } var actorId = Global.IdManager.GetActorId(name); if (this.actorDic.TryGetValue(actorId, out var a)) { callback(DefaultErrCode.create_actor_already_exists, a.UniqueName, a.Id); return; } #if !CLIENT var hostId = Global.IdManager.GetHostIdByActorId(actorId); if (hostId != 0 && Global.Host.Id != hostId) { //callback(DefaultErrCode.create_actor_remote_exists, name, actorId); //return; //迁移actor到本地 var remoteHost = this.GetHost(hostId); remoteHost.MigrateActor(actorId, (code, actorData) => { var actor = CreateActorLocally(typename, actorData); if (actor != null) { callback(DefaultErrCode.OK, actor.UniqueName, actor.Id); } else { callback(DefaultErrCode.ERROR, "", 0); } }); return; } #endif a = CreateActorLocally(typename, name); if (a != null) { callback(DefaultErrCode.OK, a.UniqueName, a.Id); } else { callback(DefaultErrCode.ERROR, "", 0); } }
public async Task RemoveClientActor(ulong actorId, DisconnectReason reason, Action <DefaultErrCode> callback, RpcContext ctx) { var clientId = Global.IdManager.GetHostIdByActorId(actorId, true); if (clientId == 0) { callback(DefaultErrCode.OK); return; } var hostId = Global.IdManager.GetHostIdByActorId(actorId); if (hostId != this.Id && hostId != 0) { //call remote host GetHost(hostId)?.RemoveClientActor(actorId, reason, callback); return; } var clientHost = this.GetHost(clientId); if (clientHost != null) { Log.Info("begin_notify_client_close", clientId); var result = await clientHost.OnBeforeDisconnectAsync(reason); Log.Info(result?.ToString()); } var peer = Global.NetManager.GetLocalPeerById(clientId, Global.Config.ClientNetwork); if (peer != null && !Global.NetManager.Deregister(peer)) { Global.IdManager.RemoveClientHost(clientId); callback(DefaultErrCode.ERROR); return; } Global.IdManager.RemoveClientHost(clientId); callback(DefaultErrCode.OK); }
public async Task RemoveClientHostId(ulong fromHostId, ulong clientId, Action <bool> callback, RpcContext ctx) { var result = Global.IdManager.RemoveClientHost(clientId, noReg: true); //notify all hosts foreach (var h in HostRefDic.Values) { if (h.toHostId != fromHostId) { await h.OnRemoveClientHostIdAsync(clientId, (code) => { Log.Info("Notify(RemoveClientHostId):", h.toHostId); }); } } callback(result); }
public void SERVER_API_NATIVE_bind_client_actor(String actorName, Action <DefaultErrCode> callback, RpcContext context) { this.BindClientActor(actorName, callback, context); }
public void OnAddHostId(HostInfo hostInfo, Action <bool> callback, RpcContext ctx) { var result = Global.IdManager.RegisterHostInfo(hostInfo); callback(result); }
public void SERVER_ONLY_migrate_actor(IMessage msg, RpcContext context) { var _msg = (MigrateActorReq)msg; this.MigrateActor(_msg.actorId, context); }
public void SERVER_ONLY_remove_actor(IMessage msg, RpcContext context) { var _msg = (RemoveActorReq)msg; this.RemoveActor(_msg.actorId, context); }
public void SERVER_API_register(IMessage msg, RpcContext context) { var _msg = (RegisterReq)msg; this.Register(_msg.hostId, _msg.hostName, context); }
public void SERVER_ONLY_NATIVE_remove_actor(UInt32 actorId, RpcContext context) { this.RemoveActor(actorId, context); }
public void SERVER_ONLY_NATIVE_migrate_actor(UInt32 actorId, RpcContext context) { this.MigrateActor(actorId, context); }
public void RegisterClient(ulong hostId, string hostName, Action <DefaultErrCode, HostInfo> callback, RpcContext ctx) { //var _oldId = Global.IdManager.GetHostId(ctx.Peer.RemoteAddress.ToIPv4String()); //if (_oldId == hostId && Global.IdManager.GetHostAddr(hostId) != ctx.Peer.RemoteAddress.ToIPv4String()) //{ // Global.NetManager.Deregister(ctx.Peer); // callback(DefaultErrCode.client_host_already_exists, Global.IdManager.GetHostInfo(this.Id)); // return; //} Global.IdManager.AddAddressID(ctx.Peer.ConnId, hostId); if (ctx.Peer.ConnId != hostId) { Global.NetManager.ChangePeerId(ctx.Peer.ConnId, hostId, hostName, ctx.Peer.RemoteAddress.ToIPv4String() ); } Global.NetManager.RegisterClient(hostId, hostName, ctx.Peer); var hostInfo = Global.IdManager.GetHostInfo(this.Id, true); hostInfo.HostAddr = hostInfo.HostExtAddr; callback(DefaultErrCode.OK, hostInfo); }
public void OnAddActorId(ActorInfo actorInfo, Action <bool> callback, RpcContext ctx) { var result = Global.IdManager.RegisterActorInfo(actorInfo); callback(result); }
public void SayHello(Action <DefaultErrCode, HostInfo> callback, RpcContext ctx) { callback(DefaultErrCode.OK, Global.IdManager.GetHostInfo(this.Id)); }
public void OnRemoveClientHostId(ulong clientId, Action <bool> callback, RpcContext ctx) { var result = Global.IdManager.RemoveClientHost(clientId, noReg: true); callback(result); }
public async Task AddHostId(ulong hostId, string hostName, string intAddr, string extAddr, Action <bool, IdDataSet> callback, RpcContext ctx) { var result = Global.IdManager.RegisterHost(ctx.Peer.ConnId, hostId, hostName, intAddr, extAddr, false, noReg: true); //add hostref if (Global.IdManager.IsSameLocalhost(intAddr, this.InternalAddress.ToIPv4String())) { HostRefDic[hostId] = this.GetHost(hostName, intAddr); } else { HostRefDic[hostId] = this.GetHost(hostName, extAddr); } var hostInfo = Global.IdManager.GetHostInfo(hostId, true); //notify all hosts foreach (var h in HostRefDic.Values) { if (h.toHostId != hostId) { await h.OnAddHostIdAsync(hostInfo, (code) => { Log.Info("Notify(AddHostId):", h.toHostId); }); } } callback(result, Global.IdManager.GetIdAll()); }
public void OnAddClientActorId(ulong clientId, ulong actorId, string actorName, string address, Action <bool> callback, RpcContext ctx) { var result = Global.IdManager.RegisterClientActor(actorId, actorName, clientId, address, noReg: true); callback(result); }
public async Task AddClientActorId(ulong fromHostId, ulong clientId, ulong actorId, string actorName, string address, Action <bool> callback, RpcContext ctx) { var result = Global.IdManager.RegisterClientActor(actorId, actorName, clientId, address, noReg: true); //notify all hosts foreach (var h in HostRefDic.Values) { if (h.toHostId != fromHostId) { await h.OnAddClientActorIdAsync(clientId, actorId, actorName, address, (code) => { Log.Info("Notify(AddClientActorId):", h.toHostId); }); } } callback(result); }
public void OnRemoveActorId(ulong actorId, Action <bool> callback, RpcContext ctx) { var result = Global.IdManager.RemoveActorId(actorId, noReg: true); callback(result); }
protected void OnReceive(NetPeer peer, IByteBuffer buffer) { if (!peer.IsActive) { return; } //Log.Debug(string.Format("RECV({0}) {1} {2} {3}", peer.netType, peer.ConnId, peer.RemoteAddress, StringUtil.ToHexString(buffer.ToArray()))); if (buffer.ReadableBytes == 1) { byte opCode = buffer.ReadByte(); //Log.Warn("RECV_PROTOCOL", opCode); if (opCode == (byte)OpCode.PING) { //Log.Info(string.Format("Ping({0}) {1} FROM {2}", peer.netType, peer.ConnId, peer.RemoteAddress)); peer.Pong(); if (peer != null && peer.RemoteAddress != null) { Global.IdManager.ReregisterHost(peer.ConnId, peer.RemoteAddress.ToIPv4String()); } #if !CLIENT //如果peer是客户端,则代表 var clientActorId = Global.IdManager.GetClientActorId(peer.ConnId); if (clientActorId != 0 && this.actorDic.ContainsKey(clientActorId)) { Global.IdManager.RegisterClientActor(clientActorId, GetActor(clientActorId).UniqueName, peer.ConnId, peer.RemoteAddress.ToIPv4String()); } #endif Global.NetManager.OnPong(peer); return; } else if (opCode == (byte)OpCode.PONG) { #if CLIENT //Log.Info("ping>>>" + (TimeUtil.GetTimeStampMS2() - lastTs).ToString()); #endif peer.lastTickTime = TimeUtil.GetTimeStampMS2(); Global.NetManager.OnPong(peer); return; } else if (opCode == (byte)OpCode.GOODBYE) { //删除这个连接 Global.NetManager.Deregister(peer); return; } return; } uint protoCode = buffer.ReadUnsignedIntLE(); #if !CLIENT if (protoCode == OpCode.REGISTER_REQ) { var hostId = (ulong)buffer.ReadLongLE(); var nameBytes = new byte[buffer.ReadableBytes]; buffer.ReadBytes(nameBytes); var hostName = Encoding.UTF8.GetString(nameBytes); var context = new RpcContext(null, peer); this.Register(hostId, hostName, context); return; } #endif if (protoCode == OpCode.PARTIAL) { var partialId = (ulong)buffer.ReadLongLE(); var partIndex = buffer.ReadByte(); var totPartCount = buffer.ReadByte(); var payload = new byte[buffer.ReadableBytes]; buffer.ReadBytes(payload); var finalBytes = Global.NetManager.AddPartialRpc(partialId, partIndex, totPartCount, payload); if (finalBytes != null) { var finalBuf = Unpooled.WrappedBuffer(finalBytes); var _protoCode = finalBuf.ReadUnsignedIntLE(); #if !CLIENT if (_protoCode == OpCode.REGISTER_REQ) { ProcessRegisterProtocol(peer, _protoCode, finalBuf); } else { ProcessRpcProtocol(peer, _protoCode, finalBuf); } #else ProcessRpcProtocol(peer, _protoCode, finalBuf); #endif } return; } ProcessRpcProtocol(peer, protoCode, buffer); }
public void CreateActor(string typename, string name, Action <DefaultErrCode, ActorInfo> callback, RpcContext ctx) { if (name == "" || name == null) { callback(DefaultErrCode.ERROR, null); return; } var actorId = Global.IdManager.GetActorId(name); if (this.actorDic.TryGetValue(actorId, out var a)) { Log.Info("create_actor_exists", actorId, a); a.Activate(); callback(DefaultErrCode.create_actor_already_exists, Global.IdManager.GetActorInfo(a.Id)); return; } #if !CLIENT var hostId = Global.IdManager.GetHostIdByActorId(actorId); if (hostId != 0 && Global.Host.Id != hostId) { //callback(DefaultErrCode.create_actor_remote_exists, name, actorId); //return; //迁移actor到本地 Log.Info("create_actor_exists2", actorId, hostId); var remoteHost = this.GetHost(hostId); remoteHost.MigrateActor(actorId, (code, actorData, actorInfo) => { Global.IdManager.RegisterActorInfo(actorInfo); var actor = CreateActorLocally(typename, actorData); if (actor != null) { callback(DefaultErrCode.OK, Global.IdManager.GetActorInfo(actor.Id)); } else { callback(DefaultErrCode.ERROR, null); } }); return; } #endif a = CreateActorLocally(typename, name); Log.Info("actor_create_result", a != null); if (a != null) { callback(DefaultErrCode.OK, Global.IdManager.GetActorInfo(a.Id)); } else { callback(DefaultErrCode.ERROR, null); } Log.Info("actor_create_cb"); }
public void MigrateActor(ulong actorId, Action <DefaultErrCode, byte[]> callback, RpcContext __context) { if (!this.actorDic.ContainsKey(actorId)) { callback(DefaultErrCode.migrate_actor_not_exists, null); return; } this.actorDic.TryRemove(actorId, out var a); var actorData = a.Pack(); if (a.Client != null) { var clientId = Global.IdManager.GetHostIdByActorId(actorId, isClient: true); if (clientId != 0) { var peer = Global.NetManager.GetPeerById(clientId, Global.Config.ClientNetwork); Global.NetManager.Deregister(peer); } /* if a client's actor is created somewhere else * it means the client is kicked out by another client * so the old client shall be destroyed. * IN SHORT: a client actor cannot migrate with server actor */ } a.Destroy(); callback(DefaultErrCode.OK, actorData); }
[ServerOnly] //移除actor public void RemoveActor(ulong actorId, Action <DefaultErrCode> callback, RpcContext ctx) { this.RemoveActorById(actorId); callback(DefaultErrCode.OK); }
public void RegisterClient(ulong hostId, string hostName, Action <DefaultErrCode, HostInfo> callback, RpcContext __context) { //var _oldId = Global.IdManager.GetHostId(__context.Peer.RemoteAddress.ToIPv4String()); //if (_oldId == hostId && Global.IdManager.GetHostAddr(hostId) != __context.Peer.RemoteAddress.ToIPv4String()) //{ // Global.NetManager.Deregister(__context.Peer); // callback(DefaultErrCode.client_host_already_exists, Global.IdManager.GetHostInfo(this.Id)); // return; //} if (__context.Peer.ConnId != hostId) { Global.NetManager.ChangePeerId(__context.Peer.ConnId, hostId, hostName, __context.Peer.RemoteAddress.ToIPv4String()); } Global.NetManager.RegisterClient(hostId, hostName, __context.Peer); callback(DefaultErrCode.OK, Global.IdManager.GetHostInfo(this.Id)); }
public void Register(ulong hostId, string hostName, Action <DefaultErrCode, HostInfo> callback, RpcContext ctx) { if (ctx.Peer.ConnId != hostId) { //修正一下peer的id Global.NetManager.ChangePeerId(ctx.Peer.ConnId, hostId, hostName, ctx.Peer.RemoteAddress.ToIPv4String()); } else { Global.IdManager.RegisterHost(ctx.Peer.ConnId, hostId, hostName, ctx.Peer.RemoteAddress.ToIPv4String(), ctx.Peer.RemoteAddress.ToIPv4String(), false); } callback(DefaultErrCode.OK, Global.IdManager.GetHostInfo(this.Id, false)); }
public async Task RemoveClientActor(ulong actorId, DisconnectReason reason, Action <DefaultErrCode> callback, RpcContext __context) { var hostId = Global.IdManager.GetHostIdByActorId(actorId); var clientId = Global.IdManager.GetHostIdByActorId(actorId, true); if (clientId == 0) { callback(DefaultErrCode.OK); return; } if (hostId != this.Id && hostId != 0) { //call remote host this.GetHost(hostId)?.RemoveClientActor(actorId, reason, callback); return; } var clientHost = this.GetHost(clientId); if (clientHost != null) { Log.Info(await clientHost.OnBeforeDisconnectAsync(reason)); } var peer = Global.NetManager.GetPeerById(clientId, Global.Config.ClientNetwork); if (peer != null && !Global.NetManager.Deregister(peer)) { callback(DefaultErrCode.ERROR); return; } callback(DefaultErrCode.OK); }
public void SERVER_API_NATIVE_register_client(UInt32 hostId, String hostName, Action <DefaultErrCode, HostInfo> callback, RpcContext context) { this.RegisterClient(hostId, hostName, callback, context); }