Exemplo n.º 1
0
        async Task CreateServer()
        {
            // Create the server
            _server = new SimplMessageServer();

            // Create a callback for received data of type classA
            _server.AddCallBack <ClassA>((receivedMessage) =>
            {
                // get data from received message cast to ClassA
                var receivedObject = receivedMessage.GetContent <ClassA>();
                Console.WriteLine($"Server: received message {receivedObject.VarDouble}, {receivedObject.VarInt}");

                // Reply to the message with the same content (echo)
                _server.Reply(receivedObject, receivedMessage);
                Console.WriteLine($"Server: replied to message");
            });

            // Start listening for client connections on loopback end point
            _server.Listen(new IPEndPoint(IPAddress.Loopback, 5000));


            // Wait until a new client has connected
            Console.WriteLine($"Server: awaiting connection from new client");
            var connectedClient = await _server.WaitForNewClientAsync();

            Console.WriteLine($"Server: a client has connected");
        }
Exemplo n.º 2
0
        void CreateServer()
        {
            // Create the server
            _server = new SimplMessageServer(
                () => new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp),
                keepAlive: true,
                messageBufferSize: 65536,
                communicationTimeout: 10000,
                maxMessageSize: 10485760,
                useNagleAlgorithm: false);

            // Create a callback for received data of class A
            _server.AddCallBack <ClassA>(ServerReceivedClassACallback);

            // Indicate when a client has connected
            _server.ClientConnected += (s, e) => {
                Console.WriteLine($"Server: connected from {e.ConnectedClient.IPEndPoint}");
            };

            // Indicate when a client has disconnected
            _server.ClientDisconnected += (s, e) =>
            {
                Console.WriteLine($"Server: a client disconnected");
            };

            // Start listening for client connections
            _server.Listen(
                ipEndPoint: new IPEndPoint(IPAddress.Loopback, 5000),
                discoverable: true,
                name: "ServerName",
                description: "Description of server");
        }
Exemplo n.º 3
0
        void CreateServer()
        {
            // Create the server
            var server = new SimplMessageServer();

            // Create a callback for received data of type classA
            // (You could also implement this as a lambda function)
            server.AddCallBack <ClassA>(ServerReceivedClassACallback);

            // Create a callback for received data of type classA with a custom identifier
            server.AddCallBack("ObjectOfTypeClassA", ServerReceivedClassACallback);

            // Start listening for client connections on loopback end point
            server.Listen(new IPEndPoint(IPAddress.Loopback, 5000));
        }
Exemplo n.º 4
0
        void CreateServer()
        {
            // Create the server
            var server = new SimplMessageServer();

            // Create a callback for received data of type classA
            server.AddCallBack <ClassA>((receivedMessage) =>
            {
                // get data from received message cast to ClassA
                var receivedObject = receivedMessage.GetContent <ClassA>();
                Console.WriteLine($"Server received message: {receivedObject.VarDouble}, {receivedObject.VarInt}");

                // Reply to the message with the same content (echo)
                server.Reply(receivedObject, receivedMessage);
                Console.WriteLine($"Server replied to message");
            });

            // Start listening for client connections on loopback end point
            server.Listen(new IPEndPoint(IPAddress.Loopback, 5000));
        }
Exemplo n.º 5
0
        void CreateServer()
        {
            // Create the server
            var server = new SimplMessageServer();

            // Start listening for client connections on loopback end point
            server.Listen(new IPEndPoint(IPAddress.Loopback, 5000));

            // Wait until a new client has connected
            var connectedClient = server.WaitForNewClient();

            // Create an object to send
            var objectToSend = new ClassA()
            {
                VarInt = 2, VarDouble = 2.5
            };

            // Send it with an implicit descriptor (which is the class name)
            Console.WriteLine($"server sending received message: {objectToSend.VarDouble}, {objectToSend.VarInt} with implicit descriptor {typeof(ClassA).Name}");
            server.Send(objectToSend, connectedClient);
        }