HaoYueTunnel/Client-GUI/MainUI.cs
2024-04-26 17:51:55 +08:00

348 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using ClientCore;
using ClientCore.Data;
using ClientCore.Enum;
using ClientCore.Event;
using ClientCore.Network;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
namespace Client_GUI
{
public partial class MainUI : Form
{
public delegate void DSetLogStr(List<string> value);
public delegate void DSetStr(string value);
public delegate void DSetBool(bool value);
public delegate void DSetVoid();
const int LogLimit = 200;
const int ChatShowLimit = 200;
long mCurrUID = 0;
public MainUI()
{
InitializeComponent();
}
private void MainUI_Load(object sender, EventArgs e)
{
//注册事件
EventSystem.Instance.RegisterEvent(EEvent.UserLogin, OnUserLogin);
EventSystem.Instance.RegisterEvent(EEvent.TcpTunnelHelloResp, OnTcpTunnelHelloResp);
EventSystem.Instance.RegisterEvent<long>(EEvent.TcpTunnelP2PStateUpdate, OnTcpTunnelP2PStateUpdate);
EventSystem.Instance.RegisterEvent<long, string>(EEvent.UserJoin, OnUserJoin);
EventSystem.Instance.RegisterEvent<long, string>(EEvent.UserLeave, OnUserLeave);
EventSystem.Instance.RegisterEvent<string, string>(EEvent.OnChatMsg, OnChatMsg);
EventSystem.Instance.RegisterEvent<long, string>(EEvent.OnP2PChatMsg, OnP2PChatMsg);
EventSystem.Instance.RegisterEvent<int>(EEvent.OnFileConfirmChoice, OnFileConfirmChoice);
//EventSystem.Instance.RegisterEvent<long>(EEvent.OnFilePushStart, );
//EventSystem.Instance.RegisterEvent<long>(EEvent.OnFilePushEnd, );
System.Timers.Timer bTimer = new System.Timers.Timer();
bTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimeEvent_Log);
// 设置引发时间的时间间隔 此处设置为1秒(1000毫秒)
bTimer.Interval = 500;
bTimer.Enabled = true;
App.Init("127.0.0.1", 23846, 23847, OnNetLog);
//App.Init("main.axibug.com", 23846, 23847, OnNetLog);
if (App.Connect())
{
App.login.Login(StaticComm.LoginName);
}
UpdateP2PTargetState();
}
#region
void OnNetLog(int LogLevel, string msg)
{
AddLog(msg);
}
void OnUserLogin()
{
AddLog($"[User]登录成功");
App.userMgr.Send_GetUserList();
App.clientMgr.ConnectTcpTunnelServer();
}
void OnTcpTunnelHelloResp()
{
AddLog($"[TcpTunnel]OnTcpTunnelHelloResp");
}
void OnTcpTunnelP2PStateUpdate(long UID)
{
AddLog($"[TcpTunnel]OnTcpTunnelP2PStateUpdate");
if (UID == mCurrUID)
{
UpdateP2PTargetState();
}
else
{
mCurrUID = UID;
UpdateP2PTargetState();
}
}
void OnUserJoin(long UID, string NickName)
{
AddLog($"[User]用户{NickName}上线");
AddChat($"[{NickName}]上线");
UpdateOnlineUser();
}
void OnUserLeave(long UID, string NickName)
{
AddLog($"[User]用户{UID}下线");
AddChat($"[{NickName}]离开");
UpdateOnlineUser();
}
void OnChatMsg(string str1, string str2)
{
AddLog($"[Chat]{str1}:{str2}");
AddChat($"[{str1}]: {str2}");
}
void OnP2PChatMsg(long uid, string str2)
{
AddLog($"[P2PChatMsg]{uid}:{str2}");
}
void OnFileConfirmChoice(int TaskID)
{
DialogResult MsgBoxResult;//设置对话框的返回值
MsgBoxResult = System.Windows.Forms.MessageBox.Show("是否接受文件?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2);//定义对话框的按钮式样
if (MsgBoxResult.ToString() == "Yes")//如果对话框的返回值是YES按"Y"按钮)
{
//选择了Yes继续
App.p2pFile.receiver_AgreeRecvFile(TaskID, 1);
}
if (MsgBoxResult.ToString() == "No")//如果对话框的返回值是NO按"N"按钮)
{
//选择了No
App.p2pFile.receiver_AgreeRecvFile(TaskID, 0);
}
}
void OnFilePushStart(long TaskID)
{
AddLog($"[P2PFile]文件传输开始");
}
#endregion
#region
#endregion
void AddChat(string str)
{
this.BeginInvoke(new DSetStr(AddChat_delegate), str);
}
void AddChat_delegate(string str)
{
this.listBox_AllChatMsg.Items.Add(str);
while (listbox_log.Items.Count > ChatShowLimit)
{
listbox_log.Items.RemoveAt(listbox_log.Items.Count - ChatShowLimit);
}
}
void UpdateOnlineUser()
{
this.BeginInvoke(new DSetVoid(UpdateOnlineUser_delegate));
}
Dictionary<long, long> DictUserShowIdx2UID = new Dictionary<long, long>();
void UpdateOnlineUser_delegate()
{
DictUserShowIdx2UID.Clear();
UserInfo[] ulist = App.userMgr.GetUserInfo();
Console.WriteLine("User总数" + ulist.Length);
this.listBox_OnlineUser.Items.Clear();
for (int i = 0; i < ulist.Length; i++)
{
DictUserShowIdx2UID[i] = ulist[i].UID;
this.listBox_OnlineUser.Items.Add(ulist[i].NickName);
}
}
void UpdateP2PTargetState()
{
this.BeginInvoke(new DSetVoid(UpdateP2PTargetState_delegate));
}
void UpdateP2PTargetState_delegate()
{
bool bCanUse = false;
string StateName = "";
if (mCurrUID <= 0)
{
this.label_P2P_TargetUName.Text = "未选择";
}
else
{
App.clientMgr.GetP2PTargetState(mCurrUID, out E_P2P_STATE state);
switch (state)
{
case E_P2P_STATE.None:
StateName = "未连接";
break;
case E_P2P_STATE.Doing:
StateName = "打洞中……";
break;
case E_P2P_STATE.Fail:
StateName = "打洞失败";
break;
case E_P2P_STATE.Success:
StateName = "隧道已连通";
bCanUse = true;
break;
}
}
this.label_P2P_State.Text = StateName;
if (bCanUse)
{
btn_P2P_SendFile.Enabled = true;
btn_P2P_ChangeSendFile.Enabled = true;
}
else
{
btn_P2P_SendFile.Enabled = false;
btn_P2P_ChangeSendFile.Enabled = false;
}
}
#region
static Queue<string> logQueue = new Queue<string>();
public static void AddLog(string val)
{
logQueue.Enqueue($"{DateTime.Now.ToString("HH:mm:ss")}> {val}");
}
private void TimeEvent_Log(object source, System.Timers.ElapsedEventArgs e)
{
List<string> input = new List<string>();
while (logQueue.Count > 0)
{
string str = logQueue.Dequeue();
input.Add(str);
}
if (input.Count < 1)
return;
this.BeginInvoke(new DSetLogStr(ShowLog), input);
}
void ShowLog(List<string> value)
{
foreach (var v in value)
{
this.listbox_log.Items.Add(v);
}
while (listbox_log.Items.Count > LogLimit)
{
listbox_log.Items.RemoveAt(listbox_log.Items.Count - LogLimit);
}
//TODO 最下方
//listbox_log.ScrollIntoView(listbox_log.Items[listbox_log.Items.Count - 1]);
}
#endregion
private void btn_chatsend_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox_chatsendmsg.Text))
return;
App.chat.SendChatMsg(textBox_chatsendmsg.Text);
textBox_chatsendmsg.Text = string.Empty;
}
private void listBox_OnlineUser_DoubleClick(object sender, EventArgs e)
{
ListBox listBox = sender as ListBox;
if (listBox == null)
return;
// 当ListBox有选中项时
if (listBox.SelectedIndex <= -1)
return;
if (!DictUserShowIdx2UID.ContainsKey(listBox.SelectedIndex))
return;
long Uid = DictUserShowIdx2UID[listBox.SelectedIndex];
if (Uid == App.userMgr.MainPlayer.UID)
{
MessageBox.Show("不能和自己建立P2P隧道");
return;
}
if (mCurrUID == Uid)
return;
UserInfo uinfo = App.userMgr.GetUserByUid(Uid);
//已有
if (App.clientMgr.GetP2PTargetState(Uid, out E_P2P_STATE state))
{
mCurrUID = Uid;
this.label_P2P_TargetUName.Text = uinfo.NickName;
this.tabControl_Main.SelectedIndex = 1;
UpdateP2PTargetState();
}
else
{
if (uinfo != null)
{
mCurrUID = Uid;
this.label_P2P_TargetUName.Text = uinfo.NickName;
this.tabControl_Main.SelectedIndex = 1;
App.p2ptcp.SendDoTunnel(Convert.ToInt64(uinfo.UID));
}
}
}
private void btn_P2P_ChangeSendFile_Click(object sender, EventArgs e)
{
// 1. 打开文件管理器选择文件
OpenFileDialog openFileDialog1 = new OpenFileDialog(); //显示选择文件对话框
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "All files (*.*)|*.*"; //所有的文件格式
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
// 2. 查看可执行文件路径
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.label_P2P_SendFilePath.Text = openFileDialog1.FileName;
}
}
private void btn_P2P_SendFile_Click(object sender, EventArgs e)
{
string filePath = this.label_P2P_SendFilePath.Text;
if (string.IsNullOrEmpty(filePath))
{
MessageBox.Show("未选择文件");
return;
}
if (!File.Exists(filePath))
{
MessageBox.Show("选择的文件不存在");
return;
}
App.p2pFile.sender_FilePushConfirmToTarget(mCurrUID, filePath);
}
}
}