public string GetMethodParameterDetial(MethodParameterDetails methodParameterDetails) { AsyncActions.Run(() => { string json = JsonConvert.SerializeObject(methodParameterDetails); byte[] bytes = Encoding.UTF8.GetBytes(json); if (SecuritySettings != null) { bytes = EncryptBytes(bytes); } byte[] len = BitConverter.GetBytes(bytes.Length); List <byte> data = new List <byte> { (byte)DataType.GetMethodParameterDetails, (byte)CompressMode.None }; data.AddRange(len); data.AddRange(bytes); if (data.Count > ProviderSetting.MaximumSendDataBlock) { throw new Exception("SendCallbackData data length is upper than MaximumSendDataBlock"); } GoStreamWriter.WriteToStream(_client.GetStream(), data.ToArray(), IsWebSocket); }); getServiceDetailEvent.WaitOne(); return(getmethodParameterDetailsResult); }
//public object SendData(this ClientDuplex client, string className, string callerName, params object[] args) //{ // MethodCallInfo callInfo = new MethodCallInfo(); // callInfo.FullClassName = className; // callInfo.MethodName = callerName; // foreach (var item in args) // { // callInfo.Parameters.Add(new Shared.Models.ParameterInfo() { Type = item.GetType().FullName, Value = JsonConvert.SerializeObject(item) }); // } // SendData(callInfo); // return null; //} /// <summary> /// send data to call server method /// </summary> /// <param name="Data"></param> internal void SendData(MethodCallInfo Data) { AsyncActions.Run(() => { try { var json = JsonConvert.SerializeObject(Data); List <byte> bytes = new List <byte> { (byte)DataType.CallMethod, (byte)CompressMode.None }; var jsonBytes = Encoding.UTF8.GetBytes(json); if (SecuritySettings != null) { jsonBytes = EncryptBytes(jsonBytes); } byte[] dataLen = BitConverter.GetBytes(jsonBytes.Length); bytes.AddRange(dataLen); bytes.AddRange(jsonBytes); if (bytes.Count > ProviderSetting.MaximumSendDataBlock) { throw new Exception("SendData data length is upper than MaximumSendDataBlock"); } GoStreamWriter.WriteToStream(_client.GetStream(), bytes.ToArray(), IsWebSocket); } catch (Exception ex) { AutoLogger.LogError(ex, "ConnectorBase SendData"); } }); }
public void ConnectToUDP(int port) { IPEndPoint ipep = new IPEndPoint(IPAddress.Any, port); newsock = new UdpClient(ipep); AsyncActions.Run(() => { StartToReadingData(); }); }
void CreateNewEngine(ClientInfo client) { var blocks = UDPClients[client]; AsyncActions.Run(() => { StartEngineToSendData(client, blocks); }, (ex) => { UDPClients.Remove(client); }); }
/// <summary> /// connect to socket /// </summary> /// <param name="ipAddress"></param> /// <param name="port"></param> public void ConnectToUDP(string ipAddress, int port) { isStart = false; socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); iPEndPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port); StartReadingData(); AsyncActions.Run(() => { StartEngineWriter(); }); SendUdpData(new byte[] { 0 }); }
/// <summary> /// connect to socket /// </summary> /// <param name="ipAddress"></param> /// <param name="port"></param> public void ConnectToUDP(string ipAddress, int port) { isStart = false; #if (!PORTABLE) socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp); iPEndPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port); #else socket = new Sockets.Plugin.UdpSocketClient(); _ipAddress = ipAddress; _port = port; #endif StartReadingData(); AsyncActions.Run(() => { StartEngineWriter(); }); SendUdpData(new byte[] { 0 }); }
/// <summary> /// connect to server is background Thread /// </summary> /// <param name="url">url of server to connect</param> /// <param name="connectedAction">call this action after connect successfully</param> /// <param name="isAutoRecconect">if you want system try to reConnect when server or network is not avalable</param> /// <param name="isHoldMethodCallsWhenDisconnected">hold method calls when provider is disconnected and call all after connected</param> /// <param name="isWebsocket">is web socket system</param> //public void ConnectAsync(string url, Action<bool> connectedAction, bool isAutoRecconect, bool isHoldMethodCallsWhenDisconnected, bool isWebsocket = false) //{ // AsyncActions.Run(() => // { // ProviderSetting.AutoReconnect = isAutoRecconect; // ProviderSetting.HoldMethodCallsWhenDisconnected = isHoldMethodCallsWhenDisconnected; // Connect(url, isWebsocket); // connectedAction(true); // HoldThreadResetEvent.Reset(); // HoldThreadResetEvent.WaitOne(); // }, (ex) => // { // Disconnect(); // connectedAction(IsConnected); // HoldThreadResetEvent.Reset(); // HoldThreadResetEvent.WaitOne(); // }); //} /// <summary> /// connect to server is background Thread /// </summary> /// <param name="url">url of server to connect</param> /// <param name="connectedAction">call this action after connect successfully</param> #if (NET35 || NET40) public void ConnectAsyncAutoReconnect(string url, Action <bool> connectedAction) { AsyncActions.Run(() => { ProviderSetting.AutoReconnect = true; try { ConnectAsync(url); connectedAction(true); AutoReconnectWaitToDisconnectTaskResult.Task.Wait(); AutoReconnectWaitToDisconnectTaskResult = new TaskCompletionSource <object>(); ConnectAsyncAutoReconnect(url, connectedAction); } catch (Exception ex) { connectedAction(false); Disconnect(); AutoReconnectWaitToDisconnectTaskResult = new TaskCompletionSource <object>(); ConnectAsyncAutoReconnect(url, connectedAction); } }); }
public static byte ReadOneByte(Stream stream, TimeSpan timeout) { ManualResetEvent resetEvent = new ManualResetEvent(false); resetEvent.Reset(); byte oneByte = 0; AsyncActions.Run(() => { int data = stream.ReadByte(); if (data >= 0) { oneByte = (byte)data; resetEvent.Set(); } }); if (resetEvent.WaitOne(timeout)) { return(oneByte); } throw new TimeoutException(); }
/// <summary> /// start client to reading stream and data from server /// </summary> /// <param name="client"></param> internal void StartToReadingClientData() { AsyncActions.Run(() => { try { var stream = _client.GetStream(); while (true) { //first byte is DataType var dataType = (DataType)stream.ReadByte(); //secound byte is compress mode var compresssMode = (CompressMode)stream.ReadByte(); // server is called client method if (dataType == DataType.CallMethod) { var bytes = GoStreamReader.ReadBlockToEnd(stream, compresssMode, ProviderSetting.MaximumReceiveDataBlock, IsWebSocket); if (SecuritySettings != null) { bytes = DecryptBytes(bytes); } var json = Encoding.UTF8.GetString(bytes); MethodCallInfo callInfo = JsonConvert.DeserializeObject <MethodCallInfo>(json); if (callInfo.Type == MethodType.User) { CallMethod(callInfo); } else if (callInfo.Type == MethodType.SignalGo) { if (callInfo.MethodName == "/MustReconnectUdpServer") { ReconnectToUdp(callInfo); } } } //after client called server method, server response to client else if (dataType == DataType.ResponseCallMethod) { var bytes = GoStreamReader.ReadBlockToEnd(stream, compresssMode, ProviderSetting.MaximumReceiveDataBlock, IsWebSocket); if (SecuritySettings != null) { bytes = DecryptBytes(bytes); } var json = Encoding.UTF8.GetString(bytes); MethodCallbackInfo callback = JsonConvert.DeserializeObject <MethodCallbackInfo>(json); var geted = ConnectorExtension.WaitedMethodsForResponse.TryGetValue(callback.Guid, out KeyValue <AutoResetEvent, MethodCallbackInfo> keyValue); if (geted) { keyValue.Value = callback; keyValue.Key.Set(); } } else if (dataType == DataType.GetServiceDetails) { var bytes = GoStreamReader.ReadBlockToEnd(stream, compresssMode, ProviderSetting.MaximumReceiveDataBlock, IsWebSocket); if (SecuritySettings != null) { bytes = DecryptBytes(bytes); } var json = Encoding.UTF8.GetString(bytes); getServiceDetialResult = JsonConvert.DeserializeObject <ProviderDetailsInfo>(json); if (getServiceDetialResult == null) { getServiceDetialExceptionResult = JsonConvert.DeserializeObject <Exception>(json); } getServiceDetailEvent.Set(); getServiceDetailEvent.Reset(); } else if (dataType == DataType.GetMethodParameterDetails) { var bytes = GoStreamReader.ReadBlockToEnd(stream, compresssMode, ProviderSetting.MaximumReceiveDataBlock, IsWebSocket); if (SecuritySettings != null) { bytes = DecryptBytes(bytes); } var json = Encoding.UTF8.GetString(bytes); getmethodParameterDetailsResult = json; getServiceDetailEvent.Set(); getServiceDetailEvent.Reset(); } else { //incorrect data! :| SignalGo.Shared.Log.AutoLogger.LogText("StartToReadingClientData Incorrect Data!"); Dispose(); break; } } } catch (Exception ex) { SignalGo.Shared.Log.AutoLogger.LogError(ex, "StartToReadingClientData"); Dispose(); } }); }