示例#1
0
        public List <ConnectionInfo> GetConnections()
        {
            List <ConnectionInfo> otherConnectionInfos = new List <ConnectionInfo>();
            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(otherConnectionInfos);
                }

                if (serverConnectionInfo != null)
                {
                    otherConnectionInfos = (from current in NetworkComms.AllConnectionInfo() where current.RemoteEndPoint != serverConnectionInfo.RemoteEndPoint select current).ToList();
                }
                else
                {
                    otherConnectionInfos = NetworkComms.AllConnectionInfo();
                }
            }
            return(otherConnectionInfos);
        }
        private void sendMsg()
        {
            if (textBox1.Text.Length == 0)
            {
                return;
            }


            if (isHost == true)
            {
                messageHolder toSend = new messageHolder(host.name + ": " + textBox1.Text);
                ChatLog.Items.Add(host.name + ": " + textBox1.Text);
                List <ConnectionInfo> l = NetworkComms.AllConnectionInfo();

                foreach (ConnectionInfo i in l)
                {
                    TCPConnection.GetConnection(i).SendObject("Message", toSend);
                }
            }
            else
            {
                messageHolder toSend = new messageHolder(cinfo.name + ": " + textBox1.Text);
                ChatLog.Items.Add(cinfo.name + ": " + textBox1.Text);
                newTCPConn.SendObject("Message", toSend);
            }
            textBox1.Clear();
        }
示例#3
0
        public void SendForHelp()
        {
            try
            {
                NetworkComms.SendObject("Queue", serverIP, serverPort, name);

                List <ConnectionInfo> arr = NetworkComms.AllConnectionInfo();
                string connInfo           = "";
                string connIP             = "";
                int    connPort           = 0;

                foreach (ConnectionInfo conn in arr)
                {
                    connInfo = conn.ToString().Split(']').Last().Split('-').First().Trim();
                    connIP   = connInfo.Split(':').First();
                    connPort = int.Parse(connInfo.Split(':').Last().Trim());
                }

                //Start listening for incoming connections if not already doing so
                if (Connection.Listening(ConnectionType.TCP) == false)
                {
                    Connection.StartListening(ConnectionType.TCP, new System.Net.IPEndPoint(System.Net.IPAddress.Any, connPort));
                    dispatcherTimer.Start();
                }
            }
            catch (Exception)
            {
                _notifyIcon.BalloonTipIcon  = System.Windows.Forms.ToolTipIcon.Info;
                _notifyIcon.BalloonTipText  = "Error connecting to server. Please try again later.";
                _notifyIcon.BalloonTipTitle = "Error connecting.";
                _notifyIcon.ShowBalloonTip(3000);
                _notifyIcon.Text = "Error connecting.";
            }
        }
示例#4
0
        private static void TellClientToUpdateUsers(UserInfo userInfo)
        {
            string serializedUser = userInfo.SerializeToString();

            foreach (ConnectionInfo info in NetworkComms.AllConnectionInfo())
            {
                TCPConnection.GetConnection(info).SendObject(Messages.UserUpdated, serializedUser);
            }
        }
示例#5
0
        private static void TellClientToRemoveSneeze(SneezeRecord sneeze)
        {
            string serializedSneeze = sneeze.SerializeToString();

            foreach (ConnectionInfo info in NetworkComms.AllConnectionInfo())
            {
                TCPConnection.GetConnection(info).SendObject(Messages.SneezeRemoved, serializedSneeze);
            }
        }
示例#6
0
        /// <summary>
        /// Send our message.
        /// </summary>
        private void SendMessage()
        {
            //If we have tried to send a zero length string we just return
            if (messageText.Text.Trim() == "")
            {
                return;
            }

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

            if (serverIP.Text != "")
            {
                try { serverConnectionInfo = new ConnectionInfo(serverIP.Text.Trim(), int.Parse(serverPort.Text)); }
                catch (Exception)
                {
                    MessageBox.Show("Failed to parse the server IP and port. Please ensure it is correct and try again", "Server IP & Port Parse Error", MessageBoxButton.OK);
                    return;
                }
            }

            //We wrap everything we want to send in the ChatMessage class we created
            ChatMessage messageToSend = new ChatMessage(NetworkComms.NetworkIdentifier, localName.Text, messageText.Text, messageSendIndex++);

            //We add our own message to the message history in-case it gets relayed back to us
            lock (lastPeerMessageDict) lastPeerMessageDict[NetworkComms.NetworkIdentifier] = messageToSend;

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

            //We refresh the MessagesFrom box so that it includes our own name
            RefreshMessagesFromBox();

            //We clear the text within the messageText box.
            this.messageText.Text = "";

            //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("ChatMessage", messageToSend); }
                catch (CommsException) { MessageBox.Show("A CommsException occurred while trying to send message to " + serverConnectionInfo, "CommsException", MessageBoxButton.OK); }
            }

            //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
            var otherConnectionInfos = (from current in NetworkComms.AllConnectionInfo() where current != serverConnectionInfo select current).ToArray();

            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("ChatMessage", messageToSend); }
                catch (CommsException) { MessageBox.Show("A CommsException occurred while trying to send message to " + info, "CommsException", MessageBoxButton.OK); }
            }
        }
示例#7
0
        /// <summary>
        /// Writes the provided sneeze to the console window
        /// </summary>
        private static void HandleSneeze(PacketHeader header, Connection connection, string serializedSneeze)
        {
            SneezeRecord sneeze = new SneezeRecord();

            sneeze.DeserializeFromString(serializedSneeze);
            database.Sneezes.Add(sneeze);
            database.Save();

            foreach (ConnectionInfo info in NetworkComms.AllConnectionInfo())
            {
                TCPConnection.GetConnection(info).SendObject(Messages.PersonSneezed, serializedSneeze);
            }
        }
示例#8
0
            /// <summary>
            /// Send a message to clients
            /// </summary>
            private void SendMessage()
            {
                if (messageText.Text.Trim() == "")
                {
                    return;
                }
                Message messageToSend = new Message(NetworkComms.NetworkIdentifier, localName.Text, "", messageText.Text, messageSendIndex++);

                lock (lastPeerMessageDict) lastPeerMessageDict[NetworkComms.NetworkIdentifier] = messageToSend;
                AppendLineToRichChatBox("[" + (DateTime.Now).ToString(hourFormat) + "] " + messageToSend.SourceName + " : " + messageToSend.MessageContent);
                RefreshMessagesFromBox();
                this.messageText.Text = "";
                var otherConnectionInfos = (from current in NetworkComms.AllConnectionInfo() select current).ToArray();

                foreach (ConnectionInfo info in otherConnectionInfos)
                {
                    try { TCPConnection.GetConnection(info).SendObject("Message", messageToSend); }
                    catch (CommsException) { MessageBox.Show("A CommsException occurred while trying to send message to " + info, "CommsException", MessageBoxButton.OK); }
                }
            }
示例#9
0
        private void refreshConns()
        {
            connectionsBox.Clear();
            List <ConnectionInfo> arr = NetworkComms.AllConnectionInfo();

            foreach (ConnectionInfo conn in arr)
            {
                if (conn.ToString().Length > 43)
                {
                    string connInfo = conn.ToString().Split('>').Last().Trim();
                    string connIP   = connInfo.Split(':').First();
                    int    connPort = int.Parse(connInfo.Split(':').Last().Split('(').First().Trim());

                    connectionsBox.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        connectionsBox.AppendText(connIP + ":" + connPort + "\n");
                    }));
                }
            }
        }
示例#10
0
            /// <summary>
            /// Send message to clients
            /// </summary>
            private void SendMessage()
            {
                if (messageText.Text.Trim() == "")
                {
                    return;
                }
                Message messageToSend = new Message(NetworkComms.NetworkIdentifier, username, "CHAT", messageText.Text, messageSendIndex++);

                lock (lastPeerMessageDict) lastPeerMessageDict[NetworkComms.NetworkIdentifier] = messageToSend;
                AppendLineToRichChatBox("[" + (DateTime.Now).ToString("HH:mm:ss dd/MM/yyyy") + "] " + messageToSend.SourceName + " : " + messageToSend.MessageContent);
                this.messageText.Text = "";

                if (serverConnectionInfo != null)
                {
                    try
                    {
                        TCPConnection.GetConnection(serverConnectionInfo).SendObject("Message", messageToSend);
                    }
                    catch (CommsException)
                    {
                        MessageBox.Show("A CommsException occurred while trying to send message to " + serverConnectionInfo, "CommsException", MessageBoxButton.OK);
                    }
                }

                var otherConnectionInfos = (from current in NetworkComms.AllConnectionInfo() where current != serverConnectionInfo select current).ToArray();

                foreach (ConnectionInfo info in otherConnectionInfos)
                {
                    try
                    {
                        TCPConnection.GetConnection(info).SendObject("Message", messageToSend);
                    }
                    catch (CommsException)
                    {
                        MessageBox.Show("A CommsException occurred while trying to send message to " + info, "CommsException", MessageBoxButton.OK);
                    }
                }
            }
示例#11
0
        /// <summary>
        /// Send a message.
        /// </summary>
        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 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;
                }
            }

            //We wrap everything we want to send in the ChatMessage class we created
            ChatMessage chatMessage = new ChatMessage(NetworkComms.NetworkIdentifier, LocalName, stringToSend, messageSendIndex++);

            //We add our own message to the message history in case it gets relayed back to us
            lock (lastPeerMessageDict) lastPeerMessageDict[NetworkComms.NetworkIdentifier] = chatMessage;

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

            //Clear the input box text
            ClearInputLine();

            //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
                {
                    if (ConnectionType == ConnectionType.TCP)
                    {
                        TCPConnection.GetConnection(serverConnectionInfo).SendObject("ChatMessage", chatMessage);
                    }
                    else if (ConnectionType == ConnectionType.UDP)
                    {
                        UDPConnection.GetConnection(serverConnectionInfo, UDPOptions.None).SendObject("ChatMessage", chatMessage);
                    }
                    else
                    {
                        throw new Exception("An invalid connectionType is set.");
                    }
                }
                catch (CommsException) { AppendLineToChatHistory("Error: A communication error occurred while trying to send message to " + serverConnectionInfo + ". Please check settings and try again."); }
                catch (Exception) { AppendLineToChatHistory("Error: A general error occurred while trying to send message to " + serverConnectionInfo + ". Please check settings and try again."); }
            }

            //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 (serverConnectionInfo != null)
            {
                otherConnectionInfos = (from current in NetworkComms.AllConnectionInfo() where current.RemoteEndPoint != serverConnectionInfo.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
                {
                    if (ConnectionType == ConnectionType.TCP)
                    {
                        TCPConnection.GetConnection(info).SendObject("ChatMessage", chatMessage);
                    }
                    else if (ConnectionType == ConnectionType.UDP)
                    {
                        UDPConnection.GetConnection(info, UDPOptions.None).SendObject("ChatMessage", chatMessage);
                    }
                    else
                    {
                        throw new Exception("An invalid connectionType is set.");
                    }
                }
                catch (CommsException) { AppendLineToChatHistory("Error: A communication error occurred while trying to send message to " + info + ". Please check settings and try again."); }
                catch (Exception) { AppendLineToChatHistory("Error: A general error occurred while trying to send message to " + info + ". Please check settings and try again."); }
            }

            return;
        }
示例#12
0
文件: GVBase.cs 项目: Katsukid/WPF
        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;
        }
示例#13
0
 /// <summary>
 /// Returns a ConnectionInfo array containing information for all connections
 /// 获取现存所有的连接,不包括已经关闭的连接
 /// </summary>
 /// <returns></returns>
 public static List <ConnectionInfo> GetAllConnectionInfo()
 {
     return(NetworkComms.AllConnectionInfo(false));
 }