public Task <int> SendAsync(byte[] buffer, int offset, int count) { var tcs = new TaskCompletionSource <int>(); SendArgs.SetBuffer(buffer, offset, count); SendArgs.UserToken = tcs; if (!Socket.SendAsync(SendArgs)) { return(Task.FromResult(SendArgs.BytesTransferred)); } return(tcs.Task); }
protected override void Send(byte[] buffer) { try { //设置发送内容 SendArgs.SetBuffer(buffer, 0, buffer.Length); //异步发送 如果没有回调事件 直接处理 if (!Socket.SendToAsync(SendArgs)) { SendCompleted(); } } catch (Exception e) { Console.WriteLine("UToken-send : " + e.Message); } }
/// <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; } }
private void Send(object o) { SendArgs arg = o as SendArgs; FileTransmiter.SupperSend(FileTransmiter.RealEndPoint, arg.File, arg.Worker, new Action <string, int, bool, double>(ReportStatus), new Action <string, long>(ReportSpeed)); }
/// <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); })); } }