public override async Task SendAsync(Packet packet) { try { await sendSemaphore.WaitAsync(); SendParser.WriteBuffer(packet); if (!netStream.CanWrite) { return; } while (SendParser.Buffer.DataSize > 0) { CancellationTokenSource cancel = new CancellationTokenSource(3000); cancel.Token.Register(() => { if (Connected) { OnError?.Invoke(this, SocketError.SocketError); } }, false); await netStream.WriteAsync(SendParser.Buffer.First, SendParser.Buffer.FirstOffset, SendParser.Buffer.FirstCount, cancel.Token); SendParser.Buffer.UpdateRead(SendParser.Buffer.FirstCount); } } catch { OnError?.Invoke(this, SocketError.SocketError); } finally { sendSemaphore.Release(); } }
/// <summary> /// 写入发送包到缓冲区队列(合并发送) /// </summary> /// <param name="packet"></param> public override void WriteSendBuffer(Packet packet) { SendParser.WriteBuffer(packet); }