public void SendUpdate(float x, float y, float r, bool s) { var data = new ServerMethodRequestMessage() { methodName = "Update", arguments = new[] { new UpdateRequestData() { x = x, y = y, r = r, s = s, id = UserId } } }; var requestData = new ServerRequestMessage() { messageType = 0, data = JsonUtility.ToJson(data) }; string message = JsonUtility.ToJson(data);//JsonUtility.ToJson(requestData); Debug.Log("SendUpdate message: " + message); _webSocket.Send(message); }
public override void OnReceiveRequestMessage(ServerRequestMessage message) { switch (message.GetMessageType()) { case ServerMessageType.AVATAR_REQUEST: GameMessageManager.OnAvatarRequestMessageReceived((AvatarRequestMessage)message); break; case ServerMessageType.GAME_AVATAR_REQUEST: GameMessageManager.OnGameAvatarRequestMessageReceived((GameAvatarRequestMessage)message); break; case ServerMessageType.GAME_JOIN_ALLIANCE_REQUEST: GameMessageManager.OnGameJoinAllianceRequestMessageReceived((GameJoinAllianceRequestMessage)message); break; case ServerMessageType.GAME_CREATE_ALLIANCE_INVITATION_REQUEST: GameMessageManager.OnGameCreateAllianceInvitationRequestMessageReceived((GameCreateAllianceInvitationRequestMessage)message); break; case ServerMessageType.LIVE_REPLAY_ADD_SPECTATOR_REQUEST: LiveReplayManager.ReceiveMessage(message); break; } }
private Task <object> InvokeInterfaceMethodAsync(ServerRequestMessage serverRequestMessage) { var invokeInfos = InterfaceCollection.GetInvokeInformation(serverRequestMessage.Method); var instance = invokeInfos.Factory.DynamicInvoke(_harrrContext.GetHubConnection().GetServiceProvider()); return(InvokeMethodInfoAsync(instance, invokeInfos.MethodInfo, serverRequestMessage.Arguments, serverRequestMessage.GenericArguments, serverRequestMessage.CancellationGuid)); }
//public override Task<object> InvokeAsync(Type returnType, string methodName, IEnumerable<object> arguments, // string[] genericArguments, CancellationToken cancellationToken = default) { // var methodInfo = typeof(ServerProxyCreatorHelper).GetMethods() // .WithName(nameof(InvokeAsync)).First(p => p.HasGenericArgumentsLengthOf(1)); // var generic = methodInfo.MakeGenericMethod(returnType); // var parameters = new object[] {methodName, arguments, genericArguments, cancellationToken}; // return InvokeHelper.InvokeMethodAsync(this, generic, new List<Type>() {returnType}, parameters); //} public override async Task <T> InvokeAsync <T>(string methodName, IEnumerable <object> arguments, string[] genericArguments, CancellationToken cancellationToken = default) { var preparedArguments = _methodArgumentPreperator.PrepareArguments(arguments).ToList(); var msg = new ServerRequestMessage(methodName, preparedArguments); if (cancellationToken != CancellationToken.None) { msg.CancellationGuid = Guid.NewGuid(); cancellationToken.Register(() => { #pragma warning disable 4014 _clientContext.CancelToken(msg.CancellationGuid.Value); #pragma warning restore 4014 }); } msg.GenericArguments = genericArguments; using var serviceProviderScope = _clientContext.ServiceProvider.CreateScope(); var hubContextType = typeof(ClientContextDispatcher <>).MakeGenericType(_clientContext.HARRRType); var harrrContext = (IClientContextDispatcher)serviceProviderScope.ServiceProvider.GetRequiredService(hubContextType); if (_httpContext != null) { await harrrContext.ProxyClientAsync(_clientContext.Id, msg, _httpContext); return(default);
private async Task <object> InvokeMethodAsync(ServerRequestMessage serverRequestMessage) { var methodCallInfo = MethodsCollection.GetMethodInformations(serverRequestMessage.Method); var instance = methodCallInfo.Factory.DynamicInvoke(_harrrContext.GetHubConnection().GetServiceProvider()); return(InvokeMethodInfoAsync(instance, methodCallInfo.MethodInfo, serverRequestMessage.Arguments, serverRequestMessage.GenericArguments, serverRequestMessage.CancellationGuid)); }
public static async Task Proxy(this ClientContext clientContext, string method, object[] arguments, HttpContext httpContext) { using var serviceProviderScope = clientContext.ServiceProvider.CreateScope(); var hubContextType = typeof(ClientContextDispatcher <>).MakeGenericType(clientContext.HARRRType); var harrrContext = (IClientContextDispatcher)serviceProviderScope.ServiceProvider.GetRequiredService(hubContextType); var msg = new ServerRequestMessage(method, arguments); await harrrContext.ProxyClientAsync(clientContext.Id, msg, httpContext); }
private Task <object> InvokeAsync(ServerRequestMessage serverRequestMessage) { if (serverRequestMessage.Method.Contains("|")) { return(InvokeInterfaceMethodAsync(serverRequestMessage)); } return(InvokeMethodAsync(serverRequestMessage)); }
public override void OnReceiveRequestMessage(ServerRequestMessage message) { switch (message.GetMessageType()) { case ServerMessageType.BIND_SERVER_SOCKET_REQUEST: ProxyMessageManager.OnBindServerSocketRequestMessageReceived((BindServerSocketRequestMessage)message); break; } }
public async Task InvokeServerMessage(ServerRequestMessage message) { try { message = PrepareServerRequestMessage(message); await InvokeAsync(message); } catch { // ignored } }
public async Task InvokeServerRequest(ServerRequestMessage message) { try { message = PrepareServerRequestMessage(message); var payload = await InvokeAsync(message); await SendResponse(message.Id, payload, null); } catch (Exception e) { await _harrrContext.GetHubConnection().SendCoreAsync(MethodNames.ReplyServerRequest, new object[] { message.Id, null, e.GetBaseException().Message }); } }
public void CancelTokenFromServer(ServerRequestMessage requestMessage) { if (requestMessage.CancellationGuid.HasValue) { if (cancellationTokenSources.TryRemove(requestMessage.CancellationGuid.Value, out var token)) { token.Cancel(); } } }
public static async Task CancelToken(this ClientContext clientContext, Guid tokenReference) { using var serviceProviderScope = clientContext.ServiceProvider.CreateScope(); var hubContextType = typeof(ClientContextDispatcher <>).MakeGenericType(clientContext.HARRRType); var harrrContext = (IClientContextDispatcher)serviceProviderScope.ServiceProvider.GetRequiredService(hubContextType); var msg = new ServerRequestMessage(MethodNames.CancelTokenFromServer, tokenReference); await harrrContext.CancelToken(clientContext.Id, tokenReference); }
public async Task CancelToken(string clientId, Guid id) { try { var msg = new ServerRequestMessage(MethodNames.CancelTokenFromServer); msg.CancellationGuid = id; await SendClientMessageAsync(clientId, MethodNames.CancelTokenFromServer, msg, CancellationToken.None); } catch (Exception e) { Console.WriteLine(e); throw; } }
public async Task <string> Challenge(string clientId) { try { var msg = new ServerRequestMessage(MethodNames.ChallengeAuthentication); var ct = new CancellationTokenSource(TimeSpan.FromSeconds(30)); return(await InvokeClientMessageAsync <string>(clientId, MethodNames.ChallengeAuthentication, msg, ct.Token)); } catch (Exception e) { Console.WriteLine(e); throw; } }
public static async Task <ClientCollectionResult <TResult> > Invoke <TResult>(this ClientContext clientContext, string method, object[] arguments, CancellationToken cancellationToken) { using var serviceProviderScope = clientContext.ServiceProvider.CreateScope(); var hubContextType = typeof(ClientContextDispatcher <>).MakeGenericType(clientContext.HARRRType); var harrrContext = (IClientContextDispatcher)serviceProviderScope.ServiceProvider.GetRequiredService(hubContextType); var msg = new ServerRequestMessage(method, arguments); var res = await harrrContext.InvokeClientAsync <TResult>(clientContext.Id, msg, cancellationToken); return(new ClientCollectionResult <TResult>(clientContext.Id, res)); }
public async Task ChallengeAuthentication(ServerRequestMessage message) { string payload = null; string error = null; try { payload = await _harrrContext.AccessTokenProvider(); } catch (Exception e) { error = e.GetBaseException().Message; } await _harrrContext.GetHubConnection().SendCoreAsync(MethodNames.ReplyServerRequest, new object[] { message.Id, payload, error }); }
public static ServerRequestArgs Create(ServerRequestMessage message, ServerSocket socket, int timeout = 30) { ServerRequestArgs request = new ServerRequestArgs(timeout); long requestId = Interlocked.Increment(ref ServerRequestManager.m_counters); message.RequestId = requestId; if (!ServerRequestManager.m_requests.TryAdd(requestId, request)) { throw new Exception("Unable to add new message"); } ServerMessaging.Send(message, socket); return(request); }
private ServerRequestMessage PrepareServerRequestMessage(ServerRequestMessage message) { switch (_harrrContext.HubProtocolType) { case HubProtocolType.JsonHubProtocol: { var requestJson = JsonSerializer.Serialize(message); message = Json.Converter.ToObject <ServerRequestMessage>(requestJson); break; } case HubProtocolType.MessagePackHubProtocol: { var requestJson = Json.Converter.ToJson(message); message = Json.Converter.ToObject <ServerRequestMessage>(requestJson); break; } } return(message); }
public override void OnReceiveRequestMessage(ServerRequestMessage message) { switch (message.GetMessageType()) { case ServerMessageType.CREATE_ALLIANCE_REQUEST: StreamMessageManager.OnCreateAllianceRequestMessageReceived((CreateAllianceRequestMessage)message); break; case ServerMessageType.ALLIANCE_JOIN_REQUEST: StreamMessageManager.OnAllianceJoinRequestMessageReceived((AllianceJoinRequestMessage)message); break; case ServerMessageType.CREATE_AVATAR_STREAM_REQUEST: StreamMessageManager.OnCreateAvatarStreamRequestMessageReceived((CreateAvatarStreamRequestMessage)message); break; case ServerMessageType.CREATE_REPLAY_STREAM_REQUEST: StreamMessageManager.OnCreateReplayStreamRequestMessageReceived((CreateReplayStreamRequestMessage)message); break; case ServerMessageType.LOAD_REPLAY_STREAM_REQUEST: StreamMessageManager.OnLoadReplayStreamRequestMessageReceived((LoadReplayStreamRequestMessage)message); break; case ServerMessageType.LOAD_AVATAR_STREAM_REQUEST: StreamMessageManager.OnLoadAvatarStreamRequestMessageReceived((LoadAvatarStreamRequestMessage)message); break; case ServerMessageType.LOAD_AVATAR_STREAM_OF_TYPE_REQUEST: StreamMessageManager.OnLoadAvatarStreamOfTypeRequestMessageReceived((LoadAvatarStreamOfTypeRequestMessage)message); break; case ServerMessageType.REQUEST_ALLIANCE_JOIN_REQUEST: StreamMessageManager.OnRequestAllianceJoinRequestMessageReceived((RequestAllianceJoinRequestMessage)message); break; } }
public Task SendClientAsync(string clientId, ServerRequestMessage serverRequestMessage, CancellationToken cancellationToken) { return(SendClientMessageAsync(clientId, MethodNames.InvokeServerMessage, serverRequestMessage, cancellationToken)); }
public Task ProxyClientAsync(string clientId, ServerRequestMessage serverRequestMessage, HttpContext httpContext) { return(ProxyClientMessageAsync(clientId, MethodNames.InvokeServerRequest, serverRequestMessage, httpContext)); }
public Task <TResult> InvokeClientAsync <TResult>(string clientId, ServerRequestMessage serverRequestMessage, CancellationToken cancellationToken) { return(InvokeClientMessageAsync <TResult>(clientId, MethodNames.InvokeServerRequest, serverRequestMessage, cancellationToken)); }
//private TResult Deserialize<TResult>(Stream s) { // using (StreamReader reader = new StreamReader(s)) // using (JsonTextReader jsonReader = new JsonTextReader(reader)) { // JsonSerializer ser = new JsonSerializer(); // return ser.Deserialize<TResult>(jsonReader); // } //} internal async Task SendClientMessageAsync(string clientId, string methodName, ServerRequestMessage serverRequestMessage, CancellationToken cancellationToken) { //var m = ServerRequestManager.AddRequest(serverRequestMessage.Id); await HubContext.Clients.Client(clientId).SendCoreAsync(methodName, new[] { serverRequestMessage }, cancellationToken); }
public static void SendResponse(ServerResponseMessage response, ServerRequestMessage request) { response.RequestId = request.RequestId; ServerMessaging.Send(response, ServerManager.GetSocket(request.SenderType, request.SenderId)); }
public override void OnReceiveRequestMessage(ServerRequestMessage message) { throw new NotSupportedException(); }
public override void OnReceiveRequestMessage(ServerRequestMessage message) { throw new System.NotImplementedException(); }
public abstract void OnReceiveRequestMessage(ServerRequestMessage message);
internal async Task <TResult> InvokeClientMessageAsync <TResult>(string clientId, string methodName, ServerRequestMessage serverRequestMessage, CancellationToken cancellationToken) { var m = ServerRequestManager.AddRequest(serverRequestMessage.Id); await HubContext.Clients.Client(clientId).SendCoreAsync(methodName, new[] { serverRequestMessage }, cancellationToken); await Task.Run(() => { try { Task.WaitAny(new Task[] { m.Task }, cancellationToken); } catch (Exception) { ServerRequestManager.CancelRequest(serverRequestMessage.Id); throw; } }); var jToken = await m.Task; //var z = jToken.ToString(); //var t = jToken.ToObject<TResult>(); return(Json.Converter.ToObject <TResult>(jToken)); }
internal async Task ProxyClientMessageAsync(string clientId, string methodName, ServerRequestMessage serverRequestMessage, HttpContext httpContext) { var m = ServerRequestManager.AddProxyRequest(serverRequestMessage.Id, httpContext); await HubContext.Clients.Client(clientId).SendCoreAsync(methodName, new[] { serverRequestMessage }, httpContext.RequestAborted); await Task.Run(() => { try { Task.WaitAny(new Task[] { m.Task }, httpContext.RequestAborted); } catch (Exception) { ServerRequestManager.CancelRequest(serverRequestMessage.Id); throw; } }); //await m.Task; }
public ServerRequestEventArgs(ServerRequestMessage serverRequestMessage) { ServerRequestMessage = serverRequestMessage; }