/// <summary> /// Encodes the invoke. /// </summary> /// <param name="method">The method.</param> /// <param name="parameter">The parameter.</param> /// <param name="encryptKey">The encrypt key.</param> /// <param name="package">The package.</param> public static void EncodeInvoke(MethodInfo method, object parameters, string encryptKey, ref NetworkInvokePackage package) { package.Context[METHOD] = serializer.Serialize <MethodInfo>(method); package.Context[METHOD_NAME] = serializer.Serialize <string>(method.Name); package.Context[METHOD_ISGENERIC] = method.IsGenericMethod; package.Context[METHOD_PARAMETERS] = SecurityUtility.DESEncrypt(serializer.Serialize <object>(parameters), encryptKey); package.Context[METHOD_INVOKEINFO] = SecurityUtility.DESEncrypt(serializer.Serialize <MethodInvokeInfo>(new MethodInvokeInfo(method)), encryptKey); }
/// <summary> /// Registers the session. /// </summary> public static void RegisterSession() { IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName()); string ip = host.AddressList[0].ToString(); string un = SecurityUtility.DESEncrypt(username, encryptKey); string pw = SecurityUtility.DESEncrypt(password, encryptKey); // 编码会话包 NetworkInvokePackage pk = GetPackage(NetworkInvokeType.Register); pk.Context[PackageUtility.SESSION_USERNAME] = un; pk.Context[PackageUtility.SESSION_PASSWORD] = pw; pk.Context[PackageUtility.SESSION_IPADDRESS] = ip; pk.Context[PackageUtility.SESSION_ENCTRYPTKEY] = encryptKey; Communicator.Invoke(serializer.Serialize <NetworkInvokePackage>(pk)); }
/// <summary> /// 调用服务器端的方法 /// </summary> /// <param name="package">远程调用包</param> /// <returns>经过加密后的方法结果</returns> private byte[] InvokeCommand(NetworkInvokePackage package) { try { ActiviteSessioin(package.SessionId); String encryptKey = GetCryptKey(package.SessionId); MethodInfo method; MethodInvokeInfo invokeInfo; object[] parameters; bool isGenericMethod = (bool)package.Context[PackageUtility.METHOD_ISGENERIC]; // 解码调用包 if (isGenericMethod) { PackageUtility.DecodeInvoke(package, encryptKey, out invokeInfo, out parameters); ISystemService system = (ISystemService)kernel[typeof(ISystemService)]; method = system.GetMethod(invokeInfo); } else { PackageUtility.DecodeInvoke(package, encryptKey, out method, out parameters); } // 调用 logger.Debug(String.Format("接收到会话 [{0}] 的远程调用请求, 调用的方法为 \"{1}\"", package.SessionId, method)); object result = Invoke(method, parameters); // 返回服务器端方法执行结果 if (result == null) { return(null); } SerializaionDataSet(result); byte[] buffer = SecurityUtility.DESEncrypt(dataProcessor.Serialize <object>(result), encryptKey); return(buffer); } catch (Exception ex) { logger.Error(String.Format("调用服务时发生错误, SessionId : {0}", package.SessionId), ex); throw; } }