⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 winchatform.cs

📁 WinChat was created in late 2000 by Spectre, DeMENted and Unique_T. You can connect to WinChat.Ne
💻 CS
📖 第 1 页 / 共 2 页
字号:
		private void menuItem8_Click(object sender, System.EventArgs e)
		{
			exit_program();
		}

		/* Function     : displayRemoteMessage()
		 * Return       : void
		 * Purpose      : This function will run as a thread to display the messages received from
		 *                the remote peer onto the remoteTextBox. 
		 * Algorithm    : It keeps checking whether the tcpUdpServer.tcpBytesReceived > 0 
		 *                (there is incoming data).  If so, append the tcpUdpServer.dataReceivedTcp
		 *                message to the remoteTextBox.  Since the remote peer might be sending the
		 *                message "notifyLastMessage" to the local server when it terminate the call,
		 *                this function needs to look for and skip this message (a temporary workaround).
		 */

		public void displayRemoteMessage()
		{
			while (true)
			{
				if (tcpUdpServer.tcpBytesReceived > 0)
				{
					remoteTextBox.AppendText("\r\n");

					//The existence of the following if statement is just a temporary workaround.
					//It will be removed from the next version.
					if (String.Compare(tcpUdpServer.dataReceivedTcp, ClientServerConstants.notifyLastMessage) != 0)
					{
						remoteTextBox.AppendText(tcpUdpServer.dataReceivedTcp + "\r\n");
					}
					tcpUdpServer.tcpBytesReceived = 0;
				}
			}
		}

		/* Function     : sendButton_Click(object sender, System.EventArgs e)
		 * Return       : void
		 * Purpose      : This function will is invoked when the "Send" button is pressed.  It
		 *                sends the message typed by the local peer in the typedMessage TextBox
		 *                to the remote server.
		 * Algorithm    : It sends the message over to the remote peer side, and then append
		 *                the typedMessage to the localTextBox.
		 */

		private void sendButton_Click(object sender, System.EventArgs e)
		{
			try
			{
				tcpUdpClient.tcpClientSendMessage(typedMessage.Text);

				localTextBox.AppendText("\r\n");
				localTextBox.AppendText(typedMessage.Text);
				typedMessage.Clear();
			}
			catch (Exception ex)
			{				
				MessageBox.Show("An Exception has occurred!" + ex.ToString(), "WinChat", 
					MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			}
		}

		/* Function     : exitButton_Click(object sender, System.EventArgs e)
		 * Return       : void
		 * Purpose      : This function will is invoked when the "Exit" button is pressed.  
		 * Algorithm    : When the Exit button is pressed, it first invokes the 
		 *                hangup_procedure() to hangup the call, then exit the program.
		 */

		private void exitButton_Click(object sender, System.EventArgs e)
		{
			this.hangup_procedure(whoIsCalling.exitButton);
			exit_program();
		}

		/* Function     : doneButton_Click(object sender, System.EventArgs e)
		 * Return       : void
		 * Purpose      : This function will is invoked when the "Hangup" Button is pressed.  
		 * Algorithm    : When the Exit button is pressed, it invokes the 
		 *                hangup_procedure() to hangup the call
		 */
		private void doneButton_Click(object sender, System.EventArgs e)
		{
			this.hangup_procedure(whoIsCalling.hangupButton);
		}

		/* Function     : exit_program()
		 * Return       : void
		 * Purpose      : This function will exit the program.  
		 * Algorithm    : This function first stops all the TcpListener and UDP socket, then
		 *                exits the application.
		 */
		private void exit_program()
		{
			tcpUdpServer.stop_servers();
			Application.Exit();
			MessageBox.Show("Thank you for using WinChat", "WinChat");
		}

		/* Function     : aboutMenuItem_Click(object sender, System.EventArgs e)
		 * Return       : void
		 * Purpose      : This function will be invoked when the About MenuItem is pressed.  
		 * Algorithm    : This function will show the AboutForm dialog.
		 */
		private void aboutMenuItem_Click(object sender, System.EventArgs e)
		{
			Form aboutForm = new AboutForm();

			aboutForm.ShowDialog();
		}

		/* Function     : notifyButton_Click(object sender, System.EventArgs e)
		 * Return       : void
		 * Purpose      : This function will be invoked when the "Notify" Button is pressed.  
		 * Algorithm    : This function handles the call request from the local peer.  It first
		 *                invokes tcpUdpClient.sendNotify() to send the notifyRequest to the remote
		 *                peer.  If the remote side responses positively (tcpUdpClient.sendNotify()
		 *                returns true), this function will invoke the callInitiation delegate (which
		 *                in turn invokes tcpUdpServer.OnCallInitiation()).  It will then attempt 
		 *                to create a TCP connection to the remote TCP server.
		 */
		private void notifyButton_Click(object sender, System.EventArgs e)
		{
			try
			{
				if (tcpUdpClient.sendNotify(hostNameTextBox.Text, ClientServerConstants.notifyRequest) == true)
					//If the remote side accepts the call.
				{
					this.typedMessage.Enabled = true;
					this.sendButton.Enabled = true;

					this.remoteMessageLabel.Text = (hostNameTextBox.Text).ToUpper() + "\'s Messages";

					isCallingSide = true;

					//Invoke the callInitiation event (which in turn invokes tcpUdpServer.OnCallInitiation())
					//so that the tcpUdpServer can proceed with appropriate actions.
					callInitiation();

					bool succeeded = false;
					bool giveup = false;
					int failCount = 0;

					while ((!succeeded) && (!giveup))
					{
						try
						{
							tcpUdpClient.createTcpClientConnection(hostNameTextBox.Text);

							succeeded = true;

							hostNameTextBox.Enabled = false;
							notifyButton.Enabled = false;

							doneButton.Enabled = true;
						}
						catch (Exception ex)
						{
							if (failCount < 3)
							{
								MessageBox.Show("An Exception has occured. \r\n" + ex.ToString(), 
									"WinChat", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

								MessageBox.Show("Cannot create connection to the server!  Press OK to try again.", 
									"WinChat", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
								failCount++;
							}
							else
							{
								MessageBox.Show("Cannot make connection to the server.", 
									"WinChat", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
								giveup = true;
							}
						}
					}
				}
				else //If the remote side does not want to talk.
				{
					MessageBox.Show("The remote side has rejected your chat request.", 
						"WinChat", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show("An Exception has occurred!" + ex.ToString(), "WinChat", 
					MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			}

		}


		/* Function     : hangup_procedure(whoIsCalling whoCallsMe)
		 * Return       : void
		 * Purpose      : This function will be hangup the call.  
		 * Algorithm    : This function hangs up the call according to whether the hanging-up side
		 *                is the calling side or the called side.  If it is the calling side, this 
		 *                function will use the hostname given in the "hostNameTextBox.Text" to 
		 *                send the notifyHangupRequest to the called side.  If it is the called side,
		 *                this function will use hostname given by "tcpUdpServer.remoteIpEndPoint"
		 *                (because the hostname is not given in "hostNameTextBox.Text") to send the 
		 *                notifyHangupRequest to the calling side.
		 *                It's noteworthy that this function has to send the notifyLastMessage to 
		 *                the remote peer to let it know that this is the last message and the remote
		 *                peer should close the TcpListener.
		 *                Finally, this function will invoke the callEnding() delegate so that the 
		 *                local tcpUdpServer can be notified of this event.
		 */

		private void hangup_procedure(whoIsCalling whoCallsMe)
		{
			this.typedMessage.Enabled = false;
			this.sendButton.Enabled = false;
			this.remoteMessageLabel.Text = "Remote Side\'s Message";

			try
			{
				//The side that pressed the Notify button (local side) can get the remote address from the 
				//hostNameTextBox.  The side receiving the Notify (remote side) has to get the remote address
				//from the remoteIpEndPoint stored in tcpUdpServer during Notification.
				if (isCallingSide == true)
				{
					//This is the calling side (the side that placed the call at the first place).
					if (tcpUdpClient.sendNotify(hostNameTextBox.Text, ClientServerConstants.notifyHangupRequest) == true)
					{
						//The notifyLastMessage is sent to the remote side to unblock the socket Receive call.
						tcpUdpClient.tcpClientSendMessage(ClientServerConstants.notifyLastMessage);

						doneButton.Enabled = false;

						tcpUdpClient.closeTcpClientConnection();
					}
					else
					{
						if (whoCallsMe == whoIsCalling.hangupButton)
						{
							//This message only gets printed out when it's not invoked from the Exit Button.
							MessageBox.Show("There might be probelm on the network.  Please try to hangup again.", 
								"WinChat", MessageBoxButtons.OK);
						}
					}

					this.isCallingSide = false;
				}
				else
				{
					//This is the called side (the side that answered the call at the first place).
					if (tcpUdpServer.remoteIpEndPoint != null)
					{
						//
						if (tcpUdpClient.sendNotify((Dns.GetHostByAddress(tcpUdpServer.remoteIpEndPoint.Address)).HostName, 
							ClientServerConstants.notifyHangupRequest) == true)
						{
							//The notifyLastMessage is sent to the remote side to unblock the socket Receive call.
							tcpUdpClient.tcpClientSendMessage(ClientServerConstants.notifyLastMessage);

							doneButton.Enabled = false;

							tcpUdpClient.closeTcpClientConnection();
				
						}
						else
						{
							if (whoCallsMe == whoIsCalling.hangupButton)
							{
								//This message only gets printed out when it's not invoked from the Exit Button.
								MessageBox.Show("There might be probelm on the network.  Please try to hangup again.", 
									"WinChat", MessageBoxButtons.OK);
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show("An Exception has occurred!" + ex.ToString(), "WinChat", 
					MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			}

			hostNameTextBox.Enabled = true;
			notifyButton.Enabled = true;

			//Invoke the call ending event to the tcpUdpServer.
			callEnding();
		}

		/* Function     : OnPeerNotify()
		 * Purpose      : This is the delegate to be called when the tcpUdpServer receives a notifyRequest.
		 * Algorithm    : This function will create the TCP connection to the remote peer.
		 * 
		 */
		public void OnPeerNotify()
		{
			this.hostNameTextBox.Enabled = false;
			this.notifyButton.Enabled = false;
			
			this.typedMessage.Enabled = true;
			this.sendButton.Enabled = true;

			this.doneButton.Enabled = true;
			
			this.remoteMessageLabel.Text = ((Dns.GetHostByAddress(this.tcpUdpServer.remoteIpEndPoint.Address)).HostName) + "\'s Messages";

			this.tcpUdpClient.createTcpClientConnection(
				(Dns.GetHostByAddress(this.tcpUdpServer.remoteIpEndPoint.Address)).HostName);

		}

		/* Function     : OnPeerHangup()
		 * Purpose      : This is the delegate to be called when the tcpUdpServer receives a notifyHangupRequest.
		 * Algorithm    : This function will close the TCP connection to the remote peer.
		 * 
		 */
		public void OnPeerHangup()
		{
			this.hostNameTextBox.Enabled = true;
			this.notifyButton.Enabled = true;
			
			this.typedMessage.Enabled = false;
			this.sendButton.Enabled = false;

			this.doneButton.Enabled = false;

			this.remoteMessageLabel.Text = "Remote Side\'s Messages";

			//The notifyHangupRequest is sent to the remote side to unblock the socket Receive call so that
			//we can close the TcpClient Connection afterward.
			tcpUdpClient.tcpClientSendMessage(ClientServerConstants.notifyLastMessage);

			tcpUdpClient.closeTcpClientConnection();
			
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -