Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("SyncIO Chat example (Local computer only).");
            int opt = ConsoleExtentions.GetDesision(new string[] { "Client", "Server" });

            Console.Clear();
            if (opt == 1)
            {
                Server();
            }
            else
            {
                Client();
            }
        }
Exemplo n.º 2
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");
        }
Exemplo n.º 3
0
        private static void Server()
        {
            var packer = new Packager(new Type[] {
                typeof(SetName),
                typeof(ChatMessage)
            });

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

            var clients = new Dictionary <Guid, ConnectedChatClient>();

            var sendToAll = new Action <IPacket>((p) => {
                //Send to all clients who have set a name.
                foreach (var c in clients.Select(x => x.Value))
                {
                    c.Connection.Send(p);
                }
            });


            var gettime = server.RegisterRemoteFunction("GetTime", new Func <string> (() => {
                Console.WriteLine("Time function called");
                return(string.Format("It is {0}.", DateTime.Now.ToShortTimeString()));
            }));

            gettime.SetAuthFunc((c, f) => {
                return(clients[c.ID].CanUseTimeCommand);
            });

            var toggletime = server.RegisterRemoteFunction("toggletime", new Func <string>(() => "\"time\" command has been toggled."));

            toggletime.SetAuthFunc((c, f) => {
                clients[c.ID].CanUseTimeCommand = !clients[c.ID].CanUseTimeCommand;
                return(true);
            });

            server.OnClientConnect += (SyncIOServer sender, SyncIOConnectedClient client) => {
                Console.WriteLine("{0}] New connection", client.ID);
                client.OnDisconnect += (c, ex) => {
                    Console.WriteLine("[{0}] Disconnected: {1}", c.ID, ex.Message);
                };
            };

            server.SetHandler <SetName>((c, p) => {
                sendToAll(new ChatMessage($"{p.Name} connected. ({c.ID})"));
                clients.Add(c.ID, new ConnectedChatClient(c, p.Name));
            });

            server.SetHandler <ChatMessage>((c, p) => {
                var msg = $"<{clients[c.ID].Name}> {p.Message}";
                sendToAll(new ChatMessage(msg));
                Console.WriteLine(msg);
            });

            Console.WriteLine("Closing socket examples:");
            //Listen on all of the following ports:
            var firstSock = server.ListenTCP(9996); //Add it to a variable for closing example

            server.ListenTCP(9997);
            server.ListenTCP(9998);
            server.ListenTCP(9999);

            if (server.Count() < 1)
            {
                ConsoleExtentions.ErrorAndClose("Failed to listen on any ports.");
            }

            foreach (var sock in server)
            {
                Console.WriteLine("Listening on {0}", sock.EndPoint.Port);
                sock.OnClose += (sender, err) => {
                    Console.WriteLine("{0}] Socket closed. {1}", sender.EndPoint.Port, err);
                };
            }

            Console.WriteLine("Closing port {0} and 9998", firstSock.EndPoint.Port);
            firstSock.Dispose();    //Either close from var
            server[9997].Dispose(); //Or by server index.

            foreach (var sock in server)
            {
                Console.WriteLine("Listening on {0}", sock.EndPoint.Port);
            }

            Console.WriteLine("Reopening ports in 3 seconds");
            Thread.Sleep(3000);
            Console.Clear();

            //Reopen:
            server.ListenTCP(9996);
            server.ListenTCP(9997);

            foreach (var sock in server)
            {
                Console.WriteLine("Listening on {0}", sock.EndPoint.Port);
            }


            while (true)
            {
                Console.ReadLine();
            }
        }