@@ -40,8 +40,6 @@ std::string GearmanEncoder::toString() const {
40
40
return " GearmanEncoder" ;
41
41
}
42
42
43
- static const std::string REQUEST_MAGIC (" \0 REQ" , 4 );
44
-
45
43
ChannelMessage GearmanEncoder::encode (ChannelHandlerContext& ctx,
46
44
const ChannelPtr& channel,
47
45
const ChannelMessage& msg) {
@@ -55,23 +53,25 @@ ChannelMessage GearmanEncoder::encode(ChannelHandlerContext& ctx,
55
53
int messageLength = 0 ;
56
54
57
55
if (message->hasData ()) {
58
- const ChannelBufferPtr data = message->getData ();
56
+ const ChannelBufferPtr& data = message->getData ();
59
57
bodyLenth += data->readableBytes ();
60
- messageLength = bodyLenth + headerLength ;
58
+ messageLength = bodyLenth + parametersLength ;
61
59
62
60
if (data->aheadWritableBytes () >= parametersLength + headerLength) {
63
- data->offsetWriterIndex (-parametersLength - headerLength);
64
-
65
- writeHeader (data, message->getType (), messageLength);
66
- writeParameters (data, message->getParameters (), true );
67
-
61
+ if (parametersLength) {
62
+ writeParametersAhead (data, message->getParameters (), true );
63
+ }
64
+ writeHeaderAhead (data, message->getType (), messageLength);
68
65
return ChannelMessage (data);
69
66
}
70
67
else {
71
68
ChannelBufferPtr buffer = ChannelBuffers::buffer (bodyLenth + headerLength);
72
69
73
70
writeHeader (buffer, message->getType (), messageLength);
74
- writeParameters (buffer, message->getParameters (), true );
71
+ if (parametersLength) {
72
+ writeParameters (buffer, message->getParameters (), true );
73
+ }
74
+
75
75
buffer->writeBytes (data);
76
76
77
77
return ChannelMessage (buffer);
@@ -82,7 +82,10 @@ ChannelMessage GearmanEncoder::encode(ChannelHandlerContext& ctx,
82
82
messageLength = bodyLenth + headerLength;
83
83
ChannelBufferPtr buffer = ChannelBuffers::buffer (messageLength);
84
84
writeHeader (buffer, message->getType (), messageLength);
85
- writeParameters (buffer, message->getParameters (), false );
85
+
86
+ if (messageLength) {
87
+ writeParameters (buffer, message->getParameters (), false );
88
+ }
86
89
87
90
return ChannelMessage (buffer);
88
91
}
@@ -105,15 +108,22 @@ int GearmanEncoder::caculateParametersLength(const GearmanMessagePtr& msg) {
105
108
}
106
109
107
110
void GearmanEncoder::writeHeader (const ChannelBufferPtr& buffer, int type, int length) {
108
- buffer->writeBytes (REQUEST_MAGIC);
111
+ buffer->writeBytes (GearmanMessage:: REQUEST_MAGIC);
109
112
buffer->writeInt (type);
110
113
buffer->writeInt (length);
111
114
}
112
115
116
+ void GearmanEncoder::writeHeaderAhead (const ChannelBufferPtr& buffer, int type, int length) {
117
+ buffer->writeIntAhead (length);
118
+ buffer->writeIntAhead (type);
119
+ buffer->writeBytesAhead (GearmanMessage::REQUEST_MAGIC);
120
+ }
121
+
113
122
void GearmanEncoder::writeParameters (const ChannelBufferPtr& buffer,
114
123
const std::vector<std::string>& parameters,
115
124
bool withZeroPad) {
116
- for (int i = 0 , j = parameters.size (); i < j; ++i) {
125
+ int j = parameters.size ();
126
+ for (int i = 0 ; i < j; ++i) {
117
127
buffer->writeBytes (parameters[i]);
118
128
119
129
if (!withZeroPad && ((j - i) == 1 )) {
@@ -125,6 +135,22 @@ void GearmanEncoder::writeParameters(const ChannelBufferPtr& buffer,
125
135
}
126
136
}
127
137
138
+ void GearmanEncoder::writeParametersAhead (const ChannelBufferPtr& buffer,
139
+ const std::vector<std::string>& parameters,
140
+ bool withZeroPad) {
141
+ int j = parameters.size ();
142
+ if (j == 0 ) return ;
143
+
144
+ if (withZeroPad) {
145
+ buffer->writeByteAhead (0 );
146
+ }
147
+ buffer->writeBytesAhead (parameters[j - 1 ]);
148
+
149
+ for (int i = j - 2 ; i >= 0 ; --i) {
150
+ buffer->writeByteAhead (0 );
151
+ buffer->writeBytesAhead (parameters[i]);
152
+ }
153
+ }
128
154
129
155
130
156
}
0 commit comments