/// <summary> /// Gets an RPC proxy for the provided interface. /// </summary> /// <typeparam name="IT">The interface type.</typeparam> /// <param name="configuration">The configuration.</param> /// <returns></returns> public IT Proxy <IT>(ProxyConfiguration configuration) { // check type is interface TypeInfo typeInfo = typeof(IT).GetTypeInfo(); if (!typeInfo.IsInterface) { throw new InvalidOperationException("A static RPC proxy must be derived from an interface"); } // get contract attribute RpcContractAttribute contractAttr = typeInfo.GetCustomAttribute <RpcContractAttribute>(); if (contractAttr == null) { throw new InvalidOperationException("A static RPC proxy must be decorated with a contract attribute"); } // create proxy IT proxy = DispatchProxy.Create <IT, RpcProxy <IT> >(); RpcProxy <IT> rpcProxy = (RpcProxy <IT>)(object) proxy; rpcProxy.Channel = this; rpcProxy.Configuration = configuration; return(proxy); }
public async void TestProxyCreation() { var caculator = RpcProxy.Create <ICaculator, ProxyTest>(); Assert.Equal(3, await caculator.Add(1, 2)); Assert.Equal("Hello World", await caculator.Concact("Hello ", "World")); }
/// <summary> /// 获取PK榜单排名 /// </summary> public static async Task <int> RemoteGetPKRank(int serverId, int userId) { if (RunServerId.Value < 0) { return(-1); } if (serverId < 0) { return(-1); } if (userId == 0) { return(-1); } return(await RpcProxy.RunAsync(typeof(RankCenterService), RunServerId.Value, RpcProxy.BuildArgs(serverId), () => { if (PKRankPool == null) { Logger.LogError("PKRankPool == null"); return -1; } var rank = PKRankPool.GetRank(userId); Logger.LogDebug($"[Success]ServerId[{serverId}] get PKRank UserId[{userId}]Rank[{rank}]"); return rank; } )); }
public static async Task <int> RemoteDeleteCOOPRankItem(int serverId, string key) { if (RunServerId.Value < 0) { return(-1); } if (serverId < 0) { return(-1); } if (string.IsNullOrEmpty(key)) { return(-1); } return(await RpcProxy.RunAsync(typeof(RankCenterService), RunServerId.Value, RpcProxy.BuildArgs(serverId, key), () => { if (COOPRankPool == null) { Logger.LogError("COOPRankPool == null"); return -1; } var rank = COOPRankPool.Remove(key); Logger.LogDebug($"[Success]ServerId[{serverId}] delete COOPRankItem Key[{key}]Rank[{rank}]"); return rank; } )); }
public static async Task <TRankPack> RemoteLoadPack(int serverId) { if (serverId < 0) { return(null); } if (RunServerId.Value < 0) { return(null); } return(await RpcProxy.RunAsync(typeof(RankCenterService), RunServerId.Value, RpcProxy.BuildArgs(serverId), () => { if (PKRankPool == null) { Logger.LogError("PKRankPool == null"); return null; } if (COOPRankPool == null) { Logger.LogError("COOPRankPool == null"); return null; } var pack = new TRankPack() { PKList = PKRankPool.GetRange(0, 99), COOPList = COOPRankPool.GetRange(0, 99), }; var pkCount = pack.PKList == null ? 0 : pack.PKList.Count; var coopCount = pack.COOPList == null ? 0 : pack.COOPList.Count; Logger.LogDebug($"[Success]ServerId[{serverId}] load RankPack pkCount[{pkCount}]coopCount[{coopCount}]"); return pack; } )); }
public static void Invoke(string methodId, IPeer peer, ResponseCallback responseCallback, Action<ISerializable> action) { using (var serializable = new Serializable(null)) { action?.Invoke(serializable); var rpcInvoke = new RpcInvoke("", methodId); RpcProxy.Invoke(MessageType.RpcInvoke, rpcInvoke, serializable.WriteStream, peer, responseCallback); } }
public static async Task <int> RemoteAddCOOPRankItem(int serverId, TRankCOOPItem item, int robotScore1, int robotScore2) { if (RunServerId.Value < 0) { return(-1); } if (item == null) { return(-1); } if ((item.UserId1) == 0 && (item.UserId2 == 0)) { return(-1); } return(await RpcProxy.RunAsync(typeof(RankCenterService), RunServerId.Value, RpcProxy.BuildArgs(serverId, item, robotScore1, robotScore2), () => { if (COOPRankPool == null) { Logger.LogError("COOPRankPool == null"); return -1; } //低于允许上榜的最低回合数 if (item.Round < ConfigConstants.COOP_RANKING_MIN) { var key = item.PrimaryKey; //从榜单池中移除 var rank = COOPRankPool.Remove(key); //删除数据 TRankCOOPItem.Cache.TryDelete(key); Logger.LogDebug($"[Fail]ServerId[{serverId}] add COOPRankItem Key[{key}]Round[{item.Round}]Rank[{rank}] < COOP_RANKING_MIN"); } //允许上榜 else { var rank = COOPRankPool.Add(item); //加入成功, 保存数据 if (rank > 0) { TRankCOOPItem.Cache.AddOrUpdate(item); } Logger.LogDebug($"[Success]ServerId[{serverId}] add COOPRankItem Key[{item.PrimaryKey}]Round[{item.Round}]Rank[{rank}]"); //机器人自动上榜PVP if ((item.UserId1 < 0) && (robotScore1 >= ConfigConstants.PK_RANKING_MIN) && (!PKRankPool.ContainsKey(item.UserId1))) { var pkItem = new TRankPKItem(-1, item.UserId1, item.Name1, robotScore1, item.Team1.HeroBaseId, item.Team1.TowerPool, item.Team1.FieldId, DateTime.UtcNow.ToSecondsSinceEpoch()); PKRankPool.Add(pkItem); } if ((item.UserId2 < 0) && (robotScore2 >= ConfigConstants.PK_RANKING_MIN) && (!PKRankPool.ContainsKey(item.UserId2))) { var pkItem = new TRankPKItem(-1, item.UserId2, item.Name2, robotScore2, item.Team2.HeroBaseId, item.Team2.TowerPool, item.Team2.FieldId, DateTime.UtcNow.ToSecondsSinceEpoch()); PKRankPool.Add(pkItem); } } return GetCOOPMinRound(); } )); }
public RpcProxy GetProxy <T>(string impName = null) where T : class { if (string.IsNullOrEmpty(impName)) { impName = typeof(T).Name; } var proxy = new RpcProxy(typeof(T), impName); proxy.RpcInvoke = this; return(proxy); }
public IUser AddListener(SocketService socketService) { var peer = PoolAllocator <IPeer> .GetObject() as Peer; peer.SetStatus(ConnectionStatus.Connected, socketService, this); socketService.HandleDisconnect += () => { if (peer.Status != ConnectionStatus.Connected) { return; } Disconnected?.Invoke(peer); peer.Recycle(); }; socketService.HandleRead += (readStream) => { var tmp = readStream.Clone(); _messageQueue.Enqueue(() => { var securityComponent = peer.GetComponent <ISecurityComponent>(); if (securityComponent == null) { Logger.Error($"{nameof(securityComponent)}组件为空!"); return; } var remoteMessageId = tmp.ShiftRight <ulong>(); Ssfi.Security.DecryptAES(tmp, securityComponent.AesKey); RpcProxy.Invoke(remoteMessageId, tmp, peer); tmp.Dispose(); }); }; socketService.HandleError += reason => Logger.Error(reason); socketService.HandleWrite += (isSuccess) => { if (!isSuccess) { Logger.Error($"消息发送失败!"); } }; socketService.HandleAcknowledge += (isSuccess, readStream) => { var securityComponent = peer.GetComponent <ISecurityComponent>(); if (securityComponent == null) { Logger.Error($"{nameof(securityComponent)}组件为空!"); return; } // local var localMessageId = readStream.ShiftRight <ulong>(); peer.Acknowledge(isSuccess, localMessageId); }; return(peer); }
public static RpcProxy GetProxy <T>(this ITcpSession session, string impName = null) where T : class { if (session == null) { throw new Exception("session Can't be empty"); } if (string.IsNullOrEmpty(impName)) { impName = typeof(T).Name; } var proxy = new RpcProxy(typeof(T), impName); ProxyFactory fac = session.SessionData.Get("proxyfactory") as ProxyFactory; proxy.RpcInvoke = fac.GetInvoke(session.SessionId); return(proxy); }
public static async Task <int> RemoteAddPKRankItem(int serverId, TRankPKItem item) { if (RunServerId.Value < 0) { return(-1); } if (item == null) { return(-1); } if (item.UserId == 0) { return(-1); } return(await RpcProxy.RunAsync(typeof(RankCenterService), RunServerId.Value, RpcProxy.BuildArgs(serverId, item), () => { if (PKRankPool == null) { Logger.LogError("PKRankPool == null"); return -1; } //低于允许上榜的最低分 if (item.Score < ConfigConstants.PK_RANKING_MIN) { //从榜单池中移除 var rank = PKRankPool.Remove(item.UserId); //删除数据 TRankPKItem.Cache.TryDelete(item.UserId.ToString()); Logger.LogDebug($"[Fail]ServerId[{serverId}] add PKRankItem UserId[{item.UserId}]Score[{item.Score}]Rank[{rank}] < PK_RANKING_MIN"); } //允许上榜 else { //加入成功, 保存数据 var rank = PKRankPool.Add(item); if (rank > 0) { TRankPKItem.Cache.AddOrUpdate(item); } Logger.LogDebug($"[Success]ServerId[{serverId}] add PKRankItem UserId[{item.UserId}]Score[{item.Score}]Rank[{rank}]"); } return GetPKMinScore(); } )); }
public static async Task <int> RemoteGetCOOPMinRound(int serverId) { if (RunServerId.Value < 0) { return(ConfigConstants.COOP_RANKING_MIN); } if (serverId < 0) { return(ConfigConstants.COOP_RANKING_MIN); } return(await RpcProxy.RunAsync(typeof(RankCenterService), RunServerId.Value, RpcProxy.BuildArgs(serverId), () => { var round = GetCOOPMinRound(); Logger.LogDebug($"[Success]ServerId[{serverId}] get COOPMinRound[{round}]"); return round; } )); }
public static async Task <int> RemoteGetPKMinScore(int serverId) { if (RunServerId.Value < 0) { return(ConfigConstants.PK_RANKING_MIN); } if (serverId < 0) { return(ConfigConstants.PK_RANKING_MIN); } return(await RpcProxy.RunAsync(typeof(RankCenterService), RunServerId.Value, RpcProxy.BuildArgs(serverId), () => { var score = GetPKMinScore(); Logger.LogDebug($"[Success]ServerId[{serverId}] get PKMinScore[{score}]"); return score; } )); }
public static async Task <ResultWithError <RoomPrepareInfo> > M2R_PrepareRoom(int serverId, int roomId, List <MatchUser> users, bool isManual = false) { return(await RpcProxy.RunAsync(typeof(RoomService), serverId, RpcProxy.BuildArgs(serverId, roomId, users, isManual), async() => await INSTANCE.PostAsync(() => INSTANCE.PrepareRoom(roomId, users, isManual)) )); }
/// <summary>Wire up proxy.</summary> public AirSimProxy() { _proxy = new RpcProxy(); }
protected virtual T CreateClient <T>() { return(RpcProxy.Create <T, ClientDispatchProxy>()); }
public static async Task ReportBattleException(int serverId, int attackerId, int version, string battleEnterDataOfLua) { await RpcProxy.RunAsync(typeof(BattleService), 10000, RpcProxy.BuildArgs(serverId, attackerId, version, battleEnterDataOfLua), () => { }); }
public ClientSocket(NetworkConfig networkConfig, UpdateRunner updateRunner) { if (networkConfig == null) { throw new ArgumentNullException(nameof(networkConfig)); } if (updateRunner == null) { throw new ArgumentNullException(nameof(updateRunner)); } updateRunner.Add(this); _socket = new UdpSocket(sizeof(ulong), networkConfig); _socket.HandleConnect += (socketService, rs) => { if (rs == null) { Logger.Warn("连接失败!"); Enqueue(() => { Connected?.Invoke(null, null); }); return; } var length = rs.ShiftRight <ushort>(); var data = rs.ShiftRight(length); var bytes = new byte[data.Count]; Buffer.BlockCopy(data.Buffer, data.Offset, bytes, 0, bytes.Length); var buffer = Ssui.Security.DecryptAesKey(bytes, _keys); var aesKey = buffer.toString(); Logger.Debug(aesKey); var peer = new Peer(_socket, aesKey); socketService.HandleDisconnect += () => { Enqueue(() => { Disconnected?.Invoke(peer); }); }; socketService.HandleRead += (readStream) => { // remote var remoteMessageId = readStream.ShiftRight <ulong>(); var newReadStream = readStream.Clone(); Ssui.Security.DecryptAES(newReadStream, aesKey); Enqueue(() => { RpcProxy.Invoke(remoteMessageId, newReadStream, peer); newReadStream.Dispose(); }); }; socketService.HandleAcknowledge += (state, readStream) => { var newReadStream = readStream.Clone(); var localMessageId = readStream.ShiftRight <ulong>(); Enqueue(() => { peer.Acknowledge(state, localMessageId); newReadStream.Dispose(); }); }; socketService.HandleError += error => Enqueue(() => Logger.Error(error)); socketService.HandleWrite += (isSuccess) => { Enqueue(() => { if (!isSuccess) { Logger.Warn("发送失败!"); } }); }; var newRs = rs.Clone(); Enqueue(() => { peer.SetStatus(ConnectionStatus.Connected, socketService); Connected?.Invoke(peer, newRs); newRs.Dispose(); }); }; }