private bool readBody(byte[] data, int offset, int limit) { int length = limit - offset; if (length >= (buffer.Length - bufferOffset)) { Array.Copy(data, offset, buffer, bufferOffset, buffer.Length - bufferOffset); offset += (buffer.Length - bufferOffset); // 处理message this.bufferOffset = 0; this.headOffset = 0; this.packetHead = 0; rcvPacketCallback.Invoke(buffer); this.inboundState = InboundState.readHead; if (offset <= limit) { processBytes(data, offset, limit); } return(true); } else { Array.Copy(data, offset, buffer, bufferOffset, limit - offset); bufferOffset += (limit - offset); return(false); } }
private bool readPacketLength(byte[] data, int offset, int limit) { if (limit <= offset) { return(false); } for (; ;) { byte dataIndex = data[offset]; offset++; headBuffer[headOffset] = dataIndex; headOffset++; if ((dataIndex & 128) != 0) { if (headOffset >= MAX_PACKET_LENGTH_SIZE) { throw new PacketException("Invalid input stream"); } if (limit <= offset) { break; } } else { UInt32 rLength = 0; int multiplier = 0; int headIndex = 0; while (headIndex <= headOffset) { byte val = headBuffer[headIndex]; rLength |= ((UInt32)(val & 127)) << multiplier; headIndex++; if ((val & 128) == 0) { break; } multiplier += 7; } buffer = new byte[rLength + 1 + headIndex]; buffer[0] = this.packetHead; Array.Copy(headBuffer, 0, buffer, 1, headIndex); bufferOffset = 1 + headIndex; this.inboundState = InboundState.readBody; if (offset <= limit) { processBytes(data, offset, limit); } return(true); } } return(false); }
/** * */ private bool readHead(byte[] data, int offset, int limit) { int length = limit - offset; if (length > 0) { this.packetHead = data[0]; this.inboundState = InboundState.readLength; offset++; if (limit > offset) { processBytes(data, offset, limit); } return(true); } return(false); }
public PacketBoundHandler(ITransport tranport, Action <byte[]> rcvCallback) { this.tranport = tranport; this.inboundState = InboundState.readHead; this.rcvPacketCallback = rcvCallback; }
public void Dispose() { this.inboundState = InboundState.closed; }