public void EncryptAES(IWriteStream writeStream, string sharedSecret) { if (!IsEncrypt) { return; } using (var aesAlg = new RijndaelManaged()) { var key = new Rfc2898DeriveBytes(sharedSecret, _salt); aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8); var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); var byteFragment = writeStream.ToByteFragment(); using (var resultStream = new MemoryStream()) { using (var csEncrypt = new CryptoStream(resultStream, encryptor, CryptoStreamMode.Write)) { csEncrypt.Write(byteFragment.Buffer, byteFragment.Offset, byteFragment.Count); } writeStream.Reset(); writeStream.ShiftLeft(aesAlg.IV); var ivLength = (ushort)aesAlg.IV.Length; writeStream.ShiftLeft(ivLength); writeStream.ShiftRight((ushort)byteFragment.Count); var bytes = resultStream.ToArray(); writeStream.ShiftRight(bytes); } } }
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); }
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); } }
public ByteFragment ToBytes() { var byteFragment = _writeStream.ToByteFragment(); return(byteFragment); }
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); }