Esempio n. 1
0
 /// <summary>
 /// Check is a connection to the server is successful.
 /// </summary>
 /// <returns>Returns whether the connection to the server is successful.</returns>
 public Boolean isOnline()
 {
     using (SocketHandler sHandler = new SocketHandler(ip, port)) {
         return(sHandler.connect());
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Sends a command to the server and returns a custom CmdReply object.
        /// </summary>
        /// <typeparam name="T">Command reply payload value type.</typeparam>
        /// <param name="command">String representing the command name to be executed.</param>
        /// <param name="args">Any method parameters needed for commands.</param>
        /// <param name="address">Optional address parameter.</param>
        /// <param name="pwd">Optional password parameter.</param>
        /// <returns>Custom CmdReply object.</returns>
        public CmdReply <T> getReply <T>(String command, Dictionary <String, Object> args,
                                         String address = null, String pwd = null)
        {
            if (!commands.ContainsKey(command))
            {
                throw new Exceptions.InvalidArgumentException("Invalid command provided!");
            }

            Dictionary <String, Object> temp = new Dictionary <String, Object>();

            temp.Add("cmd", command);

            address = (address == null) ? this.address : address;
            pwd     = (pwd == null) ? this.pwd : pwd;

            foreach (String arg in commands[command])
            {
                if (arg == "addr")
                {
                    temp.Add("addr", address);
                }
                else if (arg == "pwd")
                {
                    temp.Add("pwd", pwd);
                }
                else
                {
                    if (!args.ContainsKey(arg))
                    {
                        throw new Exceptions.InvalidArgumentException("Invalid command argument provided.");
                    }
                    temp.Add(arg, args[arg]);
                }
            }

            CmdReply <T> cmd = new CmdReply <T>();
            String       re  = String.Empty;

            using (SocketHandler sHandler = new SocketHandler(ip, port)) {
                if (sHandler.connect())
                {
                    sHandler.send(JsonConvert.SerializeObject(temp));
                    while (true)
                    {
                        String str = String.Empty;
                        str = sHandler.receive();
                        if (str != String.Empty)
                        {
                            re += str;
                        }
                        try {
                            cmd = JsonConvert.DeserializeObject <CmdReply <T> >(re);
                            break;
                        } catch (Exception ohShit) {
                            // Do nothing... received data is incomplete...
                        }
                    }
                }
            }
            return(cmd);
        }