public void SendStart() { lock (lockObject) { // 전송이 아직 완료된 상태가 아니므로 데이터만 가져오고 큐에서 제거하진 않는다. IRequestItem packet = sendQueue.Peek(); //총 크기 byte[] headerBuffer = packet.RequestPacketHearder; byte[] buffer = new byte[headerBuffer.Length + packet.PacketBuffer.Length]; // 이번에 보낼 패킷 사이즈 만큼 버퍼 크기를 설정하고 SendArgs.SetBuffer(SendArgs.Offset, buffer.Length); //Copy Buffer.BlockCopy(headerBuffer, 0, buffer, 0, headerBuffer.Length); Buffer.BlockCopy(packet.PacketBuffer, 0, buffer, headerBuffer.Length, packet.PacketBuffer.Length); // 패킷 내용을 SocketAsyncEventArgs버퍼에 복사한다. Buffer.BlockCopy(buffer, 0, SendArgs.Buffer, SendArgs.Offset, buffer.Length); // 비동기 전송 시작. bool pending = Socket.SendAsync(SendArgs); if (!pending) { SendProcess(SendArgs); } } }
public Task <int> SendAsync(byte[] buffer, int offset, int count) { var tcs = new TaskCompletionSource <int>(); SendArgs.SetBuffer(buffer, offset, count); SendArgs.UserToken = tcs; Socket.SendAsync(SendArgs); return(tcs.Task); }
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); } }