private static T1 GetFromServer <T1>(string sendMessage, string returnMessage, string serializedObject = "") where T1 : ServerObject, new()
        {
            ConnectionInfo serverConnectionInfo;

            if (ip == null)
            {
                return(default(T1));
            }
            try
            {
                serverConnectionInfo = new ConnectionInfo(ip, CommonInfo.ServerPort);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                //ShowMessage("Failed to parse the server IP and port. Please ensure it is correct and try again");
                return(default(T1));
            }

            try
            {
                TCPConnection serverConnection = TCPConnection.GetConnection(serverConnectionInfo);

                string serializedObj;
                if (serializedObject == "")
                {
                    serializedObj = serverConnection.SendReceiveObject <string>(sendMessage, returnMessage, 30000);
                }
                else
                {
                    serializedObj = serverConnection.SendReceiveObject <string, string>(sendMessage, returnMessage, 30000, serializedObject);
                }

                T1 obj = new T1();
                obj.DeserializeFromString(serializedObj);
                return(obj);
            }
            catch (CommsException e)
            {
                Console.WriteLine(e.ToString());
                /*AppendLineToChatHistory("Error: A communication error occurred while trying to send message to " + serverConnectionInfo + ". Please check settings and try again.");*/

                FailedToConnect?.Invoke();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                /*AppendLineToChatHistory("Error: A general error occurred while trying to send message to " + serverConnectionInfo + ". Please check settings and try again.");*/
            }

            return(default(T1));
        }
Esempio n. 2
0
        private void Button1_Click(object sender, EventArgs e)
        {
            ConnectionInfo connection = new ConnectionInfo(ServerEndPoint);
            TCPConnection  tcp        = TCPConnection.GetConnection(connection);
            var            s          = new Student
            {
                Name = "AAA",
                Age  = new Random().Next(0, 100)
            };
            var result = tcp.SendReceiveObject <Student, String>("sendstudent", "recivestudent", 5000, s);

            textBox1.Text += $"{DateTime.Now}>> student data is send:{result}\r\n>";
        }
Esempio n. 3
0
        /// <summary>
        /// Faz o request com o servidor e aguarda o retorno.
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        public void Request(string ip, int port)
        {
            try
            {
                // Cria o objeto ConnectionInfo com o IP e Porta recebidos
                ConnectionInfo connectionInfo = new ConnectionInfo(ip, port);

                // Cria o Get da conexão com o Servidor
                TCPConnection serverConnection = TCPConnection.GetConnection(connectionInfo);

                // Imprime no log as informações do Request
                PrintRequestMessage(ip, port);

                // Faz o request e aguarda o retorno do servidor
                string myCustomObject = serverConnection.SendReceiveObject <string>("RequestCustomObject", "CustomObjectReply", 3000);

                // Imprime no log a resposta do servidor
                PrintResponseMessage(ip, port, myCustomObject);
                response = "Conectado!";
            }
            catch (Exception err)
            {
                if (err is ExpectedReturnTimeoutException)
                {
                    string message = "Receive timed out after the 3000ms";
                    response = message;
                    PrintErrorMessage(ip, port, message);
                }
                else if (err is ConnectionSetupException)
                {
                    string message = "Falha ao conectar com o servidor de testes no endereço: " + ip + ":" + port;
                    response = message;
                    PrintErrorMessage(ip, port, message);
                }
                else if (err is ConnectionShutdownException)
                {
                    string message = "A comunicação com o Servidor não recebeu uma resposta";
                    response = message;
                    PrintErrorMessage(ip, port, message);
                }
                else
                {
                    PrintErrorMessage(ip, port, err.ToString());
                    response = err.ToString();
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Sends a request to add the specified stock record to the database. Produces error messages if failed. Waits
        /// for 10000ms.
        /// </summary>
        /// <param name="stockName">Name of new stock.</param>
        /// <param name="purchase">Purchase cost of new stock.</param>
        /// <param name="sell">Sell price of the item.</param>
        /// <param name="qty">Current quantity of the stock.</param>
        /// <returns>The success of the insert command.</returns>
        public bool InsertStock(string stockName, double purchase, double sell, int qty)
        {
            try
            {
                //Send a request to insert stock, expecting a confirmation.
                if (_connection.SendReceiveObject<StockRecord, bool>("InsertStockRecord", "ReturnInsertStockRecord", 10000,
                    new StockRecord(0, stockName, purchase, sell, qty)))
                {
                    View.SuccessNotify("Successfully added stock.", "Receipt successful");
                    return true;
                }
                //The server has failed to insert the stock in the database.
                else
                {
                    View.ErrorNotify("Data could not be added to the database.\n Check the server for further details.",
                        "Database Error");
                }
            }
            catch (ExpectedReturnTimeoutException exception)
            {
                View.ErrorNotify("No confirmation recieved from server.\n Likly a connection issue, check the server status.",
                    "Connection Error");
            }

            return false;
        }