private static WcfServiceClient GetWcfClient(RequestSettings setting) { WcfServiceClient wcfClient = WcfServiceClientManager.Current.Get(setting.GameId, setting.ServerId); if (wcfClient == null) { throw new KeyNotFoundException(string.Format("Not found setting for game:{0} server:{1}", setting.GameId, setting.ServerId)); } return wcfClient; }
private static bool DoCall(RequestSettings setting, out byte[] buffer, bool isRoute = false) { buffer = new byte[0]; WcfServiceClient wcfClient = GetWcfClient(setting); if (wcfClient.Connect()) { if (isRoute) { return wcfClient.TryCallRemote(setting.RouteName, setting.ParamString, setting.RemoteAddress, out buffer); } return wcfClient.TryRequest(setting.ParamString, setting.RemoteAddress, out buffer); } return false; }
/// <summary> /// 发送请求 /// </summary> public static bool Request(RequestSettings setting, out byte[] buffer) { return DoCall(setting, out buffer); }
/// <summary> /// /// </summary> public static void ResetChannel(RequestSettings setting) { GetWcfClient(setting).ResetChannel(); }
/// <summary> /// 远端调用 /// </summary> public static bool CallRemote(RequestSettings setting, out byte[] buffer) { return DoCall(setting, out buffer, true); }
/// <summary> /// /// </summary> public static void ResetChannel(RequestSettings setting) { GetWcfClient(setting).ResetChannel(); }
/// <summary> /// 远端调用 /// </summary> public static bool CallRemote(RequestSettings setting, out byte[] buffer) { return(DoCall(setting, out buffer, true)); }
/// <summary> /// 发送请求 /// </summary> public static bool Request(RequestSettings setting, out byte[] buffer) { return(DoCall(setting, out buffer)); }
/// <summary> /// /// </summary> public void Request() { ReadParam(); RequestSettings settings = new RequestSettings(GameId, ServerId, "", ParamString); byte[] sendBuffer = new byte[0]; RequestError error = RequestError.Success; try { ServiceRequest.Request(settings, out sendBuffer); } catch (CommunicationObjectFaultedException fault) { TraceLog.WriteError("The wcfclient request faulted:{0}", fault); error = RequestError.Closed; ServiceRequest.ResetChannel(settings); } catch (Exception ex) { if (ex.InnerException is SocketException) { var sex = ex.InnerException as SocketException; TraceLog.WriteError("The wcfclient request connect:{0}-{1}", sex.SocketErrorCode, sex); if (sex.SocketErrorCode == SocketError.TimedOut) { error = RequestError.Timeout; } else { error = RequestError.UnableConnect; } } else { TraceLog.WriteError("The wcfclient request error:{0}", ex); error = RequestError.Unknown; } ServiceRequest.ResetChannel(settings); } switch (error) { case RequestError.Success: WriteBuffer(sendBuffer); break; case RequestError.Closed: case RequestError.NotFindService: WriteNotFindError(); break; case RequestError.UnableConnect: WriteConnectionError(); break; case RequestError.Timeout: WriteTimeoutError(); break; case RequestError.Unknown: WriteUnknownError(); break; default: throw new ArgumentOutOfRangeException("RequestError", error, "Not process RequestError enum."); } }