Exemplo n.º 1
0
        void CreateClient()
        {
            // Create the client
            var client = new SimplMessageClient();

            // Make the client connect automatically
            client.AutoConnect();

            // Wait until the connection is actually made
            client.WaitForConnection();

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

            //Send it
            Console.WriteLine("Client will send message and wait for reply");
            var receivedObject = client.SendReceive <ClassA, ClassA>(objectToSend);

            if (receivedObject == null)
            {
                Console.WriteLine("Client received no answer");
            }
            else
            {
                Console.WriteLine($"Client received answer: {receivedObject.VarDouble}, {receivedObject.VarInt}");
            }
        }
Exemplo n.º 2
0
        void CreateClient()
        {
            // Create the client
            var client = new SimplMessageClient();

            // Make the client connect automatically
            client.AutoConnect();

            // Wait until the connection is actually made
            client.WaitForConnection();

            // 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($"Client sending received message: {objectToSend.VarDouble}, {objectToSend.VarInt} with implicit descriptor {typeof(ClassA).Name}");
            client.Send(objectToSend);

            // Send it with a custom descriptor
            Console.WriteLine($"Client sending received message: {objectToSend.VarDouble}, {objectToSend.VarInt} with descriptor ObjectOfTypeClassA");
            client.Send("ObjectOfTypeClassA", objectToSend);
        }
Exemplo n.º 3
0
        public void Start()
        {
            // Get the singleton instance of SimplMessageClient
            _client = SimplMessageClient.Instance;

            // Make the client connect automatically
            _client.AutoConnect();
        }
Exemplo n.º 4
0
        async Task CreateClient()
        {
            // Create the client
            _client = new SimplMessageClient();

            // Make the client connect automatically
            _client.AutoConnect();

            // Wait until the connection is actually made
            // Because this is done asynchronously so the CreateClient
            // Method will be exited so that the server can be created

            Console.WriteLine($"Client: awaiting connection to server");
            await _client.WaitForConnectionAsync();

            Console.WriteLine($"Client: connected to server ");
        }
Exemplo n.º 5
0
        void CreateClient()
        {
            // Create the client
            var client = new SimplMessageClient();

            // Make the client connect automatically
            client.AutoConnect();

            // Create a callback for received data of type classA. This time we use a lambda function instead
            client.AddCallBack <ClassA>((receivedMessage) =>
            {
                // get data from received message cast to ClassA
                var receivedObject = receivedMessage.GetContent <ClassA>();

                // Notify that the server received data
                Console.WriteLine($"Client received message: {receivedObject.VarDouble}, {receivedObject.VarInt}");
            });
        }
Exemplo n.º 6
0
        void CreateClient()
        {
            // Create the client
            _client = new SimplMessageClient(
                () => new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp),
                keepAlive: true,
                messageBufferSize: 65536,
                communicationTimeout: 10000,
                maxMessageSize: 10485760,
                useNagleAlgorithm: false);


            // Add a callback that will be triggered when a message from the server is received
            _client.AddCallBack <ClassB>(ClientReceivedClassBCallback);

            // Indicate when connected
            _client.Connected += (s, e) => {
                Console.WriteLine($"Client: connected to {e.IPEndPoint.Address}!");
            };

            // Indicate when disconnected
            _client.Disconnected += (s, e) =>
            {
                //Disconnected not triggered, due to botched wasconnected logic
                Console.WriteLine("Client: disconnected");
            };

            // Indicate when trying to connect
            _client.ConnectionAttempt += (s, e) =>
            {
                Console.WriteLine($"Client: trying to connect to {e.IPEndPoint.Address}!");
            };

            // Make the client connect automatically
            _client.AutoConnect("ServerName");

            // Wait until the connection is actually made
            _client.WaitForConnection();

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

            // Send it
            Console.WriteLine($"Client: sending message: {objectToSend.VarDouble}, {objectToSend.VarInt}");
            _client.Send(objectToSend);

            // Wait some time for the server to respond.
            // This is needed, because if the server responds while the client is not connected, the respons is lost
            // there is no resend functionality
            Thread.Sleep(100);

            // Change mode to fixed IP auto connection
            _client.AutoConnect(new IPEndPoint(IPAddress.Loopback, 5000));

            // Now disconnect the previous connection
            _client.Disconnect();

            // Wait until the new connection is actually made based on the fixed IP
            _client.WaitForConnection();

            // Send the object again
            Console.WriteLine($"Client: sending message: {objectToSend.VarDouble}, {objectToSend.VarInt}");
            _client.Send(objectToSend);

            // Wait some time for the server to respond
            Thread.Sleep(100);

            // Change to manual connection, this will disable autoconnecting
            _client.Connect(new IPEndPoint(IPAddress.Loopback, 5000));

            // The previous function should block until the connection is made, so we will not wait, but just check
            if (_client.IsConnected())
            {
                Console.WriteLine("Client: Connected");
            }
            else
            {
                Console.WriteLine("Client: not connected, should not happen!");
            }

            // Send the object again
            Console.WriteLine($"Client: sending message: {objectToSend.VarDouble}, {objectToSend.VarInt}");
            _client.Send(objectToSend);

            // Wait some time for the server to respond
            Thread.Sleep(100);

            // Now disconnect the previous connection
            _client.Disconnect();

            // Since we have disabled the auto connect, there should be no reconnection,
            // so we'll add a timeout to prevent hanging
            bool connected = _client.WaitForConnection(TimeSpan.FromSeconds(10));

            if (connected)
            {
                Console.WriteLine("Client: Connected, even though we disabled autoconnect!");
            }
            else
            {
                Console.WriteLine("Client: did not reconnect, because we disabled autoconnect");
            }
        }