private async void OnReceive(byte[] obj) { //check the nature of the message: if (obj.Length == 12) { if (obj.Compare(Globals.AuthSelf)) { RPCPayload authLoad = new RPCPayload { MethodName = Globals.AuthResponsePayload, AccessToken = AccessToken.Value, }; await SendPayloadAsync(authLoad).ConfigureAwait(false); return; } } //convert to string: string json = obj.GetString <UTF8Encoding>(); RPCPayload payload = Deserialize <RPCPayload>(json); PayloadReceived(payload); }
private void OnReceived(string connectionId, byte[] data) { //Check if it's ping: string json = data.GetString <UTF8Encoding>(); RPCPayload payload = Deserialize <RPCPayload>(json); PayloadReceivedAsync(connectionId, payload); }
private async Task DisconnectClientAsync(string connectionId, string reason) { //create the special payload: RPCPayload payload = new RPCPayload { Parameters = new List <string> { reason }, MethodName = Globals.CloseConnection }; await SendPayloadAsync(connectionId, payload).ConfigureAwait(false); await _mServer.DisconnectClientAsync(connectionId); }
public async Task BeamAsync(string methodName, object[] args, Type[] types) { RPCPayload payload = new RPCPayload { Parameters = new List <string>(), MethodName = methodName }; for (int i = 0; i < args.Length; i++) { payload.Parameters.Add(Serialize(args[i], types[i])); } byte[] data = Serialize(payload).GetBytes <UTF8Encoding>(); await _mServer.SendAllAsync(data).ConfigureAwait(false); }
public async Task BeamAsync(string methodName, object[] args, Type[] types) { RPCPayload payload = new RPCPayload { MethodName = methodName, Parameters = new List <string>() }; if (args != null) { for (int i = 0; i < args.Length; i++) { payload.Parameters.Add(Serialize(args[i], types[i])); } } await SendPayloadAsync(payload); }
internal async Task BeamClientAsync(string connectionId, string methodName, object[] args, Type[] types) { if (!_mServer.HasClient(connectionId)) { return; } RPCPayload payload = new RPCPayload { Parameters = new List <string>(), MethodName = methodName }; if (args != null) { for (int i = 0; i < args.Length; i++) { payload.Parameters.Add(Serialize(args[i], types[i])); } } await SendPayloadAsync(connectionId, payload).ConfigureAwait(false); }
private async void PayloadReceived(RPCPayload payload) { //See if the payload wants us to close the connection if (payload.MethodName == Globals.CloseConnection) { Exception ex = null; if (payload.Parameters.Count > 0) { ex = new OperationCanceledException(payload.Parameters[0]); } await _mClient.DisconnectAsync(ex).ConfigureAwait(false); return; } if (!_subs.ContainsKey(payload.MethodName)) { return; } InvocationHandler handler = _subs[payload.MethodName]; if (handler.Types == null || handler.Types.Count == 0) { handler.Invoke(null); } else { List <object> parameters = new List <object>(); for (int i = 0; i < payload.Parameters.Count; i++) { parameters.Add(Deserialize(payload.Parameters[i], handler.Types[i])); } handler.Invoke(parameters.ToArray()); } }
private async void PayloadReceivedAsync(string connectionId, RPCPayload payload) { //check if the method is an authentication payload if (payload.MethodName == Globals.AuthResponsePayload) { ClientProvidedAuthentication?.Invoke(connectionId, payload.AccessToken); return; } if (_methodCache == null) { ConstructMethodCache(); } if (!_methodCache.ContainsKey(payload.MethodName)) { return; } //if we require auth send CacheItem item = _methodCache[payload.MethodName]; List <object> parameters = new List <object>(); //first, validate the access token: if (item.Authorize) { ClaimsPrincipal principal = await ValidateAccessToken(payload.AccessToken, item.Scopes).ConfigureAwait(false); if (principal == null) { return; } string userId = principal.FindFirst(ClaimTypes.NameIdentifier)?.Value; //If we require authentication, ensure that is done first: if (_requiresAuth && User(userId) == null) { await DisconnectClientAsync(connectionId, "NOT_AUTH").ConfigureAwait(false); return; } //Add the user if not exists, only in non-auth mode. if (!string.IsNullOrEmpty(userId) && !_requiresAuth) { AddUserConnectionId(connectionId, userId); } //Inject the principal if so required if (item.InjectPrincipal) { MercuryPrincipal mPrincipal = new MercuryPrincipal(connectionId, this, principal); parameters.Add(mPrincipal); } } if (payload.Parameters.Count > 0) { for (int i = 0; i < payload.Parameters.Count; i++) { parameters.Add(Deserialize(payload.Parameters[i], item.Types[i])); } } _ = parameters.Count == 0 ? item.MethodInfo.Invoke(this, null) : item.MethodInfo.Invoke(this, parameters.ToArray()); }
private async Task SendPayloadAsync(string connectionId, RPCPayload payload) { await _mServer.SendAsync(connectionId, Serialize(payload).GetBytes <UTF8Encoding>()) .ConfigureAwait(false); }
private async Task SendPayloadAsync(RPCPayload payload) { byte[] bytes = Serialize(payload).GetBytes <UTF8Encoding>(); await _mClient.SendAsync(bytes).ConfigureAwait(false); }