Exemplo n.º 1
0
        public void SendMessage(string numValue, string strValue)
        {
            //If we have tried to send a zero length string we just return
            if (strValue.Trim() == "" && numValue.Trim() == "")
            {
                return;
            }

            //We may or may not have entered some server connection information
            ConnectionInfo serverConnectionInfo = null;

            if (ServerIPAddress != "")
            {
                try { serverConnectionInfo = new ConnectionInfo(ServerIPAddress, ServerPort); }
                catch (Exception)
                {
                    ShowMessage("Failed to parse the server IP and port. Please ensure it is correct and try again");
                    return;
                }
            }
            // Check if input value is number
            int value = 0;

            int.TryParse(numValue, out value);
            //We wrap everything we want to send in the ChatMessage class we created
            DataSendModel message = new DataSendModel(NetworkComms.NetworkIdentifier, LocalName, value, strValue);

            //We write our own message to the chatBox
            //AppendLineToChatHistory(message.SourceName + " - " + message.Message);

            //If we provided server information we send to the server first
            if (serverConnectionInfo != null)
            {
                //We perform the send within a try catch to ensure the application continues to run if there is a problem.
                try
                {
                    TCPConnection.GetConnection(serverConnectionInfo).SendObject("ClientMessage", message);
                }
                catch (CommsException) { }
                catch (Exception) { }
            }
            return;
        }
Exemplo n.º 2
0
        public void SendMessage(string stringToSend)
        {
            //If we have tried to send a zero length string we just return
            if (stringToSend.Trim() == "")
            {
                return;
            }

            //We may or may not have entered some server connection information
            ConnectionInfo clientConnectionInfo = null;

            if (ServerIPAddress != "")
            {
                try { clientConnectionInfo = new ConnectionInfo(ServerIPAddress, ServerPort); }
                catch (Exception)
                {
                    ShowMessage("Failed to parse the server IP and port. Please ensure it is correct and try again");
                    return;
                }
            }
            // Check if input value is number
            int value = 0;

            if (!int.TryParse(stringToSend, out value))
            {
            }
            //We wrap everything we want to send in the ChatMessage class we created
            DataSendModel message = new DataSendModel(NetworkComms.NetworkIdentifier, LocalName, value);

            //We write our own message to the chatBox
            //AppendLineToChatHistory(message.SourceName + " - " + message.Message);

            //If we provided server information we send to the server first
            if (clientConnectionInfo != null)
            {
                //We perform the send within a try catch to ensure the application continues to run if there is a problem.
                try
                {
                    TCPConnection.GetConnection(clientConnectionInfo).SendObject("ServerMessage", message);
                    var         x         = clientConnectionInfo.RemoteEndPoint.ToString();
                    var         temp      = x.Split(':');
                    ClientModel newClient = new ClientModel()
                    {
                        IP     = temp[0],
                        Port   = int.Parse(temp[1]),
                        Name   = message.SourceName,
                        Value  = 0,
                        Value2 = "Empty"
                    };
                    bool checkExsisted = false;
                    foreach (var i in ls)
                    {
                        if (newClient.IP == i.IP && newClient.Port == i.Port)
                        {
                            checkExsisted = true;
                        }
                    }
                    if (!checkExsisted || ls.Count == 0)
                    {
                        ls.Add(newClient);
                        DisplayValuesFromClient(ls);
                    }
                }
                catch (CommsException e) { }
                catch (Exception) { }
            }

            //If we have any other connections we now send the message to those as well
            //This ensures that if we are the server everyone who is connected to us gets our message
            //We want a list of all established connections not including the server if set
            List <ConnectionInfo> otherConnectionInfos;

            if (clientConnectionInfo != null)
            {
                otherConnectionInfos = (from current in NetworkComms.AllConnectionInfo() where current.RemoteEndPoint != clientConnectionInfo.RemoteEndPoint select current).ToList();
            }
            else
            {
                otherConnectionInfos = NetworkComms.AllConnectionInfo();
            }

            foreach (ConnectionInfo info in otherConnectionInfos)
            {
                //We perform the send within a try catch to ensure the application continues to run if there is a problem.
                try
                {
                    TCPConnection.GetConnection(info).SendObject("ServerMessage", message);
                }
                catch (CommsException) { NetworkComms.AllConnectionInfo().Remove(info); }
                catch (Exception) {  }
            }

            return;
        }