예제 #1
0
        public static void Initiate(string mud_address, int port)
        {
            client = new NetObjectClient();

            client.OnReceived += (o, a) =>
            {
                var msg = a.Data.Object as ServerMessage;
                if (msg.Name == "Welcome")
                {
                    thisGuid = new Guid(msg.Contents);
                    GUIDReceived?.Invoke(msg.Contents);
                }
                else if (msg.Name == "broadcast")
                {
                    Console.WriteLine(msg.Contents);
                }
                else if (msg.Name == "Error")
                {
                    var ex = JsonConvert.DeserializeObject <Exception>(msg.Contents);
                    TerminalBackend.PrefixEnabled = true;
                    Console.WriteLine($@"{{MUD_ERROR}}: {ex.Message}");
                    TerminalBackend.PrefixEnabled = true;
                    Console.Write($"{SaveSystem.CurrentSave.Username}@{CurrentSave.SystemName}:~$ ");
                }
                else
                {
                    MessageReceived?.Invoke(msg);
                }
            };

            client.Connect(mud_address, port);
        }
예제 #2
0
        public WebAdmin()
        {
            this.RequiresAuthentication();


            client = new NetObjectClient();

            client.OnReceived += (o, a) =>
            {
                var msg = a.Data.Object as ServerMessage;
                if (msg.Name == "Welcome")
                {
                    thisGuid = new Guid(msg.Contents);
                }
            };

            client.Connect(Program.server.Address.MapToIPv4().ToString(), 13370);

            string template = Properties.Resources.Home;

            Get["/"]       = _ => { return(GetPage(template, "index.html")); };
            Get["/{page}"] = parameters =>
            {
                return(GetPage(template, parameters.page));
            };
        }
        /// <summary>
        /// Connect to a ShiftOS server
        /// </summary>
        /// <param name="address">IP address</param>
        /// <param name="port">Port (typically this is 4433.)</param>
        public static void ConnectToServer(string address, int port)
        {
            if (clients == null)
            {
                clients = new Dictionary <string, NetObjectClient>();
            }
            var client = new NetObjectClient();

            client.OnReceived += (object s, NetReceivedEventArgs <NetObject> a) => {
                try {
                    var obj = (ObjectModel)a.Data.Object;
                    if (obj.Command == "set_ident")
                    {
                        this_id = obj.SysId;
                    }
                }
                catch
                {
                }
            };

            try
            {
                client.Connect(address, port);
                clients.Add(client.RemoteHost, client);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error: {ex.Message}");
            }
        }
예제 #4
0
 public CNetwork(string ip, int port)
 {
     client = new NetObjectClient();
     IP     = ip;
     Port   = port;
     writer = new ArrayWriter();
     reader = new ArrayReader(buffer);
 }
예제 #5
0
        /// <summary>
        /// Creates a new chat client.
        /// </summary>
        /// <param name="IP">IP address</param>
        /// <param name="nick">User nickname</param>
        public ChatClient(string IP, string nick)
        {
            NetObjectClient c = null;

            foreach (var client in Package_Grabber.clients)
            {
                if (client.Key == IP)
                {
                    if (client.Value.IsConnected == true)
                    {
                        c          = client.Value;
                        IP_Address = IP;
                    }
                }
            }
            if (c != null)
            {
                c.OnReceived += (object s, NetReceivedEventArgs <NetObject> a) =>
                {
                    try {
                        var      obj  = (ObjectModel)a.Data.Object;
                        string[] args = obj.Command.Split(' ');
                        if (args[0] == "chat")
                        {
                            switch (args[1])
                            {
                            case "joined":
                                try
                                {
                                    var usr = (ChatUser)obj.OptionalObject;
                                    if (usr.Name == nick)
                                    {
                                        if (_User == null)
                                        {
                                            _User = usr;
                                        }
                                    }
                                    if (_User != usr)
                                    {
                                        var h = OnUserJoin;
                                        if (h != null)
                                        {
                                            h(usr, new EventArgs());
                                        }
                                    }
                                }
                                catch
                                {
                                }
                                break;

                            case "userlist":
                                try
                                {
                                    var lst = (List <ChatUser>)obj.OptionalObject;
                                    var h   = OnUserListReceived;
                                    if (h != null)
                                    {
                                        h(lst, new EventArgs());
                                    }
                                }
                                catch
                                {
                                }
                                break;

                            case "left":
                                try
                                {
                                    var usr = (ChatUser)obj.OptionalObject;
                                    var h   = OnUserLeave;
                                    if (h != null)
                                    {
                                        h(usr, new EventArgs());
                                    }
                                }
                                catch
                                {
                                }
                                break;

                            case "message_received":
                                try
                                {
                                    var usr = (ChatMessage)obj.OptionalObject;
                                    var h   = OnMessageReceive;
                                    if (h != null)
                                    {
                                        h(usr, new EventArgs());
                                    }
                                }
                                catch
                                {
                                }
                                break;
                            }
                        }
                        else if (args[0] == "topic")
                        {
                            try
                            {
                                string name = (string)obj.OptionalObject;
                                var    h    = OnTopicReceived;
                                if (h != null)
                                {
                                    h(name, new EventArgs());
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
                    catch
                    {
                    }
                };
                Join(nick);
            }
            else
            {
                throw new ArgumentException("IP Address not found.");
            }
        }
예제 #6
0
        /// <summary>
        /// Initiate a new Digital Society connection.
        /// </summary>
        /// <param name="mud_address">The IP address or hostname of the target server</param>
        /// <param name="port">The target port.</param>
        public static void Initiate(string mud_address, int port)
        {
            client = new NetObjectClient();
            client.OnDisconnected += (o, a) =>
            {
                if (!UserDisconnect)
                {
                    Desktop.PushNotification("digital_society_connection", "Disconnected from Digital Society.", "The ShiftOS kernel has been disconnected from the Digital Society. We are attempting to re-connect you.");
                    TerminalBackend.PrefixEnabled = true;
                    ConsoleEx.ForegroundColor     = ConsoleColor.Red;
                    ConsoleEx.Bold = true;
                    Console.Write($@"Disconnected from MUD: ");
                    ConsoleEx.Bold            = false;
                    ConsoleEx.Italic          = true;
                    ConsoleEx.ForegroundColor = ConsoleColor.DarkYellow;
                    Console.WriteLine("You have been disconnected from the multi-user domain for an unknown reason. Your save data is preserved within the kernel and you will be reconnected shortly.");
                    TerminalBackend.PrefixEnabled = true;
                    TerminalBackend.PrintPrompt();
                    Initiate(mud_address, port);
                }
            };
            client.OnReceived += (o, a) =>
            {
                if (PingTimer.IsRunning)
                {
                    DigitalSocietyPing = PingTimer.ElapsedMilliseconds;
                    PingTimer.Reset();
                }
                var msg = a.Data.Object as ServerMessage;
                if (msg.Name == "Welcome")
                {
                    thisGuid = new Guid(msg.Contents);
                    GUIDReceived?.Invoke(msg.Contents);
                    guidReceiveARE.Set();
                    TerminalBackend.PrefixEnabled = true;
                    TerminalBackend.PrintPrompt();
                }
                else if (msg.Name == "allusers")
                {
                    foreach (var acc in JsonConvert.DeserializeObject <string[]>(msg.Contents))
                    {
                        Console.WriteLine(acc);
                    }
                    TerminalBackend.PrintPrompt();
                }
                else if (msg.Name == "update_your_cp")
                {
                    var args = JsonConvert.DeserializeObject <Dictionary <string, object> >(msg.Contents);
                    if (args["username"] as string == SaveSystem.CurrentUser.Username)
                    {
                        SaveSystem.CurrentSave.Codepoints += (ulong)args["amount"];
                        Desktop.InvokeOnWorkerThread(new Action(() =>
                        {
                            Infobox.Show($"MUD Control Centre", $"Someone bought an item in your shop, and they have paid {args["amount"]}, and as such, you have been granted these Codepoints.");
                        }));
                        SaveSystem.SaveGame();
                    }
                }
                else if (msg.Name == "broadcast")
                {
                    Console.WriteLine(msg.Contents);
                }
                else if (msg.Name == "forward")
                {
                    MessageReceived?.Invoke(JsonConvert.DeserializeObject <ServerMessage>(msg.Contents));
                }
                else if (msg.Name == "Error")
                {
                    var ex = JsonConvert.DeserializeObject <Exception>(msg.Contents);
                    TerminalBackend.PrefixEnabled = true;
                    ConsoleEx.ForegroundColor     = ConsoleColor.Red;
                    ConsoleEx.Bold = true;
                    Console.Write($@"{{MUD_ERROR}}: ");
                    ConsoleEx.Bold            = false;
                    ConsoleEx.Italic          = true;
                    ConsoleEx.ForegroundColor = ConsoleColor.DarkYellow;
                    Console.WriteLine(ex.Message);
                    TerminalBackend.PrefixEnabled = true;
                    TerminalBackend.PrintPrompt();
                }
                else
                {
                    MessageReceived?.Invoke(msg);
                }
            };

            try
            {
                client.Connect(mud_address, port);
            }
            catch (SocketException ex)
            {
                System.Diagnostics.Debug.Print(ex.ToString());
                Initiate(mud_address, port);
            }
        }