`
shihuan830619
  • 浏览: 583762 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Netty中Protobuf编解码应用

阅读更多
工程结构图如下:


SubscribeReqProto.java文件内容如下:
package com.shihuan.netty.codec.protobuf;

public final class SubscribeReqProto {

	private SubscribeReqProto() {
		
	}

	public static void registerAllExtensions(com.google.protobuf.ExtensionRegistry registry) {
		
	}
	
	public interface SubscribeReqOrBuilder extends com.google.protobuf.MessageOrBuilder {

		// required int32 subReqID = 1;
		/**
		 * <code>required int32 subReqID = 1;</code>
		 */
		boolean hasSubReqID();

		/**
		 * <code>required int32 subReqID = 1;</code>
		 */
		int getSubReqID();

		// required string userName = 2;
		/**
		 * <code>required string userName = 2;</code>
		 */
		boolean hasUserName();

		/**
		 * <code>required string userName = 2;</code>
		 */
		java.lang.String getUserName();

		/**
		 * <code>required string userName = 2;</code>
		 */
		com.google.protobuf.ByteString getUserNameBytes();

		// required string productName = 3;
		/**
		 * <code>required string productName = 3;</code>
		 */
		boolean hasProductName();

		/**
		 * <code>required string productName = 3;</code>
		 */
		java.lang.String getProductName();

		/**
		 * <code>required string productName = 3;</code>
		 */
		com.google.protobuf.ByteString getProductNameBytes();

		// repeated string address = 4;
		/**
		 * <code>repeated string address = 4;</code>
		 */
		java.util.List<java.lang.String> getAddressList();

		/**
		 * <code>repeated string address = 4;</code>
		 */
		int getAddressCount();

		/**
		 * <code>repeated string address = 4;</code>
		 */
		java.lang.String getAddress(int index);

		/**
		 * <code>repeated string address = 4;</code>
		 */
		com.google.protobuf.ByteString getAddressBytes(int index);
	}
	
	/**
	 * Protobuf type {@code netty.SubscribeReq}
	 */
	public static final class SubscribeReq extends com.google.protobuf.GeneratedMessage implements SubscribeReqOrBuilder {
		// Use SubscribeReq.newBuilder() to construct.
		private SubscribeReq(com.google.protobuf.GeneratedMessage.Builder<?> builder) {
			super(builder);
			this.unknownFields = builder.getUnknownFields();
		}

		private SubscribeReq(boolean noInit) {
			this.unknownFields = com.google.protobuf.UnknownFieldSet.getDefaultInstance();
		}

		private static final SubscribeReq defaultInstance;

		public static SubscribeReq getDefaultInstance() {
			return defaultInstance;
		}

		public SubscribeReq getDefaultInstanceForType() {
			return defaultInstance;
		}

		private final com.google.protobuf.UnknownFieldSet unknownFields;

		@java.lang.Override
		public final com.google.protobuf.UnknownFieldSet getUnknownFields() {
			return this.unknownFields;
		}

		private SubscribeReq(com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException {
			initFields();
			int mutable_bitField0_ = 0;
			com.google.protobuf.UnknownFieldSet.Builder unknownFields = com.google.protobuf.UnknownFieldSet.newBuilder();
			try {
				boolean done = false;
				while (!done) {
					int tag = input.readTag();
					switch (tag) {
						case 0:
							done = true;
							break;
						default: {
							if (!parseUnknownField(input, unknownFields, extensionRegistry, tag)) {
								done = true;
							}
							break;
						}
						case 8: {
							bitField0_ |= 0x00000001;
							subReqID_ = input.readInt32();
							break;
						}
						case 18: {
							bitField0_ |= 0x00000002;
							userName_ = input.readBytes();
							break;
						}
						case 26: {
							bitField0_ |= 0x00000004;
							productName_ = input.readBytes();
							break;
						}
						case 34: {
							if (!((mutable_bitField0_ & 0x00000008) == 0x00000008)) {
								address_ = new com.google.protobuf.LazyStringArrayList();
								mutable_bitField0_ |= 0x00000008;
							}
							address_.add(input.readBytes());
							break;
						}
					}
				}
			} catch (com.google.protobuf.InvalidProtocolBufferException e) {
				throw e.setUnfinishedMessage(this);
			} catch (java.io.IOException e) {
				throw new com.google.protobuf.InvalidProtocolBufferException(e.getMessage()).setUnfinishedMessage(this);
			} finally {
				if (((mutable_bitField0_ & 0x00000008) == 0x00000008)) {
					address_ = new com.google.protobuf.UnmodifiableLazyStringList(address_);
				}
				this.unknownFields = unknownFields.build();
				makeExtensionsImmutable();
			}
		}

		public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() {
			return com.shihuan.netty.codec.protobuf.SubscribeReqProto.internal_static_netty_SubscribeReq_descriptor;
		}

		protected com.google.protobuf.GeneratedMessage.FieldAccessorTable internalGetFieldAccessorTable() {
			return com.shihuan.netty.codec.protobuf.SubscribeReqProto.internal_static_netty_SubscribeReq_fieldAccessorTable.ensureFieldAccessorsInitialized(
					com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq.class,
					com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq.Builder.class);
		}

		public static com.google.protobuf.Parser<SubscribeReq> PARSER = new com.google.protobuf.AbstractParser<SubscribeReq>() {
			public SubscribeReq parsePartialFrom(com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException {
				return new SubscribeReq(input, extensionRegistry);
			}
		};

		@java.lang.Override
		public com.google.protobuf.Parser<SubscribeReq> getParserForType() {
			return PARSER;
		}

		private int bitField0_;
		// required int32 subReqID = 1;
		public static final int SUBREQID_FIELD_NUMBER = 1;
		private int subReqID_;

		/**
		 * <code>required int32 subReqID = 1;</code>
		 */
		public boolean hasSubReqID() {
			return ((bitField0_ & 0x00000001) == 0x00000001);
		}

		/**
		 * <code>required int32 subReqID = 1;</code>
		 */
		public int getSubReqID() {
			return subReqID_;
		}

		// required string userName = 2;
		public static final int USERNAME_FIELD_NUMBER = 2;
		private java.lang.Object userName_;

		/**
		 * <code>required string userName = 2;</code>
		 */
		public boolean hasUserName() {
			return ((bitField0_ & 0x00000002) == 0x00000002);
		}

		/**
		 * <code>required string userName = 2;</code>
		 */
		public java.lang.String getUserName() {
			java.lang.Object ref = userName_;
			if (ref instanceof java.lang.String) {
				return (java.lang.String) ref;
			} else {
				com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref;
				java.lang.String s = bs.toStringUtf8();
				if (bs.isValidUtf8()) {
					userName_ = s;
				}
				return s;
			}
		}

		/**
		 * <code>required string userName = 2;</code>
		 */
		public com.google.protobuf.ByteString getUserNameBytes() {
			java.lang.Object ref = userName_;
			if (ref instanceof java.lang.String) {
				com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8((java.lang.String) ref);
				userName_ = b;
				return b;
			} else {
				return (com.google.protobuf.ByteString) ref;
			}
		}

		// required string productName = 3;
		public static final int PRODUCTNAME_FIELD_NUMBER = 3;
		private java.lang.Object productName_;

		/**
		 * <code>required string productName = 3;</code>
		 */
		public boolean hasProductName() {
			return ((bitField0_ & 0x00000004) == 0x00000004);
		}

		/**
		 * <code>required string productName = 3;</code>
		 */
		public java.lang.String getProductName() {
			java.lang.Object ref = productName_;
			if (ref instanceof java.lang.String) {
				return (java.lang.String) ref;
			} else {
				com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref;
				java.lang.String s = bs.toStringUtf8();
				if (bs.isValidUtf8()) {
					productName_ = s;
				}
				return s;
			}
		}

		/**
		 * <code>required string productName = 3;</code>
		 */
		public com.google.protobuf.ByteString getProductNameBytes() {
			java.lang.Object ref = productName_;
			if (ref instanceof java.lang.String) {
				com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8((java.lang.String) ref);
				productName_ = b;
				return b;
			} else {
				return (com.google.protobuf.ByteString) ref;
			}
		}

		// repeated string address = 4;
		public static final int ADDRESS_FIELD_NUMBER = 4;
		private com.google.protobuf.LazyStringList address_;

		/**
		 * <code>repeated string address = 4;</code>
		 */
		public java.util.List<java.lang.String> getAddressList() {
			return address_;
		}

		/**
		 * <code>repeated string address = 4;</code>
		 */
		public int getAddressCount() {
			return address_.size();
		}

		/**
		 * <code>repeated string address = 4;</code>
		 */
		public java.lang.String getAddress(int index) {
			return address_.get(index);
		}

		/**
		 * <code>repeated string address = 4;</code>
		 */
		public com.google.protobuf.ByteString getAddressBytes(int index) {
			return address_.getByteString(index);
		}

		private void initFields() {
			subReqID_ = 0;
			userName_ = "";
			productName_ = "";
			address_ = com.google.protobuf.LazyStringArrayList.EMPTY;
		}

		private byte memoizedIsInitialized = -1;

		public final boolean isInitialized() {
			byte isInitialized = memoizedIsInitialized;
			if (isInitialized != -1)
				return isInitialized == 1;

			if (!hasSubReqID()) {
				memoizedIsInitialized = 0;
				return false;
			}
			if (!hasUserName()) {
				memoizedIsInitialized = 0;
				return false;
			}
			if (!hasProductName()) {
				memoizedIsInitialized = 0;
				return false;
			}
			memoizedIsInitialized = 1;
			return true;
		}

		public void writeTo(com.google.protobuf.CodedOutputStream output) throws java.io.IOException {
			getSerializedSize();
			if (((bitField0_ & 0x00000001) == 0x00000001)) {
				output.writeInt32(1, subReqID_);
			}
			if (((bitField0_ & 0x00000002) == 0x00000002)) {
				output.writeBytes(2, getUserNameBytes());
			}
			if (((bitField0_ & 0x00000004) == 0x00000004)) {
				output.writeBytes(3, getProductNameBytes());
			}
			for (int i = 0; i < address_.size(); i++) {
				output.writeBytes(4, address_.getByteString(i));
			}
			getUnknownFields().writeTo(output);
		}

		private int memoizedSerializedSize = -1;

		public int getSerializedSize() {
			int size = memoizedSerializedSize;
			if (size != -1)
				return size;

			size = 0;
			if (((bitField0_ & 0x00000001) == 0x00000001)) {
				size += com.google.protobuf.CodedOutputStream.computeInt32Size(1, subReqID_);
			}
			if (((bitField0_ & 0x00000002) == 0x00000002)) {
				size += com.google.protobuf.CodedOutputStream.computeBytesSize(2, getUserNameBytes());
			}
			if (((bitField0_ & 0x00000004) == 0x00000004)) {
				size += com.google.protobuf.CodedOutputStream.computeBytesSize(3, getProductNameBytes());
			}
			{
				int dataSize = 0;
				for (int i = 0; i < address_.size(); i++) {
					dataSize += com.google.protobuf.CodedOutputStream.computeBytesSizeNoTag(address_.getByteString(i));
				}
				size += dataSize;
				size += 1 * getAddressList().size();
			}
			size += getUnknownFields().getSerializedSize();
			memoizedSerializedSize = size;
			return size;
		}

		private static final long serialVersionUID = 0L;

		@java.lang.Override
		protected java.lang.Object writeReplace() throws java.io.ObjectStreamException {
			return super.writeReplace();
		}

		public static com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq parseFrom(com.google.protobuf.ByteString data) throws com.google.protobuf.InvalidProtocolBufferException {
			return PARSER.parseFrom(data);
		}

		public static com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq parseFrom(com.google.protobuf.ByteString data, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException {
			return PARSER.parseFrom(data, extensionRegistry);
		}

		public static com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq parseFrom(byte[] data) throws com.google.protobuf.InvalidProtocolBufferException {
			return PARSER.parseFrom(data);
		}

		public static com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq parseFrom(byte[] data, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException {
			return PARSER.parseFrom(data, extensionRegistry);
		}

		public static com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq parseFrom(java.io.InputStream input) throws java.io.IOException {
			return PARSER.parseFrom(input);
		}

		public static com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq parseFrom(java.io.InputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException {
			return PARSER.parseFrom(input, extensionRegistry);
		}

		public static com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq parseDelimitedFrom(java.io.InputStream input) throws java.io.IOException {
			return PARSER.parseDelimitedFrom(input);
		}

		public static com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq parseDelimitedFrom(java.io.InputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException {
			return PARSER.parseDelimitedFrom(input, extensionRegistry);
		}

		public static com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq parseFrom(com.google.protobuf.CodedInputStream input) throws java.io.IOException {
			return PARSER.parseFrom(input);
		}

		public static com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq parseFrom(com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException {
			return PARSER.parseFrom(input, extensionRegistry);
		}

		public static Builder newBuilder() {
			return Builder.create();
		}

		public Builder newBuilderForType() {
			return newBuilder();
		}

		public static Builder newBuilder(com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq prototype) {
			return newBuilder().mergeFrom(prototype);
		}

		public Builder toBuilder() {
			return newBuilder(this);
		}

		@java.lang.Override
		protected Builder newBuilderForType(com.google.protobuf.GeneratedMessage.BuilderParent parent) {
			Builder builder = new Builder(parent);
			return builder;
		}

		/**
		 * Protobuf type {@code netty.SubscribeReq}
		 */
		public static final class Builder extends com.google.protobuf.GeneratedMessage.Builder<Builder> implements com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReqOrBuilder {
			public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() {
				return com.shihuan.netty.codec.protobuf.SubscribeReqProto.internal_static_netty_SubscribeReq_descriptor;
			}

			protected com.google.protobuf.GeneratedMessage.FieldAccessorTable internalGetFieldAccessorTable() {
				return com.shihuan.netty.codec.protobuf.SubscribeReqProto.internal_static_netty_SubscribeReq_fieldAccessorTable.ensureFieldAccessorsInitialized(com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq.class, com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq.Builder.class);
			}

			// Construct using
			// com.phei.netty.codec.protobuf.SubscribeReqProto.SubscribeReq.newBuilder()
			private Builder() {
				maybeForceBuilderInitialization();
			}

			private Builder(com.google.protobuf.GeneratedMessage.BuilderParent parent) {
				super(parent);
				maybeForceBuilderInitialization();
			}

			private void maybeForceBuilderInitialization() {
				if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {
				}
			}

			private static Builder create() {
				return new Builder();
			}

			public Builder clear() {
				super.clear();
				subReqID_ = 0;
				bitField0_ = (bitField0_ & ~0x00000001);
				userName_ = "";
				bitField0_ = (bitField0_ & ~0x00000002);
				productName_ = "";
				bitField0_ = (bitField0_ & ~0x00000004);
				address_ = com.google.protobuf.LazyStringArrayList.EMPTY;
				bitField0_ = (bitField0_ & ~0x00000008);
				return this;
			}

			public Builder clone() {
				return create().mergeFrom(buildPartial());
			}

			public com.google.protobuf.Descriptors.Descriptor getDescriptorForType() {
				return com.shihuan.netty.codec.protobuf.SubscribeReqProto.internal_static_netty_SubscribeReq_descriptor;
			}

			public com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq getDefaultInstanceForType() {
				return com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq.getDefaultInstance();
			}

			public com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq build() {
				com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq result = buildPartial();
				if (!result.isInitialized()) {
					throw newUninitializedMessageException(result);
				}
				return result;
			}

			public com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq buildPartial() {
				com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq result = new com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq(this);
				int from_bitField0_ = bitField0_;
				int to_bitField0_ = 0;
				if (((from_bitField0_ & 0x00000001) == 0x00000001)) {
					to_bitField0_ |= 0x00000001;
				}
				result.subReqID_ = subReqID_;
				if (((from_bitField0_ & 0x00000002) == 0x00000002)) {
					to_bitField0_ |= 0x00000002;
				}
				result.userName_ = userName_;
				if (((from_bitField0_ & 0x00000004) == 0x00000004)) {
					to_bitField0_ |= 0x00000004;
				}
				result.productName_ = productName_;
				if (((bitField0_ & 0x00000008) == 0x00000008)) {
					address_ = new com.google.protobuf.UnmodifiableLazyStringList(address_);
					bitField0_ = (bitField0_ & ~0x00000008);
				}
				result.address_ = address_;
				result.bitField0_ = to_bitField0_;
				onBuilt();
				return result;
			}

			public Builder mergeFrom(com.google.protobuf.Message other) {
				if (other instanceof com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq) {
					return mergeFrom((com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq) other);
				} else {
					super.mergeFrom(other);
					return this;
				}
			}

			public Builder mergeFrom(com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq other) {
				if (other == com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq.getDefaultInstance())
					return this;
				if (other.hasSubReqID()) {
					setSubReqID(other.getSubReqID());
				}
				if (other.hasUserName()) {
					bitField0_ |= 0x00000002;
					userName_ = other.userName_;
					onChanged();
				}
				if (other.hasProductName()) {
					bitField0_ |= 0x00000004;
					productName_ = other.productName_;
					onChanged();
				}
				if (!other.address_.isEmpty()) {
					if (address_.isEmpty()) {
						address_ = other.address_;
						bitField0_ = (bitField0_ & ~0x00000008);
					} else {
						ensureAddressIsMutable();
						address_.addAll(other.address_);
					}
					onChanged();
				}
				this.mergeUnknownFields(other.getUnknownFields());
				return this;
			}

			public final boolean isInitialized() {
				if (!hasSubReqID()) {

					return false;
				}
				if (!hasUserName()) {

					return false;
				}
				if (!hasProductName()) {

					return false;
				}
				return true;
			}

			public Builder mergeFrom(com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException {
				com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq parsedMessage = null;
				try {
					parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
				} catch (com.google.protobuf.InvalidProtocolBufferException e) {
					parsedMessage = (com.shihuan.netty.codec.protobuf.SubscribeReqProto.SubscribeReq) e.getUnfinishedMessage();
					throw e;
				} finally {
					if (parsedMessage != null) {
						mergeFrom(parsedMessage);
					}
				}
				return this;
			}

			private int bitField0_;

			// required int32 subReqID = 1;
			private int subReqID_;

			/**
			 * <code>required int32 subReqID = 1;</code>
			 */
			public boolean hasSubReqID() {
				return ((bitField0_ & 0x00000001) == 0x00000001);
			}

			/**
			 * <code>required int32 subReqID = 1;</code>
			 */
			public int getSubReqID() {
				return subReqID_;
			}

			/**
			 * <code>required int32 subReqID = 1;</code>
			 */
			public Builder setSubReqID(int value) {
				bitField0_ |= 0x00000001;
				subReqID_ = value;
				onChanged();
				return this;
			}

			/**
			 * <code>required int32 subReqID = 1;</code>
			 */
			public Builder clearSubReqID() {
				bitField0_ = (bitField0_ & ~0x00000001);
				subReqID_ = 0;
				onChanged();
				return this;
			}

			// required string userName = 2;
			private java.lang.Object userName_ = "";

			/**
			 * <code>required string userName = 2;</code>
			 */
			public boolean hasUserName() {
				return ((bitField0_ & 0x00000002) == 0x00000002);
			}

			/**
			 * <code>required string userName = 2;</code>
			 */
			public java.lang.String getUserName() {
				java.lang.Object ref = userName_;
				if (!(ref instanceof java.lang.String)) {
					java.lang.String s = ((com.google.protobuf.ByteString) ref).toStringUtf8();
					userName_ = s;
					return s;
				} else {
					return (java.lang.String) ref;
				}
			}

			/**
			 * <code>required string userName = 2;</code>
			 */
			public com.google.protobuf.ByteString getUserNameBytes() {
				java.lang.Object ref = userName_;
				if (ref instanceof String) {
					com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8((java.lang.String) ref);
					userName_ = b;
					return b;
				} else {
					return (com.google.protobuf.ByteString) ref;
				}
			}

			/**
			 * <code>required string userName = 2;</code>
			 */
			public Builder setUserName(java.lang.String value) {
				if (value == null) {
					throw new NullPointerException();
				}
				bitField0_ |= 0x00000002;
				userName_ = value;
				onChanged();
				return this;
			}

			/**
			 * <code>required string userName = 2;</code>
			 */
			public Builder clearUserName() {
				bitField0_ = (bitField0_ & ~0x00000002);
				userName_ = getDefaultInstance().getUserName();
				onChanged();
				return this;
			}

			/**
			 * <code>required string userName = 2;</code>
			 */
			public Builder setUserNameBytes(com.google.protobuf.ByteString value) {
				if (value == null) {
					throw new NullPointerException();
				}
				bitField0_ |= 0x00000002;
				userName_ = value;
				onChanged();
				return this;
			}

			// required string productName = 3;
			private java.lang.Object productName_ = "";

			/**
			 * <code>required string productName = 3;</code>
			 */
			public boolean hasProductName() {
				return ((bitField0_ & 0x00000004) == 0x00000004);
			}

			/**
			 * <code>required string productName = 3;</code>
			 */
			public java.lang.String getProductName() {
				java.lang.Object ref = productName_;
				if (!(ref instanceof java.lang.String)) {
					java.lang.String s = ((com.google.protobuf.ByteString) ref).toStringUtf8();
					productName_ = s;
					return s;
				} else {
					return (java.lang.String) ref;
				}
			}

			/**
			 * <code>required string productName = 3;</code>
			 */
			public com.google.protobuf.ByteString getProductNameBytes() {
				java.lang.Object ref = productName_;
				if (ref instanceof String) {
					com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8((java.lang.String) ref);
					productName_ = b;
					return b;
				} else {
					return (com.google.protobuf.ByteString) ref;
				}
			}

			/**
			 * <code>required string productName = 3;</code>
			 */
			public Builder setProductName(java.lang.String value) {
				if (value == null) {
					throw new NullPointerException();
				}
				bitField0_ |= 0x00000004;
				productName_ = value;
				onChanged();
				return this;
			}

			/**
			 * <code>required string productName = 3;</code>
			 */
			public Builder clearProductName() {
				bitField0_ = (bitField0_ & ~0x00000004);
				productName_ = getDefaultInstance().getProductName();
				onChanged();
				return this;
			}

			/**
			 * <code>required string productName = 3;</code>
			 */
			public Builder setProductNameBytes(com.google.protobuf.ByteString value) {
				if (value == null) {
					throw new NullPointerException();
				}
				bitField0_ |= 0x00000004;
				productName_ = value;
				onChanged();
				return this;
			}

			// repeated string address = 4;
			private com.google.protobuf.LazyStringList address_ = com.google.protobuf.LazyStringArrayList.EMPTY;

			private void ensureAddressIsMutable() {
				if (!((bitField0_ & 0x00000008) == 0x00000008)) {
					address_ = new com.google.protobuf.LazyStringArrayList(address_);
					bitField0_ |= 0x00000008;
				}
			}

			/**
			 * <code>repeated string address = 4;</code>
			 */
			public java.util.List<java.lang.String> getAddressList() {
				return java.util.Collections.unmodifiableList(address_);
			}

			/**
			 * <code>repeated string address = 4;</code>
			 */
			public int getAddressCount() {
				return address_.size();
			}

			/**
			 * <code>repeated string address = 4;</code>
			 */
			public java.lang.String getAddress(int index) {
				return address_.get(index);
			}

			/**
			 * <code>repeated string address = 4;</code>
			 */
			public com.google.protobuf.ByteString getAddressBytes(int index) {
				return address_.getByteString(index);
			}

			/**
			 * <code>repeated string address = 4;</code>
			 */
			public Builder setAddress(int index, java.lang.String value) {
				if (value == null) {
					throw new NullPointerException();
				}
				ensureAddressIsMutable();
				address_.set(index, value);
				onChanged();
				return this;
			}

			/**
			 * <code>repeated string address = 4;</code>
			 */
			public Builder addAddress(java.lang.String value) {
				if (value == null) {
					throw new NullPointerException();
				}
				ensureAddressIsMutable();
				address_.add(value);
				onChanged();
				return this;
			}

			/**
			 * <code>repeated string address = 4;</code>
			 */
			public Builder addAllAddress(java.lang.Iterable<java.lang.String> values) {
				ensureAddressIsMutable();
				super.addAll(values, address_);
				onChanged();
				return this;
			}

			/**
			 * <code>repeated string address = 4;</code>
			 */
			public Builder clearAddress() {
				address_ = com.google.protobuf.LazyStringArrayList.EMPTY;
				bitField0_ = (bitField0_ & ~0x00000008);
				onChanged();
				return this;
			}

			/**
			 * <code>repeated string address = 4;</code>
			 */
			public Builder addAddressBytes(com.google.protobuf.ByteString value) {
				if (value == null) {
					throw new NullPointerException();
				}
				ensureAddressIsMutable();
				address_.add(value);
				onChanged();
				return this;
			}

			// @@protoc_insertion_point(builder_scope:netty.SubscribeReq)
		}

		static {
			defaultInstance = new SubscribeReq(true);
			defaultInstance.initFields();
		}

		// @@protoc_insertion_point(class_scope:netty.SubscribeReq)
	}

	private static com.google.protobuf.Descriptors.Descriptor internal_static_netty_SubscribeReq_descriptor;
	private static com.google.protobuf.GeneratedMessage.FieldAccessorTable internal_static_netty_SubscribeReq_fieldAccessorTable;

	public static com.google.protobuf.Descriptors.FileDescriptor getDescriptor() {
		return descriptor;
	}

	private static com.google.protobuf.Descriptors.FileDescriptor descriptor;
	static {
		java.lang.String[] descriptorData = { "\n\030netty/SubscribeReq.proto\022\005netty\"X\n\014Sub"
				+ "scribeReq\022\020\n\010subReqID\030\001 \002(\005\022\020\n\010userName\030"
				+ "\002 \002(\t\022\023\n\013productName\030\003 \002(\t\022\017\n\007address\030\004 "
				+ "\003(\tB2\n\035com.phei.netty.codec.protobufB\021Su"
				+ "bscribeReqProto" };
		com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = new com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner() {
			public com.google.protobuf.ExtensionRegistry assignDescriptors(com.google.protobuf.Descriptors.FileDescriptor root) {
				descriptor = root;
				internal_static_netty_SubscribeReq_descriptor = getDescriptor().getMessageTypes().get(0);
				internal_static_netty_SubscribeReq_fieldAccessorTable = new com.google.protobuf.GeneratedMessage.FieldAccessorTable(internal_static_netty_SubscribeReq_descriptor, new java.lang.String[] { "SubReqID", "UserName", "ProductName", "Address", });
				return null;
			}
		};
		com.google.protobuf.Descriptors.FileDescriptor.internalBuildGeneratedFileFrom(descriptorData, new com.google.protobuf.Descriptors.FileDescriptor[] {}, assigner);
	}
	
}


SubscribeRespProto.java文件内容如下:
package com.shihuan.netty.codec.protobuf;

public final class SubscribeRespProto {

	private SubscribeRespProto() {
		
	}

	public static void registerAllExtensions(com.google.protobuf.ExtensionRegistry registry) {
		
	}

	public interface SubscribeRespOrBuilder extends
			com.google.protobuf.MessageOrBuilder {

		// required int32 subReqID = 1;
		/**
		 * <code>required int32 subReqID = 1;</code>
		 */
		boolean hasSubReqID();

		/**
		 * <code>required int32 subReqID = 1;</code>
		 */
		int getSubReqID();

		// required int32 respCode = 2;
		/**
		 * <code>required int32 respCode = 2;</code>
		 */
		boolean hasRespCode();

		/**
		 * <code>required int32 respCode = 2;</code>
		 */
		int getRespCode();

		// required string desc = 3;
		/**
		 * <code>required string desc = 3;</code>
		 */
		boolean hasDesc();

		/**
		 * <code>required string desc = 3;</code>
		 */
		java.lang.String getDesc();

		/**
		 * <code>required string desc = 3;</code>
		 */
		com.google.protobuf.ByteString getDescBytes();
	}

	/**
	 * Protobuf type {@code netty.SubscribeResp}
	 */
	public static final class SubscribeResp extends
			com.google.protobuf.GeneratedMessage implements
			SubscribeRespOrBuilder {
		// Use SubscribeResp.newBuilder() to construct.
		private SubscribeResp(
				com.google.protobuf.GeneratedMessage.Builder<?> builder) {
			super(builder);
			this.unknownFields = builder.getUnknownFields();
		}

		private SubscribeResp(boolean noInit) {
			this.unknownFields = com.google.protobuf.UnknownFieldSet
					.getDefaultInstance();
		}

		private static final SubscribeResp defaultInstance;

		public static SubscribeResp getDefaultInstance() {
			return defaultInstance;
		}

		public SubscribeResp getDefaultInstanceForType() {
			return defaultInstance;
		}

		private final com.google.protobuf.UnknownFieldSet unknownFields;

		@java.lang.Override
		public final com.google.protobuf.UnknownFieldSet getUnknownFields() {
			return this.unknownFields;
		}

		private SubscribeResp(com.google.protobuf.CodedInputStream input,
				com.google.protobuf.ExtensionRegistryLite extensionRegistry)
				throws com.google.protobuf.InvalidProtocolBufferException {
			initFields();
			int mutable_bitField0_ = 0;
			com.google.protobuf.UnknownFieldSet.Builder unknownFields = com.google.protobuf.UnknownFieldSet
					.newBuilder();
			try {
				boolean done = false;
				while (!done) {
					int tag = input.readTag();
					switch (tag) {
					case 0:
						done = true;
						break;
					default: {
						if (!parseUnknownField(input, unknownFields,
								extensionRegistry, tag)) {
							done = true;
						}
						break;
					}
					case 8: {
						bitField0_ |= 0x00000001;
						subReqID_ = input.readInt32();
						break;
					}
					case 16: {
						bitField0_ |= 0x00000002;
						respCode_ = input.readInt32();
						break;
					}
					case 26: {
						bitField0_ |= 0x00000004;
						desc_ = input.readBytes();
						break;
					}
					}
				}
			} catch (com.google.protobuf.InvalidProtocolBufferException e) {
				throw e.setUnfinishedMessage(this);
			} catch (java.io.IOException e) {
				throw new com.google.protobuf.InvalidProtocolBufferException(
						e.getMessage()).setUnfinishedMessage(this);
			} finally {
				this.unknownFields = unknownFields.build();
				makeExtensionsImmutable();
			}
		}

		public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() {
			return com.shihuan.netty.codec.protobuf.SubscribeRespProto.internal_static_netty_SubscribeResp_descriptor;
		}

		protected com.google.protobuf.GeneratedMessage.FieldAccessorTable internalGetFieldAccessorTable() {
			return com.shihuan.netty.codec.protobuf.SubscribeRespProto.internal_static_netty_SubscribeResp_fieldAccessorTable
					.ensureFieldAccessorsInitialized(
							com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp.class,
							com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp.Builder.class);
		}

		public static com.google.protobuf.Parser<SubscribeResp> PARSER = new com.google.protobuf.AbstractParser<SubscribeResp>() {
			public SubscribeResp parsePartialFrom(
					com.google.protobuf.CodedInputStream input,
					com.google.protobuf.ExtensionRegistryLite extensionRegistry)
					throws com.google.protobuf.InvalidProtocolBufferException {
				return new SubscribeResp(input, extensionRegistry);
			}
		};

		@java.lang.Override
		public com.google.protobuf.Parser<SubscribeResp> getParserForType() {
			return PARSER;
		}

		private int bitField0_;
		// required int32 subReqID = 1;
		public static final int SUBREQID_FIELD_NUMBER = 1;
		private int subReqID_;

		/**
		 * <code>required int32 subReqID = 1;</code>
		 */
		public boolean hasSubReqID() {
			return ((bitField0_ & 0x00000001) == 0x00000001);
		}

		/**
		 * <code>required int32 subReqID = 1;</code>
		 */
		public int getSubReqID() {
			return subReqID_;
		}

		// required int32 respCode = 2;
		public static final int RESPCODE_FIELD_NUMBER = 2;
		private int respCode_;

		/**
		 * <code>required int32 respCode = 2;</code>
		 */
		public boolean hasRespCode() {
			return ((bitField0_ & 0x00000002) == 0x00000002);
		}

		/**
		 * <code>required int32 respCode = 2;</code>
		 */
		public int getRespCode() {
			return respCode_;
		}

		// required string desc = 3;
		public static final int DESC_FIELD_NUMBER = 3;
		private java.lang.Object desc_;

		/**
		 * <code>required string desc = 3;</code>
		 */
		public boolean hasDesc() {
			return ((bitField0_ & 0x00000004) == 0x00000004);
		}

		/**
		 * <code>required string desc = 3;</code>
		 */
		public java.lang.String getDesc() {
			java.lang.Object ref = desc_;
			if (ref instanceof java.lang.String) {
				return (java.lang.String) ref;
			} else {
				com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref;
				java.lang.String s = bs.toStringUtf8();
				if (bs.isValidUtf8()) {
					desc_ = s;
				}
				return s;
			}
		}

		/**
		 * <code>required string desc = 3;</code>
		 */
		public com.google.protobuf.ByteString getDescBytes() {
			java.lang.Object ref = desc_;
			if (ref instanceof java.lang.String) {
				com.google.protobuf.ByteString b = com.google.protobuf.ByteString
						.copyFromUtf8((java.lang.String) ref);
				desc_ = b;
				return b;
			} else {
				return (com.google.protobuf.ByteString) ref;
			}
		}

		private void initFields() {
			subReqID_ = 0;
			respCode_ = 0;
			desc_ = "";
		}

		private byte memoizedIsInitialized = -1;

		public final boolean isInitialized() {
			byte isInitialized = memoizedIsInitialized;
			if (isInitialized != -1)
				return isInitialized == 1;

			if (!hasSubReqID()) {
				memoizedIsInitialized = 0;
				return false;
			}
			if (!hasRespCode()) {
				memoizedIsInitialized = 0;
				return false;
			}
			if (!hasDesc()) {
				memoizedIsInitialized = 0;
				return false;
			}
			memoizedIsInitialized = 1;
			return true;
		}

		public void writeTo(com.google.protobuf.CodedOutputStream output)
				throws java.io.IOException {
			getSerializedSize();
			if (((bitField0_ & 0x00000001) == 0x00000001)) {
				output.writeInt32(1, subReqID_);
			}
			if (((bitField0_ & 0x00000002) == 0x00000002)) {
				output.writeInt32(2, respCode_);
			}
			if (((bitField0_ & 0x00000004) == 0x00000004)) {
				output.writeBytes(3, getDescBytes());
			}
			getUnknownFields().writeTo(output);
		}

		private int memoizedSerializedSize = -1;

		public int getSerializedSize() {
			int size = memoizedSerializedSize;
			if (size != -1)
				return size;

			size = 0;
			if (((bitField0_ & 0x00000001) == 0x00000001)) {
				size += com.google.protobuf.CodedOutputStream.computeInt32Size(
						1, subReqID_);
			}
			if (((bitField0_ & 0x00000002) == 0x00000002)) {
				size += com.google.protobuf.CodedOutputStream.computeInt32Size(
						2, respCode_);
			}
			if (((bitField0_ & 0x00000004) == 0x00000004)) {
				size += com.google.protobuf.CodedOutputStream.computeBytesSize(
						3, getDescBytes());
			}
			size += getUnknownFields().getSerializedSize();
			memoizedSerializedSize = size;
			return size;
		}

		private static final long serialVersionUID = 0L;

		@java.lang.Override
		protected java.lang.Object writeReplace()
				throws java.io.ObjectStreamException {
			return super.writeReplace();
		}

		public static com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp parseFrom(
				com.google.protobuf.ByteString data)
				throws com.google.protobuf.InvalidProtocolBufferException {
			return PARSER.parseFrom(data);
		}

		public static com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp parseFrom(
				com.google.protobuf.ByteString data,
				com.google.protobuf.ExtensionRegistryLite extensionRegistry)
				throws com.google.protobuf.InvalidProtocolBufferException {
			return PARSER.parseFrom(data, extensionRegistry);
		}

		public static com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp parseFrom(
				byte[] data)
				throws com.google.protobuf.InvalidProtocolBufferException {
			return PARSER.parseFrom(data);
		}

		public static com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp parseFrom(
				byte[] data,
				com.google.protobuf.ExtensionRegistryLite extensionRegistry)
				throws com.google.protobuf.InvalidProtocolBufferException {
			return PARSER.parseFrom(data, extensionRegistry);
		}

		public static com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp parseFrom(
				java.io.InputStream input) throws java.io.IOException {
			return PARSER.parseFrom(input);
		}

		public static com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp parseFrom(
				java.io.InputStream input,
				com.google.protobuf.ExtensionRegistryLite extensionRegistry)
				throws java.io.IOException {
			return PARSER.parseFrom(input, extensionRegistry);
		}

		public static com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp parseDelimitedFrom(
				java.io.InputStream input) throws java.io.IOException {
			return PARSER.parseDelimitedFrom(input);
		}

		public static com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp parseDelimitedFrom(
				java.io.InputStream input,
				com.google.protobuf.ExtensionRegistryLite extensionRegistry)
				throws java.io.IOException {
			return PARSER.parseDelimitedFrom(input, extensionRegistry);
		}

		public static com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp parseFrom(
				com.google.protobuf.CodedInputStream input)
				throws java.io.IOException {
			return PARSER.parseFrom(input);
		}

		public static com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp parseFrom(
				com.google.protobuf.CodedInputStream input,
				com.google.protobuf.ExtensionRegistryLite extensionRegistry)
				throws java.io.IOException {
			return PARSER.parseFrom(input, extensionRegistry);
		}

		public static Builder newBuilder() {
			return Builder.create();
		}

		public Builder newBuilderForType() {
			return newBuilder();
		}

		public static Builder newBuilder(
				com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp prototype) {
			return newBuilder().mergeFrom(prototype);
		}

		public Builder toBuilder() {
			return newBuilder(this);
		}

		@java.lang.Override
		protected Builder newBuilderForType(
				com.google.protobuf.GeneratedMessage.BuilderParent parent) {
			Builder builder = new Builder(parent);
			return builder;
		}

		/**
		 * Protobuf type {@code netty.SubscribeResp}
		 */
		public static final class Builder extends
				com.google.protobuf.GeneratedMessage.Builder<Builder>
				implements
				com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeRespOrBuilder {
			public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() {
				return com.shihuan.netty.codec.protobuf.SubscribeRespProto.internal_static_netty_SubscribeResp_descriptor;
			}

			protected com.google.protobuf.GeneratedMessage.FieldAccessorTable internalGetFieldAccessorTable() {
				return com.shihuan.netty.codec.protobuf.SubscribeRespProto.internal_static_netty_SubscribeResp_fieldAccessorTable
						.ensureFieldAccessorsInitialized(
								com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp.class,
								com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp.Builder.class);
			}

			// Construct using
			// com.phei.netty.codec.protobuf.SubscribeRespProto.SubscribeResp.newBuilder()
			private Builder() {
				maybeForceBuilderInitialization();
			}

			private Builder(
					com.google.protobuf.GeneratedMessage.BuilderParent parent) {
				super(parent);
				maybeForceBuilderInitialization();
			}

			private void maybeForceBuilderInitialization() {
				if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {
				}
			}

			private static Builder create() {
				return new Builder();
			}

			public Builder clear() {
				super.clear();
				subReqID_ = 0;
				bitField0_ = (bitField0_ & ~0x00000001);
				respCode_ = 0;
				bitField0_ = (bitField0_ & ~0x00000002);
				desc_ = "";
				bitField0_ = (bitField0_ & ~0x00000004);
				return this;
			}

			public Builder clone() {
				return create().mergeFrom(buildPartial());
			}

			public com.google.protobuf.Descriptors.Descriptor getDescriptorForType() {
				return com.shihuan.netty.codec.protobuf.SubscribeRespProto.internal_static_netty_SubscribeResp_descriptor;
			}

			public com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp getDefaultInstanceForType() {
				return com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp
						.getDefaultInstance();
			}

			public com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp build() {
				com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp result = buildPartial();
				if (!result.isInitialized()) {
					throw newUninitializedMessageException(result);
				}
				return result;
			}

			public com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp buildPartial() {
				com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp result = new com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp(
						this);
				int from_bitField0_ = bitField0_;
				int to_bitField0_ = 0;
				if (((from_bitField0_ & 0x00000001) == 0x00000001)) {
					to_bitField0_ |= 0x00000001;
				}
				result.subReqID_ = subReqID_;
				if (((from_bitField0_ & 0x00000002) == 0x00000002)) {
					to_bitField0_ |= 0x00000002;
				}
				result.respCode_ = respCode_;
				if (((from_bitField0_ & 0x00000004) == 0x00000004)) {
					to_bitField0_ |= 0x00000004;
				}
				result.desc_ = desc_;
				result.bitField0_ = to_bitField0_;
				onBuilt();
				return result;
			}

			public Builder mergeFrom(com.google.protobuf.Message other) {
				if (other instanceof com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp) {
					return mergeFrom((com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp) other);
				} else {
					super.mergeFrom(other);
					return this;
				}
			}

			public Builder mergeFrom(
					com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp other) {
				if (other == com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp
						.getDefaultInstance())
					return this;
				if (other.hasSubReqID()) {
					setSubReqID(other.getSubReqID());
				}
				if (other.hasRespCode()) {
					setRespCode(other.getRespCode());
				}
				if (other.hasDesc()) {
					bitField0_ |= 0x00000004;
					desc_ = other.desc_;
					onChanged();
				}
				this.mergeUnknownFields(other.getUnknownFields());
				return this;
			}

			public final boolean isInitialized() {
				if (!hasSubReqID()) {

					return false;
				}
				if (!hasRespCode()) {

					return false;
				}
				if (!hasDesc()) {

					return false;
				}
				return true;
			}

			public Builder mergeFrom(
					com.google.protobuf.CodedInputStream input,
					com.google.protobuf.ExtensionRegistryLite extensionRegistry)
					throws java.io.IOException {
				com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp parsedMessage = null;
				try {
					parsedMessage = PARSER.parsePartialFrom(input,
							extensionRegistry);
				} catch (com.google.protobuf.InvalidProtocolBufferException e) {
					parsedMessage = (com.shihuan.netty.codec.protobuf.SubscribeRespProto.SubscribeResp) e.getUnfinishedMessage();
					throw e;
				} finally {
					if (parsedMessage != null) {
						mergeFrom(parsedMessage);
					}
				}
				return this;
			}

			private int bitField0_;

			// required int32 subReqID = 1;
			private int subReqID_;

			/**
			 * <code>required int32 subReqID = 1;</code>
			 */
			public boolean hasSubReqID() {
				return ((bitField0_ & 0x00000001) == 0x00000001);
			}

			/**
			 * <code>required int32 subReqID = 1;</code>
			 */
			public int getSubReqID() {
				return subReqID_;
			}

			/**
			 * <code>required int32 subReqID = 1;</code>
			 */
			public Builder setSubReqID(int value) {
				bitField0_ |= 0x00000001;
				subReqID_ = value;
				onChanged();
				return this;
			}

			/**
			 * <code>required int32 subReqID = 1;</code>
			 */
			public Builder clearSubReqID() {
				bitField0_ = (bitField0_ & ~0x00000001);
				subReqID_ = 0;
				onChanged();
				return this;
			}

			// required int32 respCode = 2;
			private int respCode_;

			/**
			 * <code>required int32 respCode = 2;</code>
			 */
			public boolean hasRespCode() {
				return ((bitField0_ & 0x00000002) == 0x00000002);
			}

			/**
			 * <code>required int32 respCode = 2;</code>
			 */
			public int getRespCode() {
				return respCode_;
			}

			/**
			 * <code>required int32 respCode = 2;</code>
			 */
			public Builder setRespCode(int value) {
				bitField0_ |= 0x00000002;
				respCode_ = value;
				onChanged();
				return this;
			}

			/**
			 * <code>required int32 respCode = 2;</code>
			 */
			public Builder clearRespCode() {
				bitField0_ = (bitField0_ & ~0x00000002);
				respCode_ = 0;
				onChanged();
				return this;
			}

			// required string desc = 3;
			private java.lang.Object desc_ = "";

			/**
			 * <code>required string desc = 3;</code>
			 */
			public boolean hasDesc() {
				return ((bitField0_ & 0x00000004) == 0x00000004);
			}

			/**
			 * <code>required string desc = 3;</code>
			 */
			public java.lang.String getDesc() {
				java.lang.Object ref = desc_;
				if (!(ref instanceof java.lang.String)) {
					java.lang.String s = ((com.google.protobuf.ByteString) ref)
							.toStringUtf8();
					desc_ = s;
					return s;
				} else {
					return (java.lang.String) ref;
				}
			}

			/**
			 * <code>required string desc = 3;</code>
			 */
			public com.google.protobuf.ByteString getDescBytes() {
				java.lang.Object ref = desc_;
				if (ref instanceof String) {
					com.google.protobuf.ByteString b = com.google.protobuf.ByteString
							.copyFromUtf8((java.lang.String) ref);
					desc_ = b;
					return b;
				} else {
					return (com.google.protobuf.ByteString) ref;
				}
			}

			/**
			 * <code>required string desc = 3;</code>
			 */
			public Builder setDesc(java.lang.String value) {
				if (value == null) {
					throw new NullPointerException();
				}
				bitField0_ |= 0x00000004;
				desc_ = value;
				onChanged();
				return this;
			}

			/**
			 * <code>required string desc = 3;</code>
			 */
			public Builder clearDesc() {
				bitField0_ = (bitField0_ & ~0x00000004);
				desc_ = getDefaultInstance().getDesc();
				onChanged();
				return this;
			}

			/**
			 * <code>required string desc = 3;</code>
			 */
			public Builder setDescBytes(com.google.protobuf.ByteString value) {
				if (value == null) {
					throw new NullPointerException();
				}
				bitField0_ |= 0x00000004;
				desc_ = value;
				onChanged();
				return this;
			}

			// @@protoc_insertion_point(builder_scope:netty.SubscribeResp)
		}

		static {
			defaultInstance = new SubscribeResp(true);
			defaultInstance.initFields();
		}

		// @@protoc_insertion_point(class_scope:netty.SubscribeResp)
	}

	private static com.google.protobuf.Descriptors.Descriptor internal_static_netty_SubscribeResp_descriptor;
	private static com.google.protobuf.GeneratedMessage.FieldAccessorTable internal_static_netty_SubscribeResp_fieldAccessorTable;

	public static com.google.protobuf.Descriptors.FileDescriptor getDescriptor() {
		return descriptor;
	}

	private static com.google.protobuf.Descriptors.FileDescriptor descriptor;
	static {
		java.lang.String[] descriptorData = { "\n\031netty/SubscribeResp.proto\022\005netty\"A\n\rSu"
				+ "bscribeResp\022\020\n\010subReqID\030\001 \002(\005\022\020\n\010respCod"
				+ "e\030\002 \002(\005\022\014\n\004desc\030\003 \002(\tB3\n\035com.phei.netty."
				+ "codec.protobufB\022SubscribeRespProto" };
		com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = new com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner() {
			public com.google.protobuf.ExtensionRegistry assignDescriptors(
					com.google.protobuf.Descriptors.FileDescriptor root) {
				descriptor = root;
				internal_static_netty_SubscribeResp_descriptor = getDescriptor()
						.getMessageTypes().get(0);
				internal_static_netty_SubscribeResp_fieldAccessorTable = new com.google.protobuf.GeneratedMessage.FieldAccessorTable(
						internal_static_netty_SubscribeResp_descriptor,
						new java.lang.String[] { "SubReqID", "RespCode",
								"Desc", });
				return null;
			}
		};
		com.google.protobuf.Descriptors.FileDescriptor
				.internalBuildGeneratedFileFrom(
						descriptorData,
						new com.google.protobuf.Descriptors.FileDescriptor[] {},
						assigner);
	}
	
}


SubReqServerHandler.java文件内容如下:
package com.shihuan.netty.codec.protobuf.server;

import com.shihuan.netty.codec.protobuf.SubscribeReqProto;
import com.shihuan.netty.codec.protobuf.SubscribeRespProto;

import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

@Sharable
public class SubReqServerHandler extends ChannelHandlerAdapter {

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		SubscribeReqProto.SubscribeReq req = (SubscribeReqProto.SubscribeReq) msg;
		if ("Lilinfeng".equalsIgnoreCase(req.getUserName())) {
			System.out.println("Service accept client subscribe req : [" + req.toString() + "]");
			ctx.writeAndFlush(resp(req.getSubReqID()));
		}
	}

	private SubscribeRespProto.SubscribeResp resp(int subReqID) {
		SubscribeRespProto.SubscribeResp.Builder builder = SubscribeRespProto.SubscribeResp.newBuilder();
		builder.setSubReqID(subReqID);
		builder.setRespCode(0);
		builder.setDesc("Netty book order succeed, 3 days later, sent to the designated address");
		return builder.build();
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
		cause.printStackTrace();
		ctx.close();// 发生异常,关闭链路
	}
	
}


SubReqServer.java文件内容如下:
package com.shihuan.netty.codec.protobuf.server;

import com.shihuan.netty.codec.protobuf.SubscribeReqProto;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

public class SubReqServer {

	public void bind(int port) throws Exception {
		// 配置服务端的NIO线程组
		EventLoopGroup bossGroup = new NioEventLoopGroup();
		EventLoopGroup workerGroup = new NioEventLoopGroup();
		try {
			ServerBootstrap b = new ServerBootstrap();
			b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {
				@Override
				public void initChannel(SocketChannel ch) {
					ch.pipeline().addLast(new ProtobufVarint32FrameDecoder());   //处理半包粘包的消息
					ch.pipeline().addLast(new ProtobufDecoder(SubscribeReqProto.SubscribeReq.getDefaultInstance()));
					ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
					ch.pipeline().addLast(new ProtobufEncoder());
					ch.pipeline().addLast(new SubReqServerHandler());
				}
			});

			// 绑定端口,同步等待成功
			ChannelFuture f = b.bind(port).sync();

			// 等待服务端监听端口关闭
			f.channel().closeFuture().sync();
		} finally {
			// 优雅退出,释放线程池资源
			bossGroup.shutdownGracefully();
			workerGroup.shutdownGracefully();
		}
	}
	
	public static void main(String[] args) throws Exception {
		int port = 8080;
		if (args != null && args.length > 0) {
			try {
				port = Integer.valueOf(args[0]);
			} catch (NumberFormatException e) {
				// 采用默认值
			}
		}
		new SubReqServer().bind(port);
	}

}


SubReqClientHandler.java文件内容如下:
package com.shihuan.netty.codec.protobuf.client;

import java.util.ArrayList;
import java.util.List;

import com.shihuan.netty.codec.protobuf.SubscribeReqProto;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class SubReqClientHandler extends ChannelHandlerAdapter {

	/**
	 * Creates a client-side handler.
	 */
	public SubReqClientHandler() {
		
	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) {
		for (int i = 0; i < 10; i++) {
			ctx.write(subReq(i));
		}
		ctx.flush();
	}

	private SubscribeReqProto.SubscribeReq subReq(int i) {
		SubscribeReqProto.SubscribeReq.Builder builder = SubscribeReqProto.SubscribeReq.newBuilder();
		builder.setSubReqID(i);
		builder.setUserName("Lilinfeng");
		builder.setProductName("Netty Book For Protobuf");
		List<String> address = new ArrayList<>();
		address.add("NanJing YuHuaTai");
		address.add("BeiJing LiuLiChang");
		address.add("ShenZhen HongShuLin");
		builder.addAllAddress(address);
		return builder.build();
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		System.out.println("Receive server response : [" + msg + "]");
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		ctx.flush();
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
		cause.printStackTrace();
		ctx.close();
	}
	
}


SubReqClient.java文件内容如下:
package com.shihuan.netty.codec.protobuf.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;

import com.shihuan.netty.codec.protobuf.SubscribeRespProto;

public class SubReqClient {

	public void connect(int port, String host) throws Exception {
		// 配置客户端NIO线程组
		EventLoopGroup group = new NioEventLoopGroup();
		try {
			Bootstrap b = new Bootstrap();
			b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() {
				@Override
				public void initChannel(SocketChannel ch) throws Exception {
					ch.pipeline().addLast(new ProtobufVarint32FrameDecoder());
					ch.pipeline().addLast(new ProtobufDecoder(SubscribeRespProto.SubscribeResp.getDefaultInstance()));
					ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
					ch.pipeline().addLast(new ProtobufEncoder());
					ch.pipeline().addLast(new SubReqClientHandler());
				}
			});

			// 发起异步连接操作
			ChannelFuture f = b.connect(host, port).sync();

			// 当代客户端链路关闭
			f.channel().closeFuture().sync();
		} finally {
			// 优雅退出,释放NIO线程组
			group.shutdownGracefully();
		}
	}
	
	public static void main(String[] args) throws Exception {
		int port = 8080;
		if (args != null && args.length > 0) {
			try {
				port = Integer.valueOf(args[0]);
			} catch (NumberFormatException e) {
				// 采用默认值
			}
		}
		new SubReqClient().connect(port, "127.0.0.1");
	}

}


TestSubscribeReqProto.java文件内容如下:
package com.shihuan.netty.codec.protobuf.test;

import java.util.ArrayList;
import java.util.List;

import com.google.protobuf.InvalidProtocolBufferException;
import com.shihuan.netty.codec.protobuf.SubscribeReqProto;

public class TestSubscribeReqProto {

	private static byte[] encode(SubscribeReqProto.SubscribeReq req) {
		return req.toByteArray();
	}

	private static SubscribeReqProto.SubscribeReq decode(byte[] body) throws InvalidProtocolBufferException {
		return SubscribeReqProto.SubscribeReq.parseFrom(body);
	}

	private static SubscribeReqProto.SubscribeReq createSubscribeReq() {
		SubscribeReqProto.SubscribeReq.Builder builder = SubscribeReqProto.SubscribeReq.newBuilder();
		builder.setSubReqID(1);
		builder.setUserName("Lilinfeng");
		builder.setProductName("Netty Book");
		List<String> address = new ArrayList<>();
		address.add("NanJing YuHuaTai");
		address.add("BeiJing LiuLiChang");
		address.add("ShenZhen HongShuLin");
		builder.addAllAddress(address);
		return builder.build();
	}
	
	public static void main(String[] args) throws InvalidProtocolBufferException {
		SubscribeReqProto.SubscribeReq req = createSubscribeReq();
		System.out.println("Before encode : " + req.toString());
		SubscribeReqProto.SubscribeReq req2 = decode(encode(req));
		System.out.println("After decode : " + req.toString());
		System.out.println("Assert equal : --> " + req2.equals(req));
	}

}


【注】: 附件里protobuf.rar是可运行工程源代码。
  • 大小: 32.7 KB
分享到:
评论

相关推荐

    Netty+自定义Protobuf编解码器

    综上所述,"Netty+自定义Protobuf编解码器"的主题涉及到如何在Netty框架下创建一个能够处理多种protobuf消息类型的编解码器,这将极大地增强网络通信的灵活性。通过自定义编解码器,我们可以根据实际需求发送和接收...

    netty http protobuf

    Netty、HTTP与Protobuf是三个在IT领域中至关重要的技术组件,它们分别在不同的层面上为高性能网络应用提供服务。下面将详细解释这三个概念及其相互结合的应用。 **Netty** Netty是一个开源的Java NIO(非阻塞I/O)...

    Netty发送protoBuf格式数据

    3. **创建ProtoBuf编码解码器**:在Netty中,我们需要自定义编码器和解码器来处理ProtoBuf格式的数据。可以继承`ByteToMessageDecoder`和`MessageToByteEncoder`,重写其方法。编码器将Java对象转换为ByteBuf,解码...

    netty+protobuf (整合源代码)

    处理器链中,我们可以自定义一个 ChannelInboundHandler 来处理接收到的 Protobuf 数据,并进行解码。同样,客户端也会设置一个 Bootstrap,连接到服务器,发送和接收 Protobuf 编码的数据。 总的来说,这个项目...

    Netty中Marshalling编解码应用

    总的来说,Netty的Marshalling编解码应用是网络编程中的一种高级技巧,能够帮助开发者更高效地处理Java对象在网络中的传输。通过深入学习和实践,你可以掌握这一技术,并将其应用于分布式系统、微服务架构等多种场景...

    netty+protobuf入门案例

    3. **创建 Netty 通道处理器**:在 Netty 中,我们需要创建一个自定义的 `ChannelInboundHandler` 或 `ChannelOutboundHandler` 来处理 Protobuf 消息的编码和解码。可以使用 ` ProtobufVarint32FrameDecoder` 和 `...

    采用netty与protobuf进行文件传输

    服务端(rrkd-file-server)接收到数据后,同样利用Netty的事件驱动模型,将接收到的字节流转交给Protobuf的解码器,还原成原始文件内容。这个过程可能涉及多个网络包的合并,Netty的流式处理能力在此处得到体现,...

    Netty中Marshalling编解码自定义栈应用

    本文将深入探讨 Marshalling 编解码器在 Netty 中的应用及其自定义实现。 Marshalling 是一种序列化技术,它能够将 Java 对象转换为字节流,便于在网络中传输,同时也可以将字节流恢复为原来的对象。在 Netty 中,...

    springboot集成netty,使用protobuf作为数据交换格式,可以用于智能终端云端服务脚手架

    3. 配置Spring Boot:在Spring Boot的配置文件中添加Netty和protobuf的相关依赖,配置服务器端口和protobuf的编码解码器。 4. 创建Netty Server:编写Netty服务器端代码,使用protobuf的Decoder和Encoder处理进来的...

    基于netty与protobuf的Android手机视频实时传输

    在现代移动应用开发中,实现实时视频传输是一项挑战性的任务,尤其是在Android平台上。本项目“基于Netty与Protobuf的Android手机视频实时传输”利用了高性能的网络库Netty和高效的序列化框架Protobuf,实现了高效、...

    netty基于protobuf的简单示例

    在本示例中,我们将深入探讨如何利用 Netty 和 Google 的 Protocol Buffers(protobuf)来构建一个简单的服务端和客户端通信系统。 Protocol Buffers 是 Google 提供的一种数据序列化协议,它可以将结构化数据序列...

    通信与协议Netty+Protobuf-游戏设计与开发(1)配套代码

    本资源主要关注的是利用Netty框架和Protocol Buffers(Protobuf)进行网络通信,这在游戏开发中是一种常见的高性能解决方案。 Netty是一个Java开源框架,它提供了一个异步事件驱动的网络应用程序框架,用于快速开发...

    基于netty和protobuf的聊天系统,客户端+服务器

    通过这个项目,开发者不仅可以学习到如何使用Netty构建网络应用,还能掌握protobuf在实际项目中的应用,理解数据序列化和反序列化的流程。同时,这个聊天系统也可以作为进一步开发分布式系统、实时通信应用的基础,...

    netty+protobuf入门案例.

    Netty和Protobuf是两种在IT领域中广泛使用的开源技术,尤其在开发高效、高性能的网络应用程序时。本文将深入探讨这两个技术,并结合一个入门案例,帮助初学者理解如何将它们结合起来使用。 Netty是一个高性能、异步...

    netty学习文件,实现http,websocket,protobuf

    // 添加Protobuf解码器和编码器 p.addLast(new ProtobufVarint32FrameDecoder()); p.addLast(new ProtobufDecoder(YourMessageProto.YourMessage.getDefaultInstance())); p.addLast(new ProtobufVarint32...

    Netty中集成Protobuf实现Java对象数据传递示例代码.rar

    在Netty中集成Protobuf,主要目的是利用Protobuf的数据序列化能力,将Java对象转换为字节流,然后通过Netty的Channel发送到网络,接收端再反序列化恢复成原来的对象。具体步骤如下: 1. 定义消息:首先创建一个....

    基于Google Protobuf的Netty编解码技术

    NULL 博文链接:https://hbxflihua.iteye.com/blog/2231878

    游戏开发中的protobuf自动解码DEMO

    在"NettyDecode"这个压缩包中,我们可以推断出它可能包含了一个使用Netty框架实现的protobuf解码示例。Netty是一个高性能、异步的网络应用框架,常用于构建高并发、低延迟的网络服务,如游戏服务器。将protobuf与...

    springboot集成netty,使用protobuf作为数据传输格式,包含心跳检测、断开重连等功能..zip

    在Spring Boot和Netty结合的场景下,protobuf可以用于编码和解码消息,确保在网络中高效地传输数据。 集成步骤通常包括以下几个关键部分: 1. **protobuf的使用**:定义protobuf消息模型,创建.proto文件,然后...

Global site tag (gtag.js) - Google Analytics