private object ReceivePackage(IPackage package) { if (!package.IsValid) { return(null); } ReceivingPackage?.Invoke(this, new PackageEventArgs(package)); if (!package.IsValid) { return(null); } if (package.Receiver != this) { package.Tick(); package.Sender.SendPackage(package); return(null); } IReport report = package.Unwrap(); ReceivedPackage?.Invoke(this, new ReportEventArgs(report)); report.Sender.SentPackage?.Invoke(report.Sender, new ReportEventArgs(report)); return(report.Content); }
private T ReceivePackage <T>(Package <T> package) { if (!package.IsValid) { return(default(T)); } ReceivingPackage?.Invoke(this, new PackageEventArgs(package)); if (!package.IsValid) { return(default(T)); } if (package.Receiver != this) { package.Tick(); package.Sender.SendPackage(package); return(default(T)); } Report <T> report = package.Unwrap(); ReceivedPackage?.Invoke(this, new ReportEventArgs(report)); report.Sender.SentPackage?.Invoke(report.Sender, new ReportEventArgs(report)); return(report.Content); }
public void ReceivePackage(CommandPackage package) { switch (package.command) { case ServerCommands.BROADCAST: ReceivedPackage?.Invoke(this, package); // PrintUtils.PrintNormal(Encoding.ASCII.GetString(package.data, 0, package.data.Length)); break; case ServerCommands.MESSAGE: ReceivedPackage?.Invoke(this, package); // PrintUtils.PrintNormal(Encoding.ASCII.GetString(package.data, 0, package.data.Length)); break; } }
/// <summary> /// Create a frame out of the <see cref="IByteBuffer" /> and return it. /// </summary> /// <param name="context"> /// The <see cref="IChannelHandlerContext" /> which this <see cref="ByteToMessageDecoder" /> belongs /// to. /// </param> /// <param name="input">The <see cref="IByteBuffer" /> from which to read data.</param> /// <returns>The <see cref="IByteBuffer" /> which represents the frame or <c>null</c> if no frame could be created.</returns> protected virtual object Decode(IChannelHandlerContext context, IByteBuffer input) { var transportContext = _getContextAction(); var package = new ReceivedPackage() { IsComplete = false, IsOutputStream = transportContext.IsOutputStream, OutputFilePath = transportContext.OutputFilePath }; if (transportContext.IsOutputStream) { if (!_foundHeader) { //未读取到头部,并且长度不足头部 if (input.ReadableBytes < lengthFieldEndOffset) { return(null); } //整个数据包的长度,应该是包长度+2+body long frameLength = input.GetLong(input.ReaderIndex) + lengthFieldEndOffset; int frameLengthInt = (int)frameLength; if (input.ReadableBytes < frameLengthInt) { //读取全部的数据 var readLength = input.ReadableBytes; IByteBuffer frame = this.ExtractFrame(context, input, input.ReaderIndex, readLength); input.SetReaderIndex(input.ReaderIndex + readLength); _length = frame.ReadLong(); _command = frame.ReadByte(); _status = frame.ReadByte(); package.Length = _length; package.Command = _command; package.Status = _status; var bodyLength = readLength - lengthFieldEndOffset; package.Body = new byte[bodyLength]; frame.ReadBytes(package.Body, 0, package.Body.Length); //设置读取的进度 _readPosition += bodyLength; _foundHeader = true; } else { //全部读完,则直接完成 // extract frame int readerIndex = input.ReaderIndex; IByteBuffer frame = ExtractFrame(context, input, readerIndex, frameLengthInt); input.SetReaderIndex(readerIndex + frameLengthInt); package.Length = frame.ReadLong(); package.Command = frame.ReadByte(); package.Status = frame.ReadByte(); package.Body = new byte[package.Length]; frame.ReadBytes(package.Body, 0, (int)package.Length); //重置 Reset(); package.IsComplete = true; } } else { //不是第一次读 package.Length = _length; package.Command = _command; package.Status = _status; //未读的长度 var unreadLength = _length - _readPosition; //需要写的长度,剩余长度与当前接收包体两者取小值 var chunkSize = Math.Min(unreadLength, input.ReadableBytes); IByteBuffer frame = this.ExtractFrame(context, input, input.ReaderIndex, (int)chunkSize); //设置读的标记,读到头为止 input.SetReaderIndex(input.ReaderIndex + (int)chunkSize); package.Body = new byte[chunkSize]; frame.ReadBytes(package.Body, 0, (int)chunkSize); //判断是否完成 if (_readPosition + chunkSize >= _length) { //完成 Reset(); package.IsComplete = true; } else { //设置读取进度 _readPosition += chunkSize; } } } else { //读取不到头部 if (input.ReadableBytes < lengthFieldEndOffset) { return(null); } //整个数据包的长度,应该是包长度+2+body long frameLength = input.GetLong(input.ReaderIndex) + lengthFieldEndOffset; int frameLengthInt = (int)frameLength; if (input.ReadableBytes < frameLengthInt) { return(null); } // extract frame int readerIndex = input.ReaderIndex; IByteBuffer frame = ExtractFrame(context, input, readerIndex, frameLengthInt); input.SetReaderIndex(readerIndex + frameLengthInt); package.Length = frame.ReadLong(); package.Command = frame.ReadByte(); package.Status = frame.ReadByte(); package.Body = new byte[package.Length]; frame.ReadBytes(package.Body, 0, (int)package.Length); //重置 Reset(); package.IsComplete = true; } return(package); }
public void ReceivePackage(CommandPackage package) { ReceivedPackage?.Invoke(this, package); }