/// <summary> /// Executes the specified pk. /// </summary> /// <param name="pk">The pk.</param> /// <returns></returns> public byte[] Execute(NetworkInvokePackage pk) { switch (pk.InvokeType) { // 注册会话连接 case NetworkInvokeType.Register: string UserName = (string)pk.Context[PackageUtility.SESSION_USERNAME]; string Password = (string)pk.Context[PackageUtility.SESSION_PASSWORD]; string IpAddress = (string)pk.Context[PackageUtility.SESSION_IPADDRESS]; string EncryptKey = (string)pk.Context[PackageUtility.SESSION_ENCTRYPTKEY]; RegisterSession(pk.SessionId, UserName, Password, IpAddress, EncryptKey); // 注册会话 return(null); // 获取远程服务 case NetworkInvokeType.RemoteService: return(GetRemoteServices(pk)); // 调用远程方法 case NetworkInvokeType.Invoke: return(InvokeCommand(pk)); default: throw new ArgumentException("无法识别的远程调用数据包格式。"); } }
/// <summary> /// Gets the remote interface catalog. /// </summary> /// <returns></returns> public static List <ServiceInfo> GetRemoteInterfaceCatalog() { NetworkInvokePackage pk = GetPackage(NetworkInvokeType.RemoteService); byte[] buf = Communicator.Invoke(serializer.Serialize <NetworkInvokePackage>(pk)); return(serializer.Deserialize <List <ServiceInfo> >(buf)); }
private static bool Ping(NetworkInvokePackage ping) { if (DefaultContainer.SystemReady) { Gateway.ActiviteSessioin(ping.SessionId); return true; } else { return false; } }
/// <summary> /// Invokes the command. /// </summary> /// <param name="method">The method.</param> /// <param name="parameter">The parameter.</param> /// <returns></returns> public static object InvokeCommand(MethodInfo method, object parameter) { //ClientEventDispatcher.Instance.Logger.Debug("提交请求[" + method.Name + "], SessionId 为 [" + sessionId + "], 用户 [" + username + "] "); NetworkInvokePackage package = GetPackage(NetworkInvokeType.Invoke); PackageUtility.EncodeInvoke(method, parameter, encryptKey, ref package); byte[] result = Communicator.Invoke(serializer.Serialize <NetworkInvokePackage>(package)); // 解密方法返回结果 return(serializer.Deserialize <object>(SecurityUtility.DESDecrypt(result, encryptKey))); }
private static bool Ping(NetworkInvokePackage ping) { if (DefaultContainer.SystemReady) { Gateway.ActiviteSessioin(ping.SessionId); return(true); } else { return(false); } }
/// <summary> /// Ping服务器 /// </summary> /// <returns>如果连得通返回true,否则为false</returns> public static bool Ping() { NetworkInvokePackage package = GetPackage(NetworkInvokeType.Ping); try { byte[] result = Communicator.Invoke(serializer.Serialize <NetworkInvokePackage>(package)); serverReady = serializer.Deserialize <bool>(result); } catch { serverReady = false; } return(serverReady); }
private static bool WcfChannelCanUse() { WcfChannel channel = CreateWcfChannel(); NetworkInvokePackage pk = new NetworkInvokePackage(NetworkInvokeType.Ping, Guid.NewGuid().ToString()); try { Serializer serializer = new Serializer(); byte[] data = channel.Invoke(serializer.Serialize<NetworkInvokePackage>(pk)); return serializer.Deserialize<bool>(data); } catch { return false; } }
private static bool WcfChannelCanUse() { WcfChannel channel = CreateWcfChannel(); NetworkInvokePackage pk = new NetworkInvokePackage(NetworkInvokeType.Ping, Guid.NewGuid().ToString()); try { Serializer serializer = new Serializer(); byte[] data = channel.Invoke(serializer.Serialize <NetworkInvokePackage>(pk)); return(serializer.Deserialize <bool>(data)); } catch { return(false); } }
private byte[] GetRemoteServices(NetworkInvokePackage pk) { try { ISystemService systemManager = kernel[typeof(ISystemService)] as ISystemService; List <ServiceInfo> subsystems = systemManager.GetServices(pk.SessionId, pk.ClientType); byte[] data = dataProcessor.Serialize <List <ServiceInfo> >(subsystems); return(data); } catch (Exception ex) { logger.Debug("检查远程服务失败:" + ex.Message); return(serializer.Serialize <List <ServiceInfo> >(new List <ServiceInfo>())); } }
/// <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)); }
public void NetWorkPackageTest() { NetworkInvokePackage pk1 = new NetworkInvokePackage(NetworkInvokeType.Invoke, "123"); pk1.Context[PackageUtility.SESSION_USERNAME] = "Jacky"; pk1.Context[PackageUtility.SESSION_PASSWORD] = "12345"; pk1.Context[PackageUtility.SESSION_IPADDRESS] = "127.0.0.1"; byte[] buffer = serializer.Serialize<NetworkInvokePackage>(pk1); Assert.AreEqual(true, buffer.Length > 0); Console.WriteLine(buffer.Length); NetworkInvokePackage pk2 = serializer.Deserialize<NetworkInvokePackage>(buffer); Assert.IsNotNull(pk2); Assert.AreEqual("123", pk2.SessionId); Assert.AreEqual("Jacky", (string)pk2.Context[PackageUtility.SESSION_USERNAME]); Assert.AreEqual("12345", (string)pk2.Context[PackageUtility.SESSION_PASSWORD]); }
public void NetWorkPackageTest() { NetworkInvokePackage pk1 = new NetworkInvokePackage(NetworkInvokeType.Invoke, "123"); pk1.Context[PackageUtility.SESSION_USERNAME] = "Jacky"; pk1.Context[PackageUtility.SESSION_PASSWORD] = "12345"; pk1.Context[PackageUtility.SESSION_IPADDRESS] = "127.0.0.1"; byte[] buffer = serializer.Serialize <NetworkInvokePackage>(pk1); Assert.AreEqual(true, buffer.Length > 0); Console.WriteLine(buffer.Length); NetworkInvokePackage pk2 = serializer.Deserialize <NetworkInvokePackage>(buffer); Assert.IsNotNull(pk2); Assert.AreEqual("123", pk2.SessionId); Assert.AreEqual("Jacky", (string)pk2.Context[PackageUtility.SESSION_USERNAME]); Assert.AreEqual("12345", (string)pk2.Context[PackageUtility.SESSION_PASSWORD]); }
public void NetworkPackageTest() { NetworkInvokePackage pk1 = new NetworkInvokePackage(NetworkInvokeType.Invoke, "123"); pk1.Context[PackageUtility.SESSION_USERNAME] = "Jacky"; pk1.Context[PackageUtility.SESSION_PASSWORD] = "12345"; using (MemoryStream stream = new MemoryStream()) { bf.Serialize(stream, pk1); byte[] buffer = stream.ToArray(); Console.WriteLine("oraginal buffer length : " + buffer.Length); byte[] buf1 = compressor.Compress(buffer); Console.WriteLine("compress buffer length : " + buf1.Length); byte[] buf2 = compressor.Decompress(buf1); Assert.AreEqual(buffer.Length, buf2.Length); Console.WriteLine("decompress buffer length : " + buf2.Length); } }
/// <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; } }
/// <summary> /// 调用服务端的相应方法 /// </summary> /// <param name="data">客户端发过来的调用包</param> /// <returns>序列化后的数据流</returns> /// # 针对XmlSerializer不能直接将子类反序列化为基类的情况进行了修改,如果采用BinaryFormatter则不存在此问题。 /// # 由于采用Xml序列化不能将客户端与应用服务器相连,所以用回bf 2007-11-07 by Jacky public static byte[] Invoke(byte[] data) { try { NetworkInvokePackage package = serializer.Deserialize <NetworkInvokePackage>(data); if (package.InvokeType == NetworkInvokeType.Ping) { return(serializer.Serialize <bool>(Ping(package))); } else { return(Gateway.Execute(package)); } } catch (Exception ex) { if (logger == null) { logger = DefaultHttpApplication.LoggerFactory.CreateLogger <CommonService>("Framework"); } logger.Error("调用服务发生错误", ex); throw ExceptionHelper.WrapException(ex); } }
/// <summary> /// Executes the specified pk. /// </summary> /// <param name="pk">The pk.</param> /// <returns></returns> public byte[] Execute(NetworkInvokePackage pk) { switch (pk.InvokeType) { // ע��Ự���� case NetworkInvokeType.Register : string UserName = (string)pk.Context[PackageUtility.SESSION_USERNAME]; string Password = (string)pk.Context[PackageUtility.SESSION_PASSWORD]; string IpAddress = (string)pk.Context[PackageUtility.SESSION_IPADDRESS]; string EncryptKey = (string)pk.Context[PackageUtility.SESSION_ENCTRYPTKEY]; RegisterSession(pk.SessionId, UserName, Password, IpAddress, EncryptKey); // ע��Ự return null; // ��ȡԶ�̷��� case NetworkInvokeType.RemoteService : return GetRemoteServices(pk); // ����Զ�̷��� case NetworkInvokeType.Invoke : return InvokeCommand(pk); default : throw new ArgumentException("��ʶ���Զ�̵������ݰ���ʽ��"); } }
/// <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; } }
private byte[] GetRemoteServices(NetworkInvokePackage pk) { try { ISystemService systemManager = kernel[typeof(ISystemService)] as ISystemService; List<ServiceInfo> subsystems = systemManager.GetServices(pk.SessionId, pk.ClientType); byte[] data = dataProcessor.Serialize<List<ServiceInfo>>(subsystems); return data; } catch (Exception ex) { logger.Debug("���Զ�̷���ʧ�ܣ�" + ex.Message); return serializer.Serialize<List<ServiceInfo>>(new List<ServiceInfo>()); } }