public void SerializeTo(Stream stream) { var bw = new BinaryWriter(stream); bw.Write(PacketId); bw.Write(SeqGuid.ToByteArray()); bw.Write(SeqIdx); bw.Write(IsLast); bw.Write(SeqSize); bw.Write(FastBuffer.SliceBuffer(SeqBuffer, 0, SeqSize)); }
private static IEnumerable <ISequencePacket> CreateSequence(byte[] newBuff, int buffSize, int modRest, int origSize) { var idx = 0; for (var i = 0; i < newBuff.Length - modRest; i += buffSize, idx++) { var seqPacket = new SequencePacket { SeqIdx = (i / buffSize), SeqBuffer = FastBuffer.SliceBuffer(newBuff, i, buffSize), SeqSize = buffSize, IsLast = false }; yield return(seqPacket); } yield return(new SequencePacket { SeqIdx = idx, SeqBuffer = FastBuffer.SliceBuffer(newBuff, idx * buffSize, modRest), SeqSize = modRest, IsLast = true }); }
private void ReadDataCallback(IAsyncResult res) { var buffObj = (BufferObject)res.AsyncState; buffObj.ReadLen = ChannelSocket.EndReceive(res); buffObj.TotalRead += buffObj.ReadLen; buffObj.CompleteBuff.AddRange(FastBuffer.SliceBuffer(buffObj.RecBuff, 0, buffObj.ReadLen)); if (buffObj.CompleteBuff.Count < buffObj.PacketSize) { // keep reading if (buffObj.BufferSize < (buffObj.PacketSize - buffObj.CompleteBuff.Count)) { ChannelSocket.BeginReceive(buffObj.RecBuff, 0, buffObj.BufferSize, 0, ReadDataCallback, buffObj); } else { ChannelSocket.BeginReceive(buffObj.RecBuff, 0, (buffObj.PacketSize - buffObj.CompleteBuff.Count), 0, ReadDataCallback, buffObj); } } else { // full message was received var dataBuff = buffObj.CompleteBuff.ToArray(); var isOperation = BitConverter.ToBoolean(dataBuff, 0); int internalId; var operationGuid = Guid.Empty; var connectionGuid = Guid.Empty; if (isOperation) { operationGuid = new Guid(dataBuff.Slice(2, 16)); Debug.Print("Received operation guid: " + operationGuid); connectionGuid = new Guid(dataBuff.Slice(18, 16)); internalId = BitConverter.ToInt32(dataBuff, 34); } else { internalId = BitConverter.ToInt32(dataBuff, 2); } if (_internalPacketTbl.ContainsKey(internalId)) { var packet = (IDataPacket)Activator.CreateInstance(_internalPacketTbl[internalId]); var ms = new MemoryStream(dataBuff) { Position = 2 }; packet.DeserializeFrom(ms); if (packet is SynchronizePacket) { HandleSynchronizePacket(packet as SynchronizePacket); } else if (packet is SequenceInitPacket) { HandleSequenceInitPacket(packet as SequenceInitPacket); } else if (packet is SequencePacket) { HandleSequencePacket(packet as SequencePacket); } else if (packet is PeerRelocationRequestPacket) { HandleRelocationPacket(packet as PeerRelocationRequestPacket); } else if (packet is SocketOperationRequest) { HandleSocketOperationPacket(packet as SocketOperationRequest); } } else if (ActiveSocketOperations.ContainsKey(operationGuid)) { using (var ms = new MemoryStream(dataBuff) { Position = 34 }) { var packet = PacketProcessor.ParsePacket(ms); packet.DeserializeFrom(ms); ActiveSocketOperations[operationGuid].PacketReceived(packet, this); } } else { RawPacketReceived(dataBuff); } buffObj.ReadLen = 0; buffObj.CompleteBuff.Clear(); buffObj.PacketSize = 0; ChannelSocket.BeginReceive(buffObj.RecBuff, 0, 4, 0, ReadLengthCallback, buffObj); } }