/// <summary> /// Close connect /// </summary> public void CloseConnect() { if (timerSendStream != null) { timerSendStream.Close(); timerSendStream = null; } if (uDPForwarder != null) { uDPForwarder.Stop(); uDPForwarder = null; } if (timerCheckReviceState != null) { timerCheckReviceState.Close(); timerCheckReviceState = null; } if (webSocket != null && (webSocket.State != WebSocketState.Closed || webSocket.State != WebSocketState.Closing)) { if (timerCreateConncet != null) { timerCreateConncet.Close(); timerCreateConncet = null; } webSocket.Close(); webSocket = null; } if (timerTestNetworkLatency != null) { timerTestNetworkLatency.Close(); timerTestNetworkLatency = null; } }
/// <summary> /// Forward stream to game /// </summary> private void SendThStreamToGame(object source, ElapsedEventArgs elapsedEventArgs, object sourceArgs) { SendArgs args = sourceArgs as SendArgs; if (uDPForwarder == null) { uDPForwarder = new UDPForwarder(localPoint, args.remote_address); } else { uDPForwarder.Stop(); uDPForwarder.ChangePoint(localPoint, args.remote_address); } uDPForwarder.SendToRemote(args.buffer); uDPForwarder.Start(); if (uDPForwarder.State == UDPForwarder.RecviceState.Running) { timerSendStream.Close(); timerSendStream = null; } }
/// <summary> /// Receive message from server /// </summary> /// <param name="sender"></param> /// <param name="args"></param> public void Websocket_MessageReceived(object sender, MessageReceivedEventArgs args) { if (args.Message.Length < 1) { return; } //Console.WriteLine(args.Message); string[] msgData = args.Message.Split(' '); if (msgData.Length > 1) { string state = msgData[0]; string[] ipData = msgData[1].Split(':'); if (ipData.Length < 2) { Form1.MainForm.Invoke(new Action(() => { Form1.MainForm.MetroLabelState.Text = "申请IP失败"; Form1.MainForm.MetroLabelMessage.Text = "没有申请到 IP"; Form1.MainForm.ShowToolTip(Form1.MainForm.MetroLabelMessage, Form1.MainForm.MetroLabelMessage.Text); })); return; } serverIP = ipData[0]; serverPort = ipData[1]; Form1.MainForm.Invoke(new Action(() => { switch (state.ToUpper()) { case "LISTEN": if (uDPForwarder != null) { uDPForwarder.Stop(); uDPForwarder = null; } state = "等待连接"; Form1.MainForm.metroTextBox1.Text = $"{serverIP}:{serverPort}"; Form1.MainForm.MetroLabelMessage.Text = $"正在等待少女连接IP地址:{serverIP},端口:{serverPort}"; Form1.MainForm.ShowToolTip(Form1.MainForm.MetroLabelMessage, Form1.MainForm.MetroLabelMessage.Text); break; case "CONNECT": if (timerSendStream != null) { timerSendStream.Close(); timerSendStream = null; } if (timerCheckReviceState != null) { timerCheckReviceState.Enabled = false; } state = "少女连接中"; Form1.MainForm.MetroLabelMessage.Text = "等待少女加入游戏..."; Form1.MainForm.ShowToolTip(Form1.MainForm.MetroLabelMessage, Form1.MainForm.MetroLabelMessage.Text); byte[] buffer = CreateThNetworkStream(Local_port, Convert.ToUInt16(serverPort)); SendArgs sendArgs = new SendArgs { buffer = buffer, remote_address = new IPEndPoint(IPAddress.Parse(serverIP), Convert.ToUInt16(serverPort)) }; timerSendStream = CreateTimer(SendThStreamToGame, sendArgs, 200, true); break; case "CONNECTED": if (uDPForwarder != null && uDPForwarder.State == UDPForwarder.RecviceState.Running) { if (timerCheckReviceState != null) { timerCheckReviceState.Enabled = true; reCheckStateCount = 0; } state = "连接成功"; Form1.MainForm.MetroLabelMessage.Text = "少女加入了游戏DA☆ZE!"; Form1.MainForm.ShowToolTip(Form1.MainForm.MetroLabelMessage, Form1.MainForm.MetroLabelMessage.Text); } else { if (timerCheckReviceState != null) { timerCheckReviceState.Enabled = false; } state = "等待连接"; Form1.MainForm.MetroLabelMessage.Text = $"正在等待少女连接IP地址:{serverIP},端口:{serverPort}"; Form1.MainForm.ShowToolTip(Form1.MainForm.MetroLabelMessage, Form1.MainForm.MetroLabelMessage.Text); } break; default: Form1.MainForm.MetroLabelMessage.Text = args.Message; break; } Form1.MainForm.MetroLabelState.Text = state; })); } else { Form1.MainForm.Invoke(new Action(() => { Form1.MainForm.MetroLabelState.Text = "服务器错误"; Form1.MainForm.MetroLabelMessage.Text = args.Message; Form1.MainForm.ShowToolTip(Form1.MainForm.MetroLabelMessage, Form1.MainForm.MetroLabelMessage.Text); })); } }