Beispiel #1
0
 public Server(ServerContext serverContext)
 {
     ServerContext = serverContext;
     M             = ServerContext.M;
     L             = ServerContext.L;
     _helper       = new MessageTypedSender(M);
     TypeBinder    = new MessageTypeNameSerializationBinder(serverContext);
     LoadExtensions();
 }
Beispiel #2
0
        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)
                    {
                    }
                }
            });
        }
Beispiel #3
0
        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);
            });
        }
Beispiel #4
0
        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);
            //});
        }