public static SequenceContext CreateFromPacket(IDataPacket data, int fragmentSize) { SequenceInitPacket initPacket; var ms = new MemoryStream(); data.SerializeTo(ms); NetworkWriter.PrependStreamSize(ms); var completeBuff = ms.ToArray(); Guid seqGuid; var sequence = new SequenceHandler().CreateSequence(data, completeBuff, fragmentSize, out initPacket, out seqGuid); var initPacketStream = new MemoryStream(); initPacket.SerializeTo(initPacketStream); #if DEBUG // var sequenceStreams = new MemoryStream[sequence.Length]; // var sequenceBuffs = new List<byte[]>(); //for (var i = 0; i < sequence.Count; i++) //{ // var seq = sequence[i]; // sequenceStreams[i] = new MemoryStream(); // seq.SerializeTo(sequenceStreams[i]); // sequenceBuffs.Add(sequenceStreams[i].ToArray()); //} #endif var seqCtx = new SequenceContext(initPacket, sequence, initPacketStream.ToArray(), null) { SequenceGuid = seqGuid }; return(seqCtx); }
public void WritePacketToSocket(IDataPacket data, IChannel senderChannel, Guid guid, BufferObject buffObj, Socket sock, AsyncCallback sendCallback, SocketOperationContext operationCtx) { var ms = new MemoryStream(); data.SerializeTo(ms); sbyte integrityHashSize = 0; if (senderChannel != null) { switch (senderChannel.IntegrityHash) { case Security.PacketIntegrityHash.Sha256: integrityHashSize = 32; break; case Security.PacketIntegrityHash.Crc32: integrityHashSize = 4; break; case Security.PacketIntegrityHash.Elf32: integrityHashSize = 4; break; } } var headerSize = 1 + (operationCtx != null ? 32 : 0) + (senderChannel != null && senderChannel.EnsurePacketIntegrity ? integrityHashSize + 2 : 1); var sendBuff = new byte[ms.Length + headerSize + 4]; FastBuffer.MemCpy(BitConverter.GetBytes(headerSize + ms.Length), 0, sendBuff, 0, 4); sendBuff[4] = operationCtx == null ? ((byte)0x0) : ((byte)0x1); sendBuff[5] = senderChannel != null && senderChannel.EnsurePacketIntegrity ? (byte)0x1 : (byte)0x0; if (senderChannel != null && senderChannel.EnsurePacketIntegrity) { sendBuff[6] = (byte)senderChannel.IntegrityHash; } if (operationCtx != null) { FastBuffer.MemCpy(operationCtx.OperationGuid.ToByteArray(), 0, sendBuff, 6, 16); FastBuffer.MemCpy(guid.ToByteArray(), 0, sendBuff, 22, 16); } var tmpBuff = new byte[ms.Length]; ms.Seek(0, SeekOrigin.Begin); ms.Read(tmpBuff, 0, tmpBuff.Length); FastBuffer.MemCpy(tmpBuff, 0, sendBuff, operationCtx != null ? 38 : 6, tmpBuff.Length); var sendObj = new Peer.Peer.SendObject { Channel = senderChannel, Packet = data }; if (sock.Connected) { sock.BeginSend(sendBuff, 0, sendBuff.Length, 0, sendCallback, sendObj); } }