public override StreamInfo RegisterFileToDownload(NetworkStream stream, CompressMode compressMode, ClientInfo client, bool isWebSocket) { var bytes = GoStreamReader.ReadBlockToEnd(stream, compressMode, ProviderSetting.MaximumReceiveDataBlock, isWebSocket); var json = Encoding.UTF8.GetString(bytes); MethodCallInfo callInfo = ServerSerializationHelper.Deserialize <MethodCallInfo>(json); MethodCallbackInfo callback = new MethodCallbackInfo(); callback.Guid = callInfo.Guid; var serviceType = RegisteredServiceTypes[callInfo.ServiceName]; var sessionId = callInfo.Data.ToString(); var clientInfo = (from x in Services.ToArray() where x.Key.SessionId == sessionId select x.Key).FirstOrDefault(); if (clientInfo == null) { throw new Exception("RegisterFile client not found!"); } var service = FindClientServiceByType(clientInfo, serviceType); #if (NETSTANDARD1_6 || NETCOREAPP1_1) var method = serviceType.GetTypeInfo().GetMethod(callInfo.MethodName, RuntimeTypeHelper.GetMethodTypes(serviceType, callInfo).ToArray()); #else var method = serviceType.GetMethod(callInfo.MethodName, RuntimeTypeHelper.GetMethodTypes(serviceType, callInfo).ToArray()); #endif List <object> parameters = new List <object>(); int index = 0; var prms = method.GetParameters(); foreach (var item in callInfo.Parameters) { parameters.Add(ServerSerializationHelper.Deserialize(item.Value, prms[index].ParameterType)); index++; } if (method.ReturnType != typeof(StreamInfo)) { throw new Exception("return type for upload must StreamInfo!"); } else { StreamInfo data = null; data = (StreamInfo)method.Invoke(service, parameters.ToArray()); if (data == null) { throw new Exception($"StreamInfo cannot be null"); } var streamReader = data.Stream; data.Stream = null; callback.Data = ServerSerializationHelper.SerializeObject(data, this); SendCallbackData(callback, client); data.Stream = streamReader; return(data); } }
/// <summary> /// call a method of client from server /// </summary> /// <param name="callInfo">method call data</param> internal void CallMethod(MethodCallInfo callInfo) { MethodCallbackInfo callback = new MethodCallbackInfo() { Guid = callInfo.Guid }; try { var service = Callbacks[callInfo.ServiceName].Value; var method = service.GetType().GetMethod(callInfo.MethodName, RuntimeTypeHelper.GetMethodTypes(service.GetType(), callInfo).ToArray()); if (method == null) { throw new Exception($"Method {callInfo.MethodName} from service {callInfo.ServiceName} not found! serviceType: {service.GetType().FullName}"); } List <object> parameters = new List <object>(); int index = 0; foreach (var item in method.GetParameters()) { parameters.Add(JsonConvert.DeserializeObject(callInfo.Parameters[index].Value, item.ParameterType)); index++; } if (method.ReturnType == typeof(void)) { method.Invoke(service, parameters.ToArray()); } else { var data = method.Invoke(service, parameters.ToArray()); callback.Data = data == null ? null : JsonConvert.SerializeObject(data); } } catch (Exception ex) { AutoLogger.LogError(ex, "ConnectorBase CallMethod"); callback.IsException = true; callback.Data = JsonConvert.SerializeObject(ex.ToString()); } SendCallbackData(callback); }
static object SendData(this ConnectorBase connector, MethodCallInfo callInfo, StreamInfo streamInfo) { var added = WaitedMethodsForResponse.TryAdd(callInfo.Guid, new KeyValue <AutoResetEvent, MethodCallbackInfo>(new AutoResetEvent(false), null)); var service = connector.Services.ContainsKey(callInfo.ServiceName) ? connector.Services[callInfo.ServiceName] : null; var method = service?.GetType().GetMethod(callInfo.MethodName, RuntimeTypeHelper.GetMethodTypes(service.GetType(), callInfo).ToArray()); if (method != null && method.ReturnType == typeof(StreamInfo)) { callInfo.Data = connector.SessionId; StreamInfo stream = connector.RegisterFileStreamToDownload(callInfo); return(stream); } else if (method != null && streamInfo != null && method.ReturnType == typeof(void) && method.GetParameters().Length == 1 && method.GetParameters()[0].ParameterType == typeof(StreamInfo)) { callInfo.Data = connector.SessionId; connector.RegisterFileStreamToUpload(streamInfo, callInfo); return(null); } else { connector.SendData(callInfo); } var seted = WaitedMethodsForResponse[callInfo.Guid].Key.WaitOne(connector.ProviderSetting.SendDataTimeout); if (!seted) { if (connector.SettingInfo != null && connector.SettingInfo.IsDisposeClientWhenTimeout) { connector.Dispose(); } throw new TimeoutException(); } var result = WaitedMethodsForResponse[callInfo.Guid].Value; if (callInfo.MethodName == "/RegisterService") { connector.SessionId = JsonConvert.DeserializeObject <string>(result.Data); result.Data = null; } WaitedMethodsForResponse.Remove(callInfo.Guid); if (result == null) { if (connector.IsDisposed) { throw new Exception("client disconnected"); } return(null); } if (result.IsException) { throw new Exception("server exception:" + JsonConvert.DeserializeObject <string>(result.Data)); } else if (result.IsAccessDenied && result.Data == null) { throw new Exception("server permission denied exception."); } return(result.Data); }