2222import com .hivemq .client .internal .logging .InternalLoggerFactory ;
2323import com .hivemq .client .internal .mqtt .ioc .ClientScope ;
2424import com .hivemq .client .internal .mqtt .message .publish .MqttPublish ;
25- import com .hivemq .client .internal .mqtt .message .publish .MqttStatefulIncomingPublish ;
2625import com .hivemq .client .internal .util .collections .ChunkedArrayQueue ;
2726import com .hivemq .client .internal .util .collections .HandleList .Handle ;
2827import com .hivemq .client .mqtt .datatypes .MqttQos ;
@@ -61,13 +60,12 @@ class MqttIncomingPublishService {
6160 }
6261
6362 @ CallByThread ("Netty EventLoop" )
64- void onPublishQos0 (final @ NotNull MqttStatefulIncomingPublish publish , final int receiveMaximum ) {
65- if (qos0Queue .size () >= ( 2 * receiveMaximum ) ) { // TODO receiveMaximum
63+ void onPublishQos0 (final @ NotNull MqttStatefulPublishWithFlows publishWithFlows , final int receiveMaximum ) {
64+ if (qos0Queue .size () >= receiveMaximum ) { // TODO receiveMaximum
6665 LOGGER .warn ("QoS 0 publish message dropped." );
6766 if (QOS_0_DROP_OLDEST ) {
6867 qos0It .reset ();
69- qos0It .next ();
70- final MqttMatchingPublishFlows flows = (MqttMatchingPublishFlows ) qos0It .next ();
68+ final MqttStatefulPublishWithFlows flows = (MqttStatefulPublishWithFlows ) qos0It .next ();
7169 qos0It .remove ();
7270 for (Handle <MqttIncomingPublishFlow > h = flows .getFirst (); h != null ; h = h .getNext ()) {
7371 if (h .getElement ().dereference () == 0 ) {
@@ -78,43 +76,40 @@ void onPublishQos0(final @NotNull MqttStatefulIncomingPublish publish, final int
7876 return ;
7977 }
8078 }
81- final MqttMatchingPublishFlows flows = onPublish (publish );
82- if (!flows .isEmpty ()) {
83- qos0Queue .offer (publish );
84- qos0Queue .offer (flows );
79+ onPublish (publishWithFlows );
80+ if (!publishWithFlows .isEmpty ()) {
81+ qos0Queue .offer (publishWithFlows );
8582 }
8683 }
8784
8885 @ CallByThread ("Netty EventLoop" )
89- boolean onPublishQos1Or2 (final @ NotNull MqttStatefulIncomingPublish publish , final int receiveMaximum ) {
90- if (qos1Or2Queue .size () >= ( 2 * receiveMaximum ) ) {
86+ boolean onPublishQos1Or2 (final @ NotNull MqttStatefulPublishWithFlows publishWithFlows , final int receiveMaximum ) {
87+ if (qos1Or2Queue .size () >= receiveMaximum ) {
9188 return false ; // flow control error
9289 }
93- publish . setId ( nextQoS1Or2PublishId ++) ;
94- final MqttMatchingPublishFlows flows = onPublish (publish );
95- if (qos1Or2Queue .isEmpty () && flows .isEmpty () && flows .areAcknowledged ()) {
96- incomingQosHandler .ack (publish );
90+ publishWithFlows . id = nextQoS1Or2PublishId ++;
91+ onPublish (publishWithFlows );
92+ if (qos1Or2Queue .isEmpty () && publishWithFlows .isEmpty () && publishWithFlows .areAcknowledged ()) {
93+ incomingQosHandler .ack (publishWithFlows );
9794 } else {
98- qos1Or2Queue .offer (publish );
99- qos1Or2Queue .offer (flows );
95+ qos1Or2Queue .offer (publishWithFlows );
10096 }
10197 return true ;
10298 }
10399
104100 @ CallByThread ("Netty EventLoop" )
105- private @ NotNull MqttMatchingPublishFlows onPublish (final @ NotNull MqttStatefulIncomingPublish publish ) {
106- final MqttMatchingPublishFlows flows = incomingPublishFlows .findMatching (publish );
107- if (flows .isEmpty ()) {
108- LOGGER .warn ("No publish flow registered for {}." , publish );
101+ private void onPublish (final @ NotNull MqttStatefulPublishWithFlows publishWithFlows ) {
102+ incomingPublishFlows .findMatching (publishWithFlows );
103+ if (publishWithFlows .isEmpty ()) {
104+ LOGGER .warn ("No publish flow registered for {}." , publishWithFlows . publish );
109105 }
110106 drain ();
111- for (Handle <MqttIncomingPublishFlow > h = flows .getFirst (); h != null ; h = h .getNext ()) {
107+ for (Handle <MqttIncomingPublishFlow > h = publishWithFlows .getFirst (); h != null ; h = h .getNext ()) {
112108 if (h .getElement ().reference () == 1 ) {
113109 referencedFlowCount ++;
114110 }
115111 }
116- emit (publish , flows );
117- return flows ;
112+ emit (publishWithFlows );
118113 }
119114
120115 @ CallByThread ("Netty EventLoop" )
@@ -124,22 +119,20 @@ void drain() {
124119
125120 qos1Or2It .reset ();
126121 while (qos1Or2It .hasNext ()) {
127- final MqttStatefulIncomingPublish publish = (MqttStatefulIncomingPublish ) qos1Or2It .next ();
128- final MqttMatchingPublishFlows flows = (MqttMatchingPublishFlows ) qos1Or2It .next ();
129- emit (publish , flows );
130- if ((qos1Or2It .getIterated () == 2 ) && flows .isEmpty () && flows .areAcknowledged ()) {
122+ final MqttStatefulPublishWithFlows publishWithFlows = (MqttStatefulPublishWithFlows ) qos1Or2It .next ();
123+ emit (publishWithFlows );
124+ if ((qos1Or2It .getIterated () == 1 ) && publishWithFlows .isEmpty () && publishWithFlows .areAcknowledged ()) {
131125 qos1Or2It .remove ();
132- incomingQosHandler .ack (publish );
126+ incomingQosHandler .ack (publishWithFlows );
133127 } else if (blockingFlowCount == referencedFlowCount ) {
134128 return ;
135129 }
136130 }
137131 qos0It .reset ();
138132 while (qos0It .hasNext ()) {
139- final MqttStatefulIncomingPublish publish = (MqttStatefulIncomingPublish ) qos0It .next ();
140- final MqttMatchingPublishFlows flows = (MqttMatchingPublishFlows ) qos0It .next ();
141- emit (publish , flows );
142- if ((qos0It .getIterated () == 2 ) && flows .isEmpty ()) {
133+ final MqttStatefulPublishWithFlows publishWithFlows = (MqttStatefulPublishWithFlows ) qos0It .next ();
134+ emit (publishWithFlows );
135+ if ((qos0It .getIterated () == 1 ) && publishWithFlows .isEmpty ()) {
143136 qos0It .remove ();
144137 } else if (blockingFlowCount == referencedFlowCount ) {
145138 return ;
@@ -148,32 +141,30 @@ void drain() {
148141 }
149142
150143 @ CallByThread ("Netty EventLoop" )
151- private void emit (
152- final @ NotNull MqttStatefulIncomingPublish statefulPublish , final @ NotNull MqttMatchingPublishFlows flows ) {
153-
154- for (Handle <MqttIncomingPublishFlow > h = flows .getFirst (); h != null ; h = h .getNext ()) {
144+ private void emit (final @ NotNull MqttStatefulPublishWithFlows publishWithFlows ) {
145+ for (Handle <MqttIncomingPublishFlow > h = publishWithFlows .getFirst (); h != null ; h = h .getNext ()) {
155146 final MqttIncomingPublishFlow flow = h .getElement ();
156147
157148 if (flow .isCancelled ()) {
158- flows .remove (h );
149+ publishWithFlows .remove (h );
159150 if (flow .dereference () == 0 ) {
160151 referencedFlowCount --;
161152 }
162153 } else {
163154 final long requested = flow .requested (runIndex );
164155 if (requested > 0 ) {
165- MqttPublish publish = statefulPublish .stateless ();
156+ MqttPublish publish = publishWithFlows . publish .stateless ();
166157 if (flow .manualAcknowledgement ) {
167158 final Confirmable confirmable ;
168159 if (publish .getQos () == MqttQos .AT_MOST_ONCE ) {
169160 confirmable = new MqttIncomingPublishConfirmable .Qos0 ();
170161 } else {
171- confirmable = new MqttIncomingPublishConfirmable (statefulPublish . getId (), flow , flows );
162+ confirmable = new MqttIncomingPublishConfirmable (flow , publishWithFlows );
172163 }
173164 publish = publish .withConfirmable (confirmable );
174165 }
175166 flow .onNext (publish );
176- flows .remove (h );
167+ publishWithFlows .remove (h );
177168 if (flow .dereference () == 0 ) {
178169 referencedFlowCount --;
179170 flow .checkDone ();
0 commit comments