private bool Acknowledge( SocketService socketService, SendOption sendOption, PacketHead packetHead, IWriteStream writeStream = null) { if (socketService == null) { Logger.Error($"{nameof(socketService)} 为 null !"); return(false); } var fragment = writeStream?.ToByteFragment(); packetHead.Length = fragment.HasValue ? (ushort)(PacketHead.GetSize() + fragment?.Count) : (ushort)PacketHead.GetSize(); packetHead.SendOption = sendOption; var ws = PoolAllocator <IWriteStream> .GetObject(); if (writeStream != null) { ws.ShiftRight(writeStream.ToByteFragment()); } Packet.ToBytes(packetHead, ws); var byteSegment = ws.ToByteFragment(); var result = StartWrite(socketService, byteSegment.Buffer, byteSegment.Offset, byteSegment.Count, ws, true); SocketStatistics.LogUnreliableSend(); SocketStatistics.LogTotalBytesSent(packetHead.Length); return(true); }
private void ResponseAck(EndPoint endPoint, IReadStream readStream, PacketHead packetHead) { if (!_connections.TryGetValue(endPoint, out var socketService)) { Logger.Error($"{endPoint}连接没有添加!"); return; } Logger.Info($"接收到{endPoint}心跳包!"); Acknowledge(socketService, SendOption.Acknowledge | SendOption.Ack, packetHead); SocketStatistics.LogAcknowledgementSend(); SocketStatistics.LogUnreliableReceive(); SocketStatistics.LogAcknowledgementSend(); SocketStatistics.LogAckReceive(); SocketStatistics.LogTotalBytesReceived(PacketHead.GetSize()); }
private void ResponseDisconnect(EndPoint endPoint, IReadStream readStream, PacketHead packetHead) { if (!_connections.TryGetValue(endPoint, out var socketService)) { Logger.Warn("断开没有记录的连接?"); return; } Logger.Info($"{endPoint}主动断开!"); Acknowledge(socketService, SendOption.Acknowledge | SendOption.Disconnect, packetHead); SocketStatistics.LogAcknowledgementSend(); SocketStatistics.LogUnreliableReceive(); SocketStatistics.LogTotalBytesReceived(PacketHead.GetSize()); OnDisconnect(socketService); }
/// <summary> /// 发送数据 /// </summary> private bool Write(SocketService socketService, ulong messageId, SendOption sendOption) { using (var writeStream = PoolAllocator <IWriteStream> .GetObject()) { var packetHead = new PacketHead { Length = (ushort)PacketHead.GetSize(), PacketId = messageId, SendOption = sendOption, TotalBytes = 0, FragmentId = 0, TotalFragments = 1 }; Packet.ToBytes(packetHead, writeStream); var byteFragment = writeStream.ToByteFragment(); var result = _writer.Client.SendTo(byteFragment.Buffer, byteFragment.Offset, byteFragment.Count, SocketFlags.None, socketService.Connection.RemoteAddress); socketService.OnWrite(result == byteFragment.Count); return(result == byteFragment.Count); } }
protected override bool WriteMessage( SocketService socketService, SendOption sendOption, ulong messageId, IWriteStream writeStream) { if (socketService == null) { Logger.Error($"{nameof(socketService)} 为 null !"); return(false); } var fragment = writeStream?.ToByteFragment(); var packetHead = new PacketHead { Length = fragment.HasValue ? (ushort)(PacketHead.GetSize() + fragment?.Count) : (ushort)PacketHead.GetSize(), PacketId = messageId, SendOption = sendOption, TotalBytes = fragment.HasValue ? (ushort)fragment?.Count : (ushort)0, FragmentId = 0, TotalFragments = 1 }; var ws = PoolAllocator <IWriteStream> .GetObject(); if (fragment.HasValue) { ws.ShiftRight((ByteFragment)fragment); } Packet.ToBytes(packetHead, ws); var byteSegment = ws.ToByteFragment(); var result = StartWrite(socketService, byteSegment.Buffer, byteSegment.Offset, byteSegment.Count, ws, true); SocketStatistics.LogUnreliableSend(); SocketStatistics.LogTotalBytesSent(packetHead.Length); return(result); }
private bool Write(SocketService socketService, IWriteStream writeStream, SendOption sendOption) { var fragment = writeStream?.ToByteFragment(); var packetHead = new PacketHead { Length = fragment.HasValue?(ushort)(PacketHead.GetSize() + fragment?.Count):(ushort)PacketHead.GetSize(), PacketId = socketService.SendCounter, SendOption = sendOption, TotalBytes = fragment.HasValue?(ushort)fragment?.Count:(ushort)0 }; var ws = PoolAllocator <IWriteStream> .GetObject(); if (fragment.HasValue) { ws.ShiftRight((ByteFragment)fragment); } Packet.ToBytes(packetHead, ws); var byteFragment = ws.ToByteFragment(); var result = _writer.Client.SendTo(byteFragment.Buffer, byteFragment.Offset, byteFragment.Count, SocketFlags.None, socketService.Connection.RemoteAddress); socketService.OnWrite(result == byteFragment.Count); return(result == byteFragment.Count); }
private bool Acknowledge( SocketService socketService, SendOption sendOption, PacketHead packetHead, IWriteStream writeStream = null) { var fragment = writeStream?.ToByteFragment(); packetHead.Length = fragment.HasValue ? (ushort)(PacketHead.GetSize() + fragment?.Count) : (ushort)PacketHead.GetSize(); packetHead.SendOption = sendOption; using (var ws = PoolAllocator <IWriteStream> .GetObject()) { if (writeStream != null) { ws.ShiftRight(writeStream.ToByteFragment()); } Packet.ToBytes(packetHead, ws); var bf = ws.ToByteFragment(); var result = _writer.Client.SendTo(bf.Buffer, bf.Offset, bf.Count, SocketFlags.None, socketService.Connection.RemoteAddress); socketService.OnWrite(result == bf.Count); return(result == bf.Count); } }
protected override bool WriteMessage( SocketService socketService, SendOption sendOption, IWriteStream writeStream) { if (socketService == null) { Logger.Error($"{nameof(socketService)} 为 null !"); return(false); } if (writeStream == null) { Logger.Error($"{nameof(writeStream)} 为 null !"); return(false); } var fragment = writeStream.ToByteFragment(); var dataLengnth = MTU - PacketHead.GetSize(); var messageId = socketService.SendCounter; // 分包 var splits = Math.Ceiling(fragment.Count * 1.0d / dataLengnth); for (var i = 0; i < splits; i++) { var packetHead = new PacketHead { Length = (ushort)(Math.Min(fragment.Count - i * dataLengnth, dataLengnth) + PacketHead.GetSize()), PacketId = messageId, SendOption = sendOption, TotalBytes = (ushort)Math.Min(fragment.Count - i * dataLengnth, dataLengnth), FragmentId = (ushort)i, TotalFragments = (ushort)splits }; var ws = PoolAllocator <IWriteStream> .GetObject(); ws.ShiftRight(fragment.Buffer, fragment.Offset + i * dataLengnth, packetHead.TotalBytes); Packet.ToBytes(packetHead, ws); var byteSegment = ws.ToByteFragment(); void Action() { StartWrite(socketService, byteSegment.Buffer, byteSegment.Offset, byteSegment.Count, ws); SocketStatistics.LogFragmentedSend(); SocketStatistics.LogDataBytesSent(packetHead.TotalBytes); SocketStatistics.LogTotalBytesSent(packetHead.Length); Logger.Info("重发数据包!"); } void RecycleAction() { ws.Dispose(); } var id = BuildID(packetHead); _fragmentTimer?.Add(id, Action, RecycleAction); StartWrite(socketService, byteSegment.Buffer, byteSegment.Offset, byteSegment.Count, writeStream); socketService.PacketIds.Add(id); SocketStatistics.LogFragmentedSend(); SocketStatistics.LogReliableSend(); SocketStatistics.LogDataBytesSent(packetHead.TotalBytes); SocketStatistics.LogTotalBytesSent(packetHead.Length); } return(true); }
/// <summary> /// 发送数据 /// </summary> public bool Write(SocketService socketService, IWriteStream writeStream) { if (socketService == null) { throw new ArgumentNullException(nameof(socketService)); } if (writeStream == null) { throw new ArgumentNullException(nameof(writeStream)); } var fragment = writeStream.ToByteFragment(); var dataLengnth = MTU - PacketHead.GetSize(); var messageId = socketService.SendCounter; // 分包 var splits = Math.Ceiling(fragment.Count * 1.0d / dataLengnth); for (var i = 0; i < splits; i++) { var packetHead = new PacketHead { Length = (ushort)(Math.Min(fragment.Count - i * dataLengnth, dataLengnth) + PacketHead.GetSize()), PacketId = messageId, SendOption = SendOption.Fragment, TotalBytes = (ushort)Math.Min(fragment.Count - i * dataLengnth, dataLengnth), FragmentId = (ushort)i, TotalFragments = (ushort)splits }; Console.WriteLine(i); var ws = PoolAllocator <IWriteStream> .GetObject(); ws.ShiftRight(fragment.Buffer, fragment.Offset + i * dataLengnth, packetHead.TotalBytes); Packet.ToBytes(packetHead, ws); var bf = ws.ToByteFragment(); _writer.Client.SendTo(bf.Buffer, bf.Offset, bf.Count, SocketFlags.None, socketService.Connection.RemoteAddress); void Action() { _writer.Client.SendTo(bf.Buffer, bf.Offset, bf.Count, SocketFlags.None, socketService.Connection.RemoteAddress); Logger.Info("重发数据包!"); } void RecycleAction() { ws.Dispose(); } var id = BuildID(packetHead); if (!_fragmentsTimer.Add(id, Action, RecycleAction)) { Logger.Error("添加重发消息失败!"); } socketService.PacketIds.Add(id); } return(true); }