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 } } }
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() { 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> /// Handles MessageReceived events of all clients, evaluates each message, /// finds appropriate service object and invokes appropriate method. /// /// </summary> /// <param name="sender">Source of event</param><param name="e">Event arguments</param> private void Client_MessageReceived(object sender, MessageEventArgs e) { RequestReplyMessenger <IScsServerClient> requestReplyMessenger = (RequestReplyMessenger <IScsServerClient>)sender; ScsRemoteInvokeMessage remoteInvokeMessage = e.Message as ScsRemoteInvokeMessage; if (remoteInvokeMessage == null) { return; } try { IScsServiceClient scsServiceClient = this._serviceClients[requestReplyMessenger.Messenger.ClientId]; if (scsServiceClient == null) { requestReplyMessenger.Messenger.Disconnect(); } else { ScsServiceApplication.ServiceObject serviceObject = this._serviceObjects[remoteInvokeMessage.ServiceClassName]; if (serviceObject == null) { ScsServiceApplication.SendInvokeResponse((IMessenger)requestReplyMessenger, (IScsMessage)remoteInvokeMessage, (object)null, new ScsRemoteException("There is no service with name '" + remoteInvokeMessage.ServiceClassName + "'")); } else { try { serviceObject.Service.CurrentClient = scsServiceClient; object returnValue; try { returnValue = serviceObject.InvokeMethod(remoteInvokeMessage.MethodName, remoteInvokeMessage.Parameters); } finally { serviceObject.Service.CurrentClient = (IScsServiceClient)null; } ScsServiceApplication.SendInvokeResponse((IMessenger)requestReplyMessenger, (IScsMessage)remoteInvokeMessage, returnValue, (ScsRemoteException)null); } catch (TargetInvocationException ex) { Exception innerException = ex.InnerException; ScsServiceApplication.SendInvokeResponse((IMessenger)requestReplyMessenger, (IScsMessage)remoteInvokeMessage, (object)null, new ScsRemoteException(innerException.Message + Environment.NewLine + "Service Version: " + serviceObject.ServiceAttribute.Version, innerException)); } catch (Exception ex) { ScsServiceApplication.SendInvokeResponse((IMessenger)requestReplyMessenger, (IScsMessage)remoteInvokeMessage, (object)null, new ScsRemoteException(ex.Message + Environment.NewLine + "Service Version: " + serviceObject.ServiceAttribute.Version, ex)); } } } } catch (Exception ex) { ScsServiceApplication.SendInvokeResponse((IMessenger)requestReplyMessenger, (IScsMessage)remoteInvokeMessage, (object)null, new ScsRemoteException("An error occured during remote service method call.", ex)); } }
/// <summary> /// Creates a new ScsServiceClient object. /// /// </summary> /// <param name="client">Underlying IScsClient object to communicate with server</param><param name="clientObject">The client object that is used to call method invokes in client side. /// May be null if client has no methods to be invoked by server.</param> public ScsServiceClient(IScsClient client, object clientObject) { this._client = client; this._clientObject = clientObject; this._client.Connected += new EventHandler(this.Client_Connected); this._client.Disconnected += new EventHandler(this.Client_Disconnected); this._requestReplyMessenger = new RequestReplyMessenger <IScsClient>(client); this._requestReplyMessenger.MessageReceived += new EventHandler <MessageEventArgs>(this.RequestReplyMessenger_MessageReceived); this._realServiceProxy = new AutoConnectRemoteInvokeProxy <T, IScsClient>(this._requestReplyMessenger, (IConnectableClient)this); this.ServiceProxy = (T)this._realServiceProxy.GetTransparentProxy(); }
/// <summary> /// Handles ClientConnected event of _scsServer object. /// /// </summary> /// <param name="sender">Source of event</param><param name="e">Event arguments</param> private void ScsServer_ClientConnected(object sender, ServerClientEventArgs e) { RequestReplyMessenger <IScsServerClient> requestReplyMessenger = new RequestReplyMessenger <IScsServerClient>(e.Client); requestReplyMessenger.MessageReceived += new EventHandler <MessageEventArgs>(this.Client_MessageReceived); requestReplyMessenger.Start(); IScsServiceClient serviceClient = ScsServiceClientFactory.CreateServiceClient(e.Client, requestReplyMessenger); this._serviceClients[serviceClient.ClientId] = serviceClient; this.OnClientConnected(serviceClient); }
/// <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> /// Handles ClientConnected event of _scsServer object. /// </summary> /// <param name="sender">Source of event</param> /// <param name="e">Event arguments</param> private void ScsServer_ClientConnected(object sender, ServerClientEventArgs e) { var requestReplyMessenger = new RequestReplyMessenger <IScsServerClient>(e.Client); requestReplyMessenger.MessageReceived += Client_MessageReceived; requestReplyMessenger.Start(); var serviceClient = ScsServiceClientFactory.CreateServiceClient(e.Client, requestReplyMessenger); _serviceClients[serviceClient.ClientId] = serviceClient; OnClientConnected(serviceClient); }
public override void Dispose() { try { if (Messenger != null) { Messenger.Dispose(); Messenger = null; } base.Dispose(); } catch { } }
/// <summary> /// Creates a new ScsServiceClient object. /// </summary> /// <param name="client">Underlying IScsClient object to communicate with server</param> /// <param name="clientObject">The client object that is used to call method invokes in client side. /// May be null if client has no methods to be invoked by server.</param> public ScsServiceClient(IScsClient client, object clientObject) { _client = client; _clientObject = clientObject; _client.Connected += Client_Connected; _client.Disconnected += Client_Disconnected; _requestReplyMessenger = new RequestReplyMessenger <IScsClient>(client); _requestReplyMessenger.MessageReceived += RequestReplyMessenger_MessageReceived; _realServiceProxy = new AutoConnectRemoteInvokeProxy <T, IScsClient>(_requestReplyMessenger, this); ServiceProxy = (T)_realServiceProxy.GetTransparentProxy(); }
/// <summary> /// Sends response to the remote application that invoked a service method. /// /// </summary> /// <param name="requestMessage">Request message</param><param name="returnValue">Return value to send</param><param name="exception">Exception to send</param> private void SendInvokeResponse(IScsMessage requestMessage, object returnValue, ScsRemoteException exception) { try { RequestReplyMessenger <IScsClient> requestReplyMessenger = this._requestReplyMessenger; ScsRemoteInvokeReturnMessage invokeReturnMessage1 = new ScsRemoteInvokeReturnMessage(); invokeReturnMessage1.RepliedMessageId = requestMessage.MessageId; invokeReturnMessage1.ReturnValue = returnValue; invokeReturnMessage1.RemoteException = exception; ScsRemoteInvokeReturnMessage invokeReturnMessage2 = invokeReturnMessage1; requestReplyMessenger.SendMessage((IScsMessage)invokeReturnMessage2); } catch { } }
public static void SendAction(string ip, int port, int?operationId, string actionName, int connectionTimeout = 3000) { string message = $"{operationId},{actionName},0"; //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 requestReplyMessenger.SendMessage(new ScsTextMessage(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); }
public override bool Connect(string remote_ip, int remote_port, int timeout = 10000) { try { this.ServerIp = remote_ip; this.ServerPort = remote_port; EzWireProtocol protocol = new EzWireProtocol(); protocol.DataReceived += protocol_DataReceived;//接收到数据,但不一定是message _client = ScsClientFactory.CreateClient(new ScsTcpEndPoint(remote_ip, remote_port)); _client.WireProtocol = protocol; _client.ConnectTimeout = timeout; _client.Connected += client_Connected; _client.Disconnected += client_Disconnected; Messenger = new RequestReplyMessenger<IScsClient>(_client); Messenger.MessageReceived += messenger_MessageReceived; Messenger.Start(); _client.Connect(); this._remain_life_number = this.DefaultLifeNumber; Helper.start_timer(ref _1s_timer, "1s timer", 1000, _1s_timer_Elapsed); } catch (Exception ex) { EzLogger.GlobalLogger.warning(string.Format("{0}{2}{1}{2}", ex.Message, ex.StackTrace, Environment.NewLine)); Dispose(); return false; } return true; }
public override bool Connect(string remote_ip, int remote_port, int timeout = 10000) { try { this.ServerIp = remote_ip; this.ServerPort = remote_port; EzWireProtocol protocol = new EzWireProtocol(); protocol.DataReceived += protocol_DataReceived;//接收到数据,但不一定是message _client = ScsClientFactory.CreateClient(new ScsTcpEndPoint(remote_ip, remote_port)); _client.WireProtocol = protocol; _client.ConnectTimeout = timeout; _client.Connected += client_Connected; _client.Disconnected += client_Disconnected; Messenger = new RequestReplyMessenger <IScsClient>(_client); Messenger.MessageReceived += messenger_MessageReceived; Messenger.Start(); _client.Connect(); this._remain_life_number = this.DefaultLifeNumber; Helper.start_timer(ref _1s_timer, "1s timer", 1000, _1s_timer_Elapsed); } catch (Exception ex) { EzLogger.GlobalLogger.warning(string.Format("{0}{2}{1}{2}", ex.Message, ex.StackTrace, Environment.NewLine)); Dispose(); return(false); } return(true); }
/// <summary> /// Creates a new service client object that is used in server-side. /// </summary> /// <param name="serverClient">Underlying server client object</param> /// <param name="requestReplyMessenger">RequestReplyMessenger object to send/receive messages over serverClient</param> /// <returns></returns> public static IScsServiceClient CreateServiceClient(IScsServerClient serverClient, RequestReplyMessenger <IScsServerClient> requestReplyMessenger) { return(new ScsServiceClient(serverClient, requestReplyMessenger)); }
/// <summary> /// Creates a new ScsServiceClient object. /// </summary> /// <param name="serverClient">Reference to underlying IScsServerClient object</param> /// <param name="requestReplyMessenger">RequestReplyMessenger to send messages</param> public ScsServiceClient(IScsServerClient serverClient, RequestReplyMessenger <IScsServerClient> requestReplyMessenger) { _serverClient = serverClient; _serverClient.Disconnected += Client_Disconnected; _requestReplyMessenger = requestReplyMessenger; }
/// <summary> /// Handles MessageReceived events of all clients, evaluates each message, finds appropriate /// service object and invokes appropriate method. /// </summary> /// <param name="sender">Source of event</param> /// <param name="e">Event arguments</param> private void Client_MessageReceived(object sender, MessageEventArgs e) { // Get RequestReplyMessenger object (sender of event) to get client RequestReplyMessenger <IScsServerClient> requestReplyMessenger = (RequestReplyMessenger <IScsServerClient>)sender; // Cast message to ScsRemoteInvokeMessage and check it if (!(e.Message is ScsRemoteInvokeMessage invokeMessage)) { return; } try { // Get client object IScsServiceClient client = _serviceClients[requestReplyMessenger.Messenger.ClientId]; if (client == null) { requestReplyMessenger.Messenger.Disconnect(); return; } // Get service object ServiceObject serviceObject = _serviceObjects[invokeMessage.ServiceClassName]; if (serviceObject == null) { SendInvokeResponse(requestReplyMessenger, invokeMessage, null, new ScsRemoteException("There is no service with name '" + invokeMessage.ServiceClassName + "'")); return; } // Invoke method try { // Set client to service, so user service can get client in service method using // CurrentClient property. object returnValue; serviceObject.Service.CurrentClient = client; try { returnValue = serviceObject.InvokeMethod(invokeMessage.MethodName, invokeMessage.Parameters); } finally { // Set CurrentClient as null since method call completed serviceObject.Service.CurrentClient = null; } // Send method invocation return value to the client SendInvokeResponse(requestReplyMessenger, invokeMessage, returnValue, null, invokeMessage.Parameters); } catch (TargetInvocationException ex) { Exception innerEx = ex.InnerException; if (innerEx != null) { SendInvokeResponse(requestReplyMessenger, invokeMessage, null, new ScsRemoteInvocationException(invokeMessage.ServiceClassName, serviceObject.ServiceAttribute?.Version ?? "", invokeMessage.MethodName, innerEx.Message, innerEx)); } } catch (Exception ex) { SendInvokeResponse(requestReplyMessenger, invokeMessage, null, new ScsRemoteInvocationException(invokeMessage.ServiceClassName, serviceObject.ServiceAttribute?.Version ?? "", invokeMessage.MethodName, ex.Message, ex)); } } catch (Exception ex) { SendInvokeResponse(requestReplyMessenger, invokeMessage, null, new ScsRemoteException("An error occured during remote service method call.", ex)); } }
/// <summary> /// Creates a new RemoteInvokeProxy object. /// /// </summary> /// <param name="clientMessenger">Messenger object that is used to send/receive messages</param> public RemoteInvokeProxy(RequestReplyMessenger <TMessenger> clientMessenger) : base(typeof(TProxy)) { this._clientMessenger = clientMessenger; }
/// <summary> /// Creates a new AutoConnectRemoteInvokeProxy object. /// </summary> /// <param name="clientMessenger">Messenger object that is used to send/receive messages</param> /// <param name="client">Reference to the client object that is used to connect/disconnect</param> public AutoConnectRemoteInvokeProxy(RequestReplyMessenger <TMessenger> clientMessenger, IConnectableClient client) : base(clientMessenger) { _client = client; }
/// <summary> /// Creates a new RemoteInvokeProxy object. /// </summary> /// <param name="clientMessenger">Messenger object that is used to send/receive messages</param> public RemoteInvokeProxy(RequestReplyMessenger <TMessenger> clientMessenger) { _clientMessenger = clientMessenger; }
/// <summary> /// Creates a new service client object that is used in server-side. /// </summary> /// <param name="serverClient">Underlying server client object</param> /// <param name="requestReplyMessenger">RequestReplyMessenger object to send/receive messages over serverClient</param> /// <returns></returns> public static IScsServiceClient CreateServiceClient(IScsServerClient serverClient, RequestReplyMessenger<IScsServerClient> requestReplyMessenger) { return new ScsServiceClient(serverClient, requestReplyMessenger); }
/// <summary> /// Creates a new ScsServiceClient object. /// /// </summary> /// <param name="serverClient">Reference to underlying IScsServerClient object</param><param name="requestReplyMessenger">RequestReplyMessenger to send messages</param> public ScsServiceClient(IScsServerClient serverClient, RequestReplyMessenger <IScsServerClient> requestReplyMessenger) { this._serverClient = serverClient; this._serverClient.Disconnected += new EventHandler(this.Client_Disconnected); this._requestReplyMessenger = requestReplyMessenger; }
public RequestReplyTcpClient(bool auto_heart_beat) : base(auto_heart_beat) { this.Messenger = null; }
public ClientSender(IScsClient client, RequestReplyMessenger <IScsClient> clientRequestReply) { _clientUtility = new ConnectionUtility(client, clientRequestReply); }