Ejemplo n.º 1
0
        public static void StartEchoServer()
        {
            // Create new action server, action methods are automatically loaded (See bottom of this file for action methods)
            // These examples are for the EasyTcpActionClient + EasyTcpActionServer
            var server = new EasyTcpActionServer();

            server.Start(Port);

            // OnUnKnownAction event, executed when an unknown action is received
            // If not used unknown actions are ignored
            server.OnUnknownAction += (sender, message) => Console.WriteLine("Unknown action received");

            /* Interceptor, function that gets called before an action is executed.
             * Action gets aborted when interceptor returns false.
             *
             * action = received message + extra property for the action code
             */
            server.Interceptor = action =>
            {
                if (action.IsAction("ECHO2"))
                {
                    return(false);
                }
                return(true);
            };

            // Load more actions from other assembly
            // server.AddActions(Assembly.GetExecutingAssembly(), "EasyTcp3.Examples.Actions2");

            // Execute action
            server.ExecuteAction("SAY_HELLO", new Message());
        }
Ejemplo n.º 2
0
        public void Setup()
        {
            _port = TestHelper.GetPort();
            var server = new EasyTcpActionServer();

            server.Start(_port);
            //See action in Actions.cs
        }
Ejemplo n.º 3
0
        public async Task DetectAndExecuteTypes()
        {
            var server = new EasyTcpActionServer(nameSpace: "DetectTestActions");

            for (int i = 1; i <= 7; i++)
            {
                await server.ExecuteAction(i);
            }
            Assert.AreEqual(7, TestActions.Counter);
        }
Ejemplo n.º 4
0
        public void TestServerActionsTrigger()
        {
            ushort port = TestHelper.GetPort();

            using var server = new EasyTcpActionServer().Start(port);

            using var client = new EasyTcpClient();
            Assert.IsTrue(client.Connect(IPAddress.Loopback, port));

            string data  = "test";
            var    reply = client.SendActionAndGetReply("ECHO", data);

            Assert.AreEqual(data, reply.ToString());
        }
Ejemplo n.º 5
0
        public void TestInterceptorTrue()
        {
            ushort port = TestHelper.GetPort();

            using var server = new EasyTcpActionServer
                  { Interceptor = (actionCode, m) => true };
            server.Start(port);

            using var client = new EasyTcpClient();
            Assert.IsTrue(client.Connect(IPAddress.Loopback, port));
            var message = client.SendActionAndGetReply("ECHO", "data");

            Assert.IsNotNull(message); // Client did receive a message
        }
Ejemplo n.º 6
0
        public async Task TestInterceptorFalse()
        {
            ushort port = TestHelper.GetPort();

            using var server = new EasyTcpActionServer
                  { Interceptor = (actionCode, m) => false }; // Create useless server
            server.Start(port);

            using var client = new EasyTcpClient();
            Assert.IsTrue(client.Connect(IPAddress.Loopback, port));
            var message = await client.SendActionAndGetReplyAsync("ECHO", "data", TimeSpan.FromMilliseconds(500));

            Assert.IsNull(message); // Client did not receive a message
        }
Ejemplo n.º 7
0
        public void TestInterceptorTrue()
        {
            ushort port = TestHelper.GetPort();

            using var server = new EasyTcpActionServer
                  {
                      Interceptor = action => action.ActionCode.IsEqualToAction("ECHO") && action.ToString() == "data"
                  }.Start(port);

            using var client = new EasyTcpClient();
            Assert.IsTrue(client.Connect(IPAddress.Loopback, port));
            var message = client.SendActionAndGetReply("ECHO", "data");

            Assert.IsNotNull(message);
        }
Ejemplo n.º 8
0
        public void OnUnknownActionFalse()
        {
            ushort port = TestHelper.GetPort();

            using var server = new EasyTcpActionServer();
            server.Start(port);

            int triggeredCounter = 0;

            server.OnUnknownAction += (sender, c) => Interlocked.Increment(ref triggeredCounter);

            using var client = new EasyTcpClient();
            Assert.IsTrue(client.Connect(IPAddress.Loopback, port));
            client.SendAction("ECHO", "data");

            TestHelper.WaitWhileTrue(() => triggeredCounter == 0);
            Assert.AreEqual(0, triggeredCounter);
        }
Ejemplo n.º 9
0
        public void TestFilters()
        {
            ushort port = TestHelper.GetPort();

            using var server = new EasyTcpActionServer().Start(port);

            using var client = new EasyTcpClient();
            Assert.IsTrue(client.Connect("127.0.0.1", port));
            client.SendAction("AUTH");
            TestHelper.WaitWhileTrue(() => _counter == 0);
            Assert.AreEqual(0, _counter);

            client.SendAction("LOGIN");
            client.SendAction("AUTH");
            client.SendAction("AUTH");
            TestHelper.WaitWhileFalse(() => _counter == 2);
            Assert.AreEqual(2, _counter);
        }
Ejemplo n.º 10
0
        public static void StartEchoServer()
        {
            // Our actions will get automatically loaded, assembly can be specified
            var server = new EasyTcpActionServer(); //Start server on port 6001

            server.Start(Port);

            // OnUnKnownAction event, executed when an unknown action is received
            //server.OnUnknownAction += (sender, message) => { };

            // Interceptor, function that gets executed before an action is executed.
            // Action gets aborted when returning false.
            //
            // i = action code, use string.ToActionCode() for comparing with strings (See ActionClient)
            // message = receiving data
            server.Interceptor = (i, message) =>
            {
                //if(i != "ECHO".ToActionCode()) Console.WriteLine($"Received action {i}");
                return(true);
            };
        }
Ejemplo n.º 11
0
        public async Task TestDetectAllTypes()
        {
            var client = new EasyTcpActionClient(nameSpace: "EasyTcp3.Test.Actions.Types");

            for (int i = 1; i <= 7; i++)
            {
                await client.ExecuteAction(i);

                Assert.AreEqual(i, TestActions.Counter);
            }

            var server = new EasyTcpActionServer(nameSpace: "EasyTcp3.Test.Actions.Types");

            TestActions.Counter = 0;
            for (int i = 1; i <= 7; i++)
            {
                await server.ExecuteAction(i);

                Assert.AreEqual(i, TestActions.Counter);
            }
        }
Ejemplo n.º 12
0
        public void OnUnknownActionTrue()
        {
            ushort port = TestHelper.GetPort();

            using var server = new EasyTcpActionServer();
            server.Start(port);

            int triggeredCounter = 0;

            server.OnUnknownAction += (sender, c) =>
            {
                if (c.ActionCode.IsEqualToAction("INVALIDACTION") && c.ToString() == "data")
                {
                    Interlocked.Increment(ref triggeredCounter);
                }
            };

            using var client = new EasyTcpClient();
            Assert.IsTrue(client.Connect(IPAddress.Loopback, port));
            client.SendAction("INVALIDACTION", "data");

            TestHelper.WaitWhileFalse(() => triggeredCounter == 1);
            Assert.AreEqual(1, triggeredCounter);
        }
Ejemplo n.º 13
0
 public void Setup()
 {
     _port = TestHelper.GetPort();
     var server = new EasyTcpActionServer().Start(_port);
 }
Ejemplo n.º 14
0
 public static void Run()
 {
     var server = new EasyTcpActionServer().Start(Port);
 }
Ejemplo n.º 15
0
 public static void Run()
 {
     using var server     = new EasyTcpActionServer().Start(PORT);
     server.OnConnect    += (sender, client) => Console.WriteLine($"Client connected [ip: {client.GetIp()}]");
     server.OnDisconnect += (sender, client) => Console.WriteLine($"Client disconnected [ip: {client.GetIp()}]");
 }