static void Main() { Console.WriteLine("Press enter to connect to the server..."); Console.ReadLine(); //Wait user to press enter //Create a client object to connect a server on 127.0.0.1 (local) IP and listens 10085 TCP port using (var client = ScsClientFactory.CreateClient(new ScsTcpEndPoint("127.0.0.1", 10085))) { //Create a RequestReplyMessenger that uses the client as internal messenger. using (var requestReplyMessenger = new RequestReplyMessenger<IScsClient>(client)) { requestReplyMessenger.Start(); //Start request/reply messenger client.Connect(); //Connect to the server Console.Write("Write some message to be sent to server: "); var messageText = Console.ReadLine(); //Get a message from user //Send user message to the server and get response var response = requestReplyMessenger.SendMessageAndWaitForResponse(new ScsTextMessage(messageText)); Console.WriteLine("Response to message: " + ((ScsTextMessage) response).Text); Console.WriteLine("Press enter to disconnect from server..."); Console.ReadLine(); //Wait user to press enter } } }
/// <summary> /// Overrides message calls and translates them to messages to remote application. /// </summary> /// <param name="msg">Method invoke message (from RealProxy base class)</param> /// <returns>Method invoke return message (to RealProxy base class)</returns> public override IMessage Invoke(IMessage msg) { IMethodCallMessage message = msg as IMethodCallMessage; if (message == null) { return(null); } ScsRemoteInvokeMessage requestMessage = new ScsRemoteInvokeMessage { ServiceClassName = typeof(TProxy).Name, MethodName = message.MethodName, Parameters = message.InArgs }; ScsRemoteInvokeReturnMessage responseMessage = _clientMessenger.SendMessageAndWaitForResponse(requestMessage, 10) as ScsRemoteInvokeReturnMessage; if (responseMessage == null) { return(null); } return(responseMessage.RemoteException != null ? new ReturnMessage(responseMessage.RemoteException, message) : new ReturnMessage(responseMessage.ReturnValue, null, 0, message.LogicalCallContext, message)); }
static void Main() { Console.WriteLine("Press enter to connect to the server..."); Console.ReadLine(); //Wait user to press enter //Create a client object to connect a server on 127.0.0.1 (local) IP and listens 10085 TCP port using (var client = ScsClientFactory.CreateClient(new ScsTcpEndPoint("127.0.0.1", 10085))) { //Create a RequestReplyMessenger that uses the client as internal messenger. using (var requestReplyMessenger = new RequestReplyMessenger <IScsClient>(client)) { requestReplyMessenger.Start(); //Start request/reply messenger client.Connect(); //Connect to the server Console.Write("Write some message to be sent to server: "); var messageText = Console.ReadLine(); //Get a message from user //Send user message to the server and get response var response = requestReplyMessenger.SendMessageAndWaitForResponse(new ScsTextMessage(messageText)); Console.WriteLine("Response to message: " + ((ScsTextMessage)response).Text); Console.WriteLine("Press enter to disconnect from server..."); Console.ReadLine(); //Wait user to press enter } } }
static void Main() { var serverPublicKey = new X509Certificate2(@"C:\Users\node\Desktop\scs\SSLSamples\CertificateFiles\Server\publicKey.cer"); Console.WriteLine("Press enter to connect to the server..."); Console.ReadLine(); //Wait user to press enter //Create a client object to connect a server on 127.0.0.1 (local) IP and listens 10085 TCP port // using (var client = ScsClientFactory.CreateClient(new ScsTcpEndPoint("127.0.0.1", 10085))) using (var client = SslScsClientFactory.CreateSslClient(new ScsTcpEndPoint("127.0.0.1", 10085) , serverPublicKey , "127.0.0.1")) { //Create a RequestReplyMessenger that uses the client as internal messenger. using (var requestReplyMessenger = new RequestReplyMessenger <IScsClient>(client)) { requestReplyMessenger.Start(); //Start request/reply messenger client.Connect(); //Connect to the server Console.Write("Write some message to be sent to server: "); var messageText = Console.ReadLine(); //Get a message from user //Send user message to the server and get response var response = requestReplyMessenger.SendMessageAndWaitForResponse(new ScsTextMessage(messageText)); Console.WriteLine("Response to message: " + ((ScsTextMessage)response).Text); Console.WriteLine("Press enter to disconnect from server..."); Console.ReadLine(); //Wait user to press enter } } }
/// <summary> /// 将方法调用转换为远程方法调用 /// </summary> /// <param name="method">代理方法</param> /// <param name="parameters">代理参数</param> /// <returns>方法返回结果</returns> public virtual object Intercept(MethodInfo method, object[] parameters) { if (method == null) { return(null); } var requestMessage = new ScsRemoteInvokeMessage { ServiceClassName = typeof(TProxy).Name, MethodName = method.Name, Parameters = parameters }; var responseMessage = _clientMessenger.SendMessageAndWaitForResponse(requestMessage) as ScsRemoteInvokeReturnMessage; if (responseMessage == null) { return(null); } return(responseMessage.RemoteException == null ? responseMessage.ReturnValue : throw responseMessage.RemoteException); }
/// <summary> /// ´Send a text command to a static node and get a response /// </summary> /// <param name="command">the command in sql like syntax</param> /// <returns>The Repsonse of the command</returns> public byte[] SendCommandAndWaitForResponse(byte[] command) { //Create a RequestReplyMessenger that uses the client as internal messenger. using (var requestReplyMessenger = new RequestReplyMessenger<IScsClient>(_client)) { requestReplyMessenger.Start(); //Start request/reply messenger //_client.Connect(); //Connect to the server //Send message to the server and get response return ((ScsRawDataMessage)requestReplyMessenger.SendMessageAndWaitForResponse(new ScsRawDataMessage(command))).MessageData; } }
/// <summary> /// Overrides message calls and translates them to messages to remote application. /// </summary> /// <param name="msg">Method invoke message (from RealProxy base class)</param> /// <returns>Method invoke return message (to RealProxy base class)</returns> public override IMessage Invoke(IMessage msg) { var message = msg as IMethodCallMessage; if (message == null) { return(null); } var requestMessage = new ScsRemoteInvokeMessage { ServiceClassName = typeof(TProxy).Name, MethodName = message.MethodName, //Parameters = message.InArgs Parameters = message.Args }; var responseMessage = _clientMessenger.SendMessageAndWaitForResponse(requestMessage) as ScsRemoteInvokeReturnMessage; if (responseMessage == null) { return(null); } /* * http://stackoverflow.com/questions/3081341/ref-argument-through-a-realproxy * The second parameter of ReturnMessage needs to contain the values of the ref and out parameters to pass back. You can get them by saving a reference to the array you pass in: * * public override System.Runtime.Remoting.Messaging.IMessage Invoke(System.Runtime.Remoting.Messaging.IMessage msg) * { * IMethodCallMessage call = msg as IMethodCallMessage; * var args = call.Args; * object returnValue = typeof(HelloClass).InvokeMember(call.MethodName, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, _hello, args); * return new ReturnMessage(returnValue, args, args.Length, call.LogicalCallContext, call); * } */ object[] args = null; int largo = 0; if (responseMessage.Parameters != null) { args = responseMessage.Parameters; largo = args.Length; } return(responseMessage.RemoteException != null ? new ReturnMessage(responseMessage.RemoteException, message) : new ReturnMessage(responseMessage.ReturnValue, args, largo, message.LogicalCallContext, message)); }
/// <summary> /// Send a Message and Wati for Reply /// </summary> /// <typeparam name="T">The expected Value</typeparam> /// <param name="msg">the message to send</param> /// <returns></returns> public static Task<IScsMessage> SendMessageWithReplyAsync(this IScsClient client, IScsMessage msg) { TaskCompletionSource<IScsMessage> tcs = new TaskCompletionSource<IScsMessage>(); using (var requestReplyMessenger = new RequestReplyMessenger<IScsClient>(client)) { requestReplyMessenger.Start(); //Start request/reply messenger //Send user message to the server and get response try { var response = requestReplyMessenger.SendMessageAndWaitForResponse(msg); tcs.SetResult(response); } catch (Exception ex) { tcs.SetException(ex); } } return tcs.Task; }
/// <summary> /// Отправка сообщения по сети и ожидание ответа /// </summary> public static string SendActionAndWaitForResponse(string ip, int port, int?operationId, string actionName, int connectionTimeout = 3000, int responseTimeOut = 5000) { string result = null; string message = $"{operationId},{actionName},1"; //Create a client object to connect a server using (var client = ScsClientFactory.CreateClient(new ScsTcpEndPoint(ip, port))) { //Create a RequestReplyMessenger that uses the client as internal messenger. using (var requestReplyMessenger = new RequestReplyMessenger <IScsClient>(client)) { requestReplyMessenger.Start(); //Start request/reply messenger client.ConnectTimeout = connectionTimeout; client.Connect(); //Connect to the server Thread.Sleep(700); //Send user message to the server and get response var response = requestReplyMessenger.SendMessageAndWaitForResponse(new ScsTextMessage(message), responseTimeOut); result = ((ScsTextMessage)response).Text; } } return(result); }