public winMain()
        {
            InitializeComponent();

            server = new SyncIOServer(SyncIO.Transport.TransportProtocal.IPv4, RemoteConfig.GetPackager());

            server.SetHandler <HandshakePacket>((client, packet) => {
                var nsClientInfo = new NSClient(packet.Username, packet.AppID);
                client.Tag       = nsClientInfo;
                client.Send(new HandshakeResponsePacket(true));
            });

            server.SetDefaultRemoteFunctionAuthCallback((client, func) => {
                var FuncInfo = func.Tag as FunctionInfo;
                if (FuncInfo == null)
                {
                    return(false);
                }

                var nsClientInfo = client.Tag as NSClient;
                if (nsClientInfo == null)
                {
                    return(false);
                }

                var succ = nsClientInfo.AppID.Equals(FuncInfo.ID) &&
                           NetSealAPI.IsValidConnection(FuncInfo.ApiCode, client, nsClientInfo.Username);

                if (!succ)
                {
                    client.Disconnect(new AccessViolationException());
                }

                return(succ);
            });

            LoadFunctions();

            foreach (var port in ConfigLoader.GetPorts())
            {
                server.ListenTCP(port);
            }

            var listeningPorts = server.Count();

            if (listeningPorts < 1)
            {
                lblStatus.Content = $"Idle. (listening on {listeningPorts} ports).";
            }
            else
            {
                lblStatus.Content = $"listening on {listeningPorts} ports.";
            }
        }
        private void miNetworking_Click(object sender, RoutedEventArgs e)
        {
            var nwwin = new winNetworkSettings(server);

            nwwin.ShowDialog();
            ConfigLoader.WritePorts(server.Select(x => x.EndPoint.Port).ToArray());
            var listeningPorts = server.Count();

            if (listeningPorts < 1)
            {
                lblStatus.Content = $"Idle. (listening on {listeningPorts} ports).";
            }
            else
            {
                lblStatus.Content = $"listening on {listeningPorts} ports.";
            }
        }
示例#3
0
文件: Program.cs 项目: sensi17/SyncIO
        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();
            }
        }