Example #1
0
        static void Setup()
        {
            _client = new SyncIOClient(TransportProtocol.IPv4, _packer);

            SetupPacketHandlers();

            if (!_client.Connect(Host, Port))
            {
                ConsoleExtentions.ErrorAndClose("Failed to connect to server.");
            }

            Console.WriteLine("Connected on port {0}. Waiting for handshake.", _client.EndPoint.Port);

            var success = _client.WaitForHandshake();

            /*
             * The asynchronous way to get handshake would be subscribing
             * to the client.OnHandshake event.
             */

            if (!success)
            {
                ConsoleExtentions.ErrorAndClose("Handshake failed.");
            }

            Console.WriteLine("Handshake success. Got ID: {0}", _client.ID);

            Console.WriteLine($"Enabling GZip compression...");
            _client.SetCompression(new SyncIOCompressionGZip());

            Console.WriteLine($"Enabling rijndael encryption using key {string.Join("", Key.Select(x => x.ToString("X2")))}");
            _client.SetEncryption(new SyncIOEncryptionAes(Key, IV));
        }
Example #2
0
 public RemoteFunction <T> RegisterFunction <T>(SyncIOClient client, string name)
 {
     lock (SyncLock) {
         if (FunctionList.ContainsKey(name))
         {
             return(FunctionList[name] as RemoteFunction <T>);
         }
         else
         {
             var f = new InternalRemoteFunction <T>(client, name);
             FunctionList.Add(name, f);
             return(f);
         }
     }
 }
Example #3
0
        public RemoteCode(string _appID, string _username)
        {
            AppID    = _appID;
            Username = _username;

            client = new SyncIOClient(SyncIO.Transport.TransportProtocal.IPv4, RemoteConfig.GetPackager());

            client.OnDisconnect += (c, e) => OnDisconnect?.Invoke();
            client.SetHandler <HandshakeResponsePacket>((c, p) => OnConnect?.Invoke());

            client.OnHandshake += (c, id, succ) => {
                if (!succ)
                {
                    OnConnect?.Invoke();
                }
                else
                {
                    c.Send(new HandshakePacket(Username, AppID));
                }
            };
        }
Example #4
0
        public void TestRemoteCalls()
        {
            var client = new SyncIOClient();
            var server = new SyncIOServer();

            var Test1 = server.RegisterRemoteFunction("Test1", new Func <string, int>((string string1) => string1.Length));

            server.RegisterRemoteFunction("Test2", new Func <string, string>((string string1) => string.Concat(string1.Reverse())));
            server.RegisterRemoteFunction("Test3", new Func <string, char>((string string1) => string1.FirstOrDefault()));

            Test1.SetAuthFunc((SyncIOConnectedClient requester, RemoteFunctionBind callingFunc) =>
            {                 //Example of authenticating call to Test1
                return(true); //Always allow call
            });

            var listenSock = server.ListenTCP(6000);

            Assert.IsNotNull(listenSock);

            client.OnDisconnect += (sCl, err) =>
            {
                throw new AssertFailedException(err.Message, err);
            };

            Assert.IsTrue(client.Connect("127.0.0.1", 6000));

            Assert.IsTrue(client.WaitForHandshake());

            var testParam = "Hello World";
            var func1     = client.GetRemoteFunction <int>("Test1");
            var func2     = client.GetRemoteFunction <string>("Test2");
            var func3     = client.GetRemoteFunction <char>("Test3");

            Assert.AreEqual(testParam.Length, func1.CallWait(testParam));
            Assert.AreEqual(string.Concat(testParam.Reverse()), func2.CallWait(testParam));
            Assert.AreEqual(testParam.FirstOrDefault(), func3.CallWait(testParam));
            func1.CallWait(1, 2, 3, 4, 5);
            Assert.AreEqual(func1.LastStatus, FunctionResponceStatus.InvalidParameters);
        }
Example #5
0
        public void TestUDP()
        {
            var packets = 10;

            var client = new SyncIOClient();
            var server = new SyncIOServer();

            object[] sent    = new object[] { 1, 2, 3, "hello", "world" };
            int      receved = 0;

            server.SetHandler((SyncIOConnectedClient s, object[] data) =>
            {
                Assert.AreEqual(client.ID, s.ID);
                CollectionAssert.AreEqual(sent, data);
                receved++;
            });

            var sock = server.ListenTCP(55455);

            Assert.IsNotNull(sock);

            sock.TryOpenUDPConnection();
            Assert.IsTrue(sock.HasUDP);

            Assert.IsTrue(client.Connect("127.0.0.1", 55455));
            Assert.IsTrue(client.WaitForHandshake());
            client.TryOpenUDPConnection();
            Assert.IsTrue(client.WaitForUDP());

            for (int i = 0; i < packets; i++)
            {
                client.SendUDP(sent);
            }
            Thread.Sleep(3000); //Wait 3 secodns

            Assert.AreEqual(packets, receved);
        }
Example #6
0
        private static void Client()
        {
            //When sending custom objects, you must create a new packer and specify them.
            var packer = new Packager(new Type[] {
                typeof(SetName),
                typeof(ChatMessage)
            });

            //Using ipv4 and the packer that has the custom types.
            var client = new SyncIOClient(TransportProtocal.IPv4, packer);

            //The diffrent types of handlers: (all optional)

            //The type handler.
            //This handler handles a specific object type.
            client.SetHandler <ChatMessage>((SyncIOClient sender, ChatMessage messagePacket) => {
                //All ChatMessage packages will be passed to this callback
                Console.WriteLine(messagePacket.Message);
            });

            //This handler handles any IPacket that does not have a handler.
            client.SetHandler((SyncIOClient sender, IPacket p) => {
                //Any packets without a set handler will be passed here
            });

            //This handler handles anything that is not a SINGLE IPacket object
            client.SetHandler((SyncIOClient sender, object[] data) => {
                //Any object array sent will be passed here, even if the array contains
                //A packet with a handler (e.g. ChatMessage)
            });



            if (!client.Connect("127.0.0.1", new Random().Next(9996, 10000)))//Connect to any of the open ports.
            {
                ConsoleExtentions.ErrorAndClose("Failed to connect to server.");
            }

            //Connecting and handshake are not the same.
            //Connecting = Establishing a connection with a socket
            //Handskake  = Establishing a connection with a SyncIOServer and getting an ID.
            Console.WriteLine("Connected on port {0}. Waiting for handshake.", client.EndPoint.Port);

            bool success = client.WaitForHandshake();

            /*
             * The asynchronous way to get handshake would be subscribing
             * to the client.OnHandshake event.
             */

            if (!success)
            {
                ConsoleExtentions.ErrorAndClose("Handshake failed.");
            }

            Console.WriteLine("Handshake success. Got ID: {0}", client.ID);

            var name = ConsoleExtentions.GetNonEmptyString("Enter a name: ");

            client.Send(new SetName(name));

            Console.WriteLine("Name set. You can now send messages.");

            bool connected = true;

            client.OnDisconnect += (s, err) => {
                connected = false;
            };

            var GetTime    = client.GetRemoteFunction <string>("GetTime");
            var toggletime = client.GetRemoteFunction <string>("toggletime");

            while (connected)
            {
                var msg = ConsoleExtentions.GetNonEmptyString("");

                if (msg == "time")
                {
                    //Call a function that requires authentication checking.
                    GetTime.CallWait();                                       //If call failed, return will be the default value for the type
                    if (GetTime.LastStatus == FunctionResponceStatus.Success) //No issues
                    {
                        Console.WriteLine(GetTime.LastValue);                 //Write last returned value to console
                    }
                    else
                    {
                        Console.WriteLine("Call failed. reason: {0}. Try the \"toggletime\" command", GetTime.LastStatus);
                    }
                }
                else if (msg == "toggletime")
                {
                    Console.WriteLine(toggletime.CallWait()); //If call fails, nothing (null) will be printed because it is strings default value.
                }
                else
                {
                    if (connected)
                    {
                        client.Send(new ChatMessage(msg));
                    }
                }
            }
            ConsoleExtentions.ErrorAndClose("Lost connection to server");
        }
Example #7
0
 public InternalRemoteFunction(SyncIOClient client, string name)
 {
     _name   = name;
     _client = client;
 }
Example #8
0
        public void TestPacketSending()
        {
            var done = new ManualResetEvent(false);

            var packer = new Packager(new Type[] {
                typeof(CustomDataSending)
            });

            var arrPayload = new object[] {
                9,
                "SyncIOTest",
                "Example",
                666.333f
            };

            var cPayload = new CustomDataSending()
            {
                Num = 5555,
                str = "SyncIOTest",
                fl  = 6666.4444f
            };

            var server = new SyncIOServer(TransportProtocol.IPv4, packer);

            Guid cID = Guid.NewGuid();

            server.OnClientConnect += (s, c) =>
            {
                Console.WriteLine("Connected.");
                cID             = c.ID;
                c.OnDisconnect += (rtcl, err) =>
                {
                    throw new AssertFailedException(err.Message, err);
                };
            };

            server.SetHandler((SyncIOConnectedClient s, object[] dat) =>
            {
                CollectionAssert.AreEqual(arrPayload, dat);
                s.Send(cPayload);
            });

            var listenSock = server.ListenTCP(9000);

            Assert.IsNotNull(listenSock);

            var client = new SyncIOClient(TransportProtocol.IPv4, packer);

            client.OnDisconnect += (sCl, err) =>
            {
                throw new AssertFailedException(err.Message, err);
            };

            client.SetHandler <CustomDataSending>((s, d) =>
            {
                Assert.AreEqual(cPayload.fl, d.fl);
                Assert.AreEqual(cPayload.Num, d.Num);
                Assert.AreEqual(cPayload.str, d.str);
                done.Set();
            });

            client.OnHandshake += (s, id, succ) =>
            {
                Assert.IsTrue(succ);
                Thread.Sleep(100); //Wait for server callback to finish
                Assert.AreEqual(cID, id);
                s.Send(arrPayload);
            };

            Assert.IsTrue(client.Connect("127.0.0.1", 9000));


            Assert.IsTrue(done.WaitOne(10 * 1000));

            listenSock.Dispose();
        }
Example #9
0
 public InternalRemoteFunction(SyncIOClient _client, string _name)
 {
     Name   = _name;
     client = _client;
 }