static void Main(string[] args) { Console.WriteLine("TestSimpleEchoClient"); _clientConnector = new ClientConnector(new ClientConnectorSettings() { PacketsMap = new Dictionary <Tuple <int, int>, Type>() { { new Tuple <int, int>(1, 1), typeof(string) }, }, ServerAddressList = new List <Tuple <string, int> >() { new Tuple <string, int>("127.0.0.1", 1112) } }); _clientConnector.OnPacket += ClientConnector_OnPacket; _clientConnector.OnConnect += ClientConnector_OnConnect; _clientConnector.OnDisconnect += ClientConnector_OnDisconnect; _clientConnector.OnException += ClientConnector_OnException; _clientConnector.OnDebugLog += ClientConnector_OnDebugLog; _clientConnector.Connect(); try { _clientConnector.Send(1, 1, "start"); } catch (Exception ex) { Console.WriteLine("Exception on first packet:" + ex.ToString()); } while (true) { try { Console.WriteLine("Enter Input"); var inputLine = Console.ReadLine(); if (inputLine == "exp") { _clientConnector.SendRequest(1, 1, inputLine); } else { _clientConnector.Send(1, 1, inputLine); } } catch (Exception ex) { Console.WriteLine("Exception:" + ex.ToString()); } } }
public void SendCall(LJPCall sendCallParameter) { if (sendCallParameter == null) { SendResponse(null);//No Data } else { var messageExtractor = factoryClientLJP.CreateMessageFactory(sendCallParameter.Version); string jsonFinal = messageExtractor.Parse(sendCallParameter); string message = $"Version={sendCallParameter.Version}\n" + $"Length={Encoding.UTF8.GetBytes(jsonFinal).Length}\n" + $"Id={sendCallParameter.Id}\n" + $"KeepAlive={sendCallParameter.KeepAlive}\n" + $"NeedResponse={sendCallParameter.NeedResponse}\n" + $"Interface={sendCallParameter.InterfaceName}\n" + $"Method={sendCallParameter.MethodName}\n" + "\n" + $"{jsonFinal}"; #if DEBUG logger.Debug($"SendCall From [{ClientConnector.LocalEndPoint?.ToString() ?? ""}] To [{ClientConnector.RemoteEndPoint?.ToString() ?? ""}], Msg = {jsonFinal.Replace("\n", "[nl]")}"); #endif var aMessage = Encoding.UTF8.GetBytes(message); ClientConnector.Send(aMessage, 0, aMessage.Length); } }
public void SendResponse(object oObject) { if (oObject == null) { oObject = new LJPExceptionDTO() { Message = "No data", IClientLJPExceptionType = (int)LJPExceptionType.NoData }; } var type = oObject.GetType(); //DataContractJsonSerializer oJsonSerializer = new(type); //MemoryStream oMS = new(); //oJsonSerializer.WriteObject(oMS, oObject);//Deserialize the oObject into Memory Stream (oMS). //oMS.Position = 0; //string sJson = new StreamReader(oMS).ReadToEnd();//Read object deserialized. var json = JsonConvert.SerializeObject(oObject); int iLength = Encoding.UTF8.GetBytes(json).Length; string message = string.Format( "Length={0}\n" + "Type={1}\n" + "\n{2}" , iLength , !oObject.GetType().GetTypeInfo().IsGenericType ? oObject.GetType().Name : oObject.GetType().FullName , json ); #if DEBUG logger.Debug($"SendReponse [Lenght={iLength}] From [{ClientConnector.LocalEndPoint?.ToString() ?? ""}] - To [{ClientConnector.RemoteEndPoint?.ToString() ?? ""}] {json.Replace("\n", "[nl]")}"); #endif var aMessage = Encoding.UTF8.GetBytes(message); ClientConnector.Send(aMessage, 0, aMessage.Length); }