public void Send(BaseTransportMessage message) { var data = _dataProvider.Serialize(message).ToString(); var dataBytes = data.GetBytes(); var prefixedDataBytes = ByteUtils.PrefixBufferSize(dataBytes); _clientSocket.SendAsync(new ArraySegment <byte>(prefixedDataBytes), SocketFlags.None); }
private void PossibleSendMessage(BaseTransportMessage m) { if (m.IsRemote) { return; } _extensions.ForEach(x => Task.Run(() => x.Send(m))); }
public void SendAsClient(Server server, IMessenger msgr, BaseTransportMessage msg) { var ctx = new WebSocketServerContext(server); var l = server.ServerContext.L; Task.Run(() => { l.Log("Establishing connection to local socket server."); using (var ws = new WebSocketSharp.WebSocket($"ws://{IPAddress.Loopback}:{ctx.Port}/")) { var settings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Objects, SerializationBinder = server.TypeBinder }; var helper = new MessageTypedSender(C.M); ws.OnMessage += (sender, args) => { if (args.IsText) { var dataText = args.Data; if (string.IsNullOrWhiteSpace(dataText)) { return; } if (JsonConvert.DeserializeObject(dataText, settings) is BaseTransportMessage m) { helper.UnPackSendReceivedMessage(new ExternalMessage(m.SessionId, m)); } } else { throw new InvalidOperationException("Only text data is supported."); } }; ws.Connect(); var json = JsonConvert.SerializeObject(new HelloRequest(), settings); ws.Send(json); while (ws.IsAlive) { } } }); }
public void SendAsClient(MessageServer server, IMessenger msgr, BaseTransportMessage msg) { var ctx = new SocketServerContext(server); var l = server.ServerContext.L; var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); l.Log("Establishing connection to local socket server."); client.Connect("127.0.0.1", ctx.PortNumber); var settings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Objects, SerializationBinder = server.TypeBinder }; var dataString = JsonConvert.SerializeObject(msg, settings); dataString = "{\"type\":\"prime.keysmanager.privateproviderslistrequestmessage\"}"; l.Log("Connection established, sending message: " + dataString); var dataBytes = dataString.GetBytes(); client.Send(dataBytes); Task.Run(() => { var helper = new MessageTypedSender(_cCtx.Messenger); do { var buffer = new byte[1024]; var iRx = client.Receive(buffer); var recv = buffer.GetString().Substring(0, iRx); if (string.IsNullOrWhiteSpace(recv)) { continue; } if (JsonConvert.DeserializeObject(recv, settings) is BaseTransportMessage m) { helper.UnPackSendReceivedMessage(new ExternalMessage(m.ClientId, m)); } } while (client.Connected); }); }
public void Send(BaseTransportMessage message) { ServerInstance?.Send(message); }
public void SendAsClient(Server server, IMessenger msgr, BaseTransportMessage msg, bool echoClient = false) { var ctx = new WebSocketServerContext(server); var l = server.ServerContext.L; Task.Run(() => { l.Log("Establishing connection to local socket server."); using (var ws = new WebSocketSharp.WebSocket($"ws://{IPAddress.Loopback}:{ctx.Port}/{(echoClient ? "echo": "")}")) { var settings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Objects, SerializationBinder = server.TypeBinder }; var helper = new MessageTypedSender(C.M); ws.OnMessage += (sender, args) => { if (args.IsText) { var dataText = args.Data; if (string.IsNullOrWhiteSpace(dataText)) { return; } l.Log($"Raw from server: {dataText}"); if (JsonConvert.DeserializeObject(dataText, settings) is BaseTransportMessage m) { helper.UnPackSendReceivedMessage(new ExternalMessage(m.SessionId, m)); } } else { throw new InvalidOperationException("Only text data is supported."); } }; ws.Connect(); var json = JsonConvert.SerializeObject(new HelloRequest(), settings); ws.Send(json); while (ws.IsAlive) { } } }); //var settings = new JsonSerializerSettings() //{ // TypeNameHandling = TypeNameHandling.Objects, // SerializationBinder = server.TypeBinder //}; //var dataString = JsonConvert.SerializeObject(msg, settings); //l.Log("Connection established, sending message: " + dataString); //var dataBytes = dataString.GetBytes(); //client.Send(dataBytes); //Task.Run(() => //{ // var helper = new MessageTypedSender(C.M); // do // { // var buffer = new byte[1024]; // var iRx = client.Receive(buffer); // var recv = buffer.GetString().Substring(0, iRx); // if (string.IsNullOrWhiteSpace(recv)) // continue; // if (JsonConvert.DeserializeObject(recv, settings) is BaseTransportMessage m) // helper.UnPackSendReceivedMessage(new ExternalMessage(m.ClientId, m)); // } while (client.Connected); //}); }
public void Send(BaseTransportMessage message) { _socketClient.Send(message); }
public void Send(BaseTransportMessage message) { ClientInstance?.Send(message); }