public async Task <IActionResult> GetReply([FromRoute] int postId, [FromRoute] int replyId) { if (!await _postManager.PostExists(postId)) { return(NotFound("Post does not exist.")); } var reply = await _replyManager.GetReply(replyId); if (reply == null) { return(NotFound("Reply does not exist.")); } var response = _mapper.Map <ReplyResponse>(reply); return(Ok(response)); }
/// <summary> /// Reads and processes the given bytes from the given buffer and provided length. /// </summary> /// <param name="buffer">Buffer containing the received bytes</param> /// <param name="offset">Offset inside <paramref name="buffer"/> parameter where the data begins</param> /// <param name="rcvLen">Number of bytes received</param> public void ReadBytes(byte[] buffer, int offset, int rcvLen) { if (rcvLen > 0 && buffer != null) { if (IsReceiveCompleted && rcvLen >= Constants.MessageHeaderSize) { FastStreamReader stream = new FastStreamReader(buffer, offset, rcvLen); if (stream.FindAndSkip(magicBytes)) { int rem = stream.GetRemainingBytesCount(); if (rem >= Constants.MessageHeaderSize) { var msg = new Message(netType); Message.ReadResult res = msg.Read(stream); if (res == Message.ReadResult.Success) { IsReceiveCompleted = true; Message[] reply = replyManager.GetReply(msg); if (reply != null) { foreach (var item in reply) { toSendQueue.Enqueue(item); } } // TODO: handle received message here. eg. write received block to disk,... // Handle remaining data rem = stream.GetRemainingBytesCount(); if (rem > 0) { rcvHolder = stream.ReadByteArrayChecked(rem); if (rem >= Constants.MessageHeaderSize) { IsReceiveCompleted = true; ReadBytes(rcvHolder, 0, rcvHolder.Length); } else { IsReceiveCompleted = false; } } else { rcvHolder = null; } } else if (res == Message.ReadResult.NotEnoughBytes) { IsReceiveCompleted = false; rcvHolder = new byte[rcvLen]; Buffer.BlockCopy(buffer, offset, rcvHolder, 0, rcvLen); } else { // Invalid message was received (checksum, network or payload size overflow) NodeStatus.AddSmallViolation(); rcvHolder = null; } } else if (rem != 0) { rcvHolder = stream.ReadByteArrayChecked(rem); IsReceiveCompleted = false; } } else { // There are always 3 bytes remaining in stream that failed to find magic and has to be stored in holder rcvHolder = stream.ReadByteArrayChecked(stream.GetRemainingBytesCount()); IsReceiveCompleted = false; // There were at least 21 bytes of garbage in this buffer NodeStatus.AddSmallViolation(); } } else { if (rcvHolder == null) { rcvHolder = new byte[rcvLen]; Buffer.BlockCopy(buffer, offset, rcvHolder, 0, rcvLen); } else { byte[] temp = new byte[rcvHolder.Length + rcvLen]; Buffer.BlockCopy(rcvHolder, 0, temp, 0, rcvHolder.Length); Buffer.BlockCopy(buffer, offset, temp, rcvHolder.Length, rcvLen); rcvHolder = temp; } if (rcvHolder.Length >= Constants.MessageHeaderSize) { IsReceiveCompleted = true; ReadBytes(rcvHolder, 0, rcvHolder.Length); } else { IsReceiveCompleted = false; } } } }
/// <summary> /// Reads and processes the given bytes from the given buffer and provided length. /// </summary> /// <param name="buffer">Buffer containing the received bytes</param> /// <param name="len">Number of bytes received</param> /// <param name="offert">Offset inside <paramref name="buffer"/> parameter where the data begins</param> public void ReadBytes(byte[] buffer, int len, int offert) { if (len > 0 && buffer != null) { if (IsReceiveCompleted && len >= Constants.MessageHeaderSize) { FastStreamReader stream = new FastStreamReader(buffer, offert, len); if (stream.FindAndSkip(magicBytes)) { int rem = stream.GetRemainingBytesCount(); if (rem >= Constants.MessageHeaderSize) { var msg = new Message(netType); Message.ReadResult res = msg.Read(stream); if (res == Message.ReadResult.Success) { IsReceiveCompleted = true; Message[] reply = replyManager.GetReply(msg); if (reply != null) { foreach (var item in reply) { toSendQueue.Enqueue(item); } } // TODO: handle received message here. eg. write received block to disk,... // Handle remaining data if (stream.GetRemainingBytesCount() > 0) { _ = stream.TryReadByteArray(stream.GetRemainingBytesCount(), out tempHolder); ReadBytes(tempHolder, tempHolder.Length, 0); } else { tempHolder = null; } } else if (res == Message.ReadResult.NotEnoughBytes) { IsReceiveCompleted = false; tempHolder = new byte[len]; Buffer.BlockCopy(buffer, 0, tempHolder, 0, len); } else // TODO: add violation (invalid message) { } } else if (rem != 0) { stream.TryReadByteArray(rem, out tempHolder); IsReceiveCompleted = false; } } else // TODO: add violation (No magic was found) { } // TODO: some sort of point system is needed to give negative points to malicious nodes // eg. sending garbage bytes which is what is caught in the "else" part of the "if" above } else { if (tempHolder == null) { tempHolder = new byte[len]; Buffer.BlockCopy(buffer, 0, tempHolder, 0, len); } else { byte[] temp = new byte[tempHolder.Length + len]; Buffer.BlockCopy(tempHolder, 0, temp, 0, tempHolder.Length); Buffer.BlockCopy(buffer, 0, temp, tempHolder.Length, len); tempHolder = temp; } if (tempHolder.Length >= Constants.MessageHeaderSize) { IsReceiveCompleted = true; ReadBytes(tempHolder, tempHolder.Length, 0); } else { IsReceiveCompleted = false; } } } }