11package com .ss .rlib .network .packet .impl ;
22
33import static com .ss .rlib .common .util .ObjectUtils .notNull ;
4- import static com .ss .rlib .network .util .NetworkUtils .getSocketAddress ;
5- import static java .util .Objects .requireNonNull ;
4+ import static com .ss .rlib .network .util .NetworkUtils .getRemoteAddress ;
65import com .ss .rlib .common .function .NotNullConsumer ;
76import com .ss .rlib .common .util .BufferUtils ;
8- import com .ss .rlib .common .util .ObjectUtils ;
97import com .ss .rlib .logger .api .Logger ;
108import com .ss .rlib .logger .api .LoggerManager ;
119import com .ss .rlib .network .BufferAllocator ;
1210import com .ss .rlib .network .Connection ;
1311import com .ss .rlib .network .packet .PacketReader ;
1412import com .ss .rlib .network .packet .ReadablePacket ;
13+ import com .ss .rlib .network .util .NetworkUtils ;
1514import lombok .AccessLevel ;
1615import lombok .Getter ;
1716import lombok .Setter ;
2221import java .nio .channels .AsynchronousCloseException ;
2322import java .nio .channels .AsynchronousSocketChannel ;
2423import java .nio .channels .CompletionHandler ;
25- import java .util .Objects ;
2624import java .util .concurrent .atomic .AtomicBoolean ;
2725import java .util .function .Consumer ;
2826
@@ -91,9 +89,14 @@ protected AbstractPacketReader(
9189
9290 @ Override
9391 public void startRead () {
94- if (isReading .compareAndSet (false , true )) {
95- channel .read (getBufferToReadFromChannel (), null , readHandler );
92+
93+ if (!isReading .compareAndSet (false , true )) {
94+ return ;
9695 }
96+
97+ LOGGER .debug (channel , ch -> "Start waiting for new data from channel \" " + getRemoteAddress (ch ) + "\" " );
98+
99+ channel .read (getBufferToReadFromChannel (), null , readHandler );
97100 }
98101
99102 /**
@@ -129,8 +132,11 @@ protected int readPackets(@NotNull ByteBuffer receivedBuffer, @NotNull ByteBuffe
129132 tempPendingBuffer = notNull (getTempPendingBuffer ());
130133 }
131134
132- LOGGER .debugNullable (receivedBuffer , tempPendingBuffer ,
133- (buf , mappedBuf ) -> "Put received buffer: " + buf + " to read mapped buffer: " + mappedBuf );
135+ LOGGER .debugNullable (
136+ receivedBuffer ,
137+ tempPendingBuffer ,
138+ (buf , mappedBuf ) -> "Put received buffer: " + buf + " to read mapped buffer: " + mappedBuf
139+ );
134140
135141 bufferToRead = BufferUtils .putToAndFlip (tempPendingBuffer , receivedBuffer );
136142 }
@@ -140,9 +146,12 @@ else if (waitedBytes > 0) {
140146
141147 if (pendingBuffer .remaining () < receivedBuffer .remaining ()) {
142148
143- LOGGER .debug (pendingBuffer , receivedBuffer ,
149+ LOGGER .debug (
150+ pendingBuffer ,
151+ receivedBuffer ,
144152 (penBuf , buf ) -> "Pending buffer: " + penBuf + " is too small to append received buffer: " +
145- buf + ", allocate mapped buffer for this" );
153+ buf + ", allocate mapped buffer for this"
154+ );
146155
147156 allocTempBuffers (pendingBuffer .flip (), pendingBuffer .capacity ());
148157
@@ -152,15 +161,21 @@ else if (waitedBytes > 0) {
152161
153162 tempPendingBuffer = notNull (getTempPendingBuffer ());
154163
155- LOGGER .debugNullable (receivedBuffer , tempPendingBuffer ,
156- (buf , mappedBuf ) -> "Put received buffer: " + buf + " to mapped buffer: " + mappedBuf );
164+ LOGGER .debugNullable (
165+ receivedBuffer ,
166+ tempPendingBuffer ,
167+ (buf , mappedBuf ) -> "Put received buffer: " + buf + " to mapped buffer: " + mappedBuf
168+ );
157169
158170 bufferToRead = BufferUtils .putToAndFlip (tempPendingBuffer , receivedBuffer );
159171
160172 } else {
161173
162- LOGGER .debug (receivedBuffer , pendingBuffer ,
163- (buf , penBuf ) -> "Put received buffer: " + buf + " to pending buffer: " + penBuf );
174+ LOGGER .debug (
175+ receivedBuffer ,
176+ pendingBuffer ,
177+ (buf , penBuf ) -> "Put received buffer: " + buf + " to pending buffer: " + penBuf
178+ );
164179
165180 bufferToRead = BufferUtils .putToAndFlip (pendingBuffer , receivedBuffer );
166181 }
@@ -180,8 +195,11 @@ else if (waitedBytes > 0) {
180195 var packetLength = readPacketLength (bufferToRead );
181196 var dataLength = getDataLength (packetLength , bufferToRead .position () - endPosition , bufferToRead );
182197
183- LOGGER .debug (packetLength , positionBeforeRead ,
184- (length , pos ) -> "Find next packet from position: " + pos + " with length: " + length );
198+ LOGGER .debug (
199+ packetLength ,
200+ positionBeforeRead ,
201+ (length , pos ) -> "Find next packet from position: " + pos + " with length: " + length
202+ );
185203
186204 // calculate position of end the next packet
187205 endPosition += packetLength ;
@@ -196,8 +214,10 @@ else if (waitedBytes > 0) {
196214 if (bufferToRead == receivedBuffer ) {
197215 if (packetLength <= pendingBuffer .capacity ()) {
198216 pendingBuffer .put (receivedBuffer );
199- LOGGER .debug (pendingBuffer ,
200- buf -> "Put pending data form received buffer to pending buffer: " + buf );
217+ LOGGER .debug (
218+ pendingBuffer ,
219+ buf -> "Put pending data form received buffer to pending buffer: " + buf
220+ );
201221 } else {
202222 allocTempBuffers (receivedBuffer , packetLength );
203223 }
@@ -219,11 +239,14 @@ else if (bufferToRead == pendingBuffer) {
219239 // if not read data is less than pending buffer then we can switch to use the pending buffer
220240 if (Math .max (packetLength , tempPendingBuffer .remaining ()) <= pendingBuffer .capacity ()) {
221241
222- pendingBuffer .clear ()
242+ pendingBuffer
243+ .clear ()
223244 .put (tempPendingBuffer );
224245
225- LOGGER .debug (pendingBuffer ,
226- buf -> "Moved pending data from mapped buffer to pending buffer: " + buf );
246+ LOGGER .debug (
247+ pendingBuffer ,
248+ buf -> "Moved pending data from mapped buffer to pending buffer: " + buf
249+ );
227250
228251 freeTempBuffers ();
229252 }
@@ -238,9 +261,12 @@ else if (packetLength > tempPendingBuffer.capacity()) {
238261 }
239262 }
240263
241- LOGGER .debug (channel , readPackets ,
242- (ch , count ) -> "Read " + count + " packet(s) from buffered data of " + getSocketAddress (ch ) + ", " +
243- "but 1 packet is still waiting for receiving additional data." );
264+ LOGGER .debug (
265+ channel ,
266+ readPackets ,
267+ (ch , count ) -> "Read " + count + " packet(s) from buffered data of " + getRemoteAddress (ch ) + ", " +
268+ "but 1 packet is still waiting for receiving additional data."
269+ );
244270
245271 return readPackets ;
246272 }
@@ -276,7 +302,7 @@ else if (packetLength > tempPendingBuffer.capacity()) {
276302 }
277303
278304 LOGGER .debug (channel , readPackets ,
279- (ch , count ) -> "Read " + count + " packet(s) from buffered data of " + getSocketAddress (ch ) + "." );
305+ (ch , count ) -> "Read " + count + " packet(s) from buffered data of " + getRemoteAddress (ch ) + "." );
280306
281307 return readPackets ;
282308 }
@@ -314,13 +340,19 @@ protected void reAllocTempBuffers(
314340 int packetLength
315341 ) {
316342
317- LOGGER .debug (sourceBuffer .capacity (), packetLength , (currentSize , newSize ) ->
318- "Resize read temp buffer from: " + currentSize + " to: " + newSize );
343+ LOGGER .debug (
344+ sourceBuffer .capacity (),
345+ packetLength ,
346+ (currentSize , newSize ) -> "Resize read temp buffer from " + currentSize + " to " + newSize
347+ );
319348
320349 var newReadTempBuffer = bufferAllocator .takeBuffer (packetLength + readBuffer .capacity ());
321350
322- LOGGER .debug (sourceBuffer , newReadTempBuffer ,
323- (old , buf ) -> "Moved pending data from old temp buffer: " + old + " to new temp buffer: " + buf );
351+ LOGGER .debug (
352+ sourceBuffer ,
353+ newReadTempBuffer ,
354+ (old , buf ) -> "Moved pending data from old temp buffer " + old + " to new temp buffer " + buf
355+ );
324356
325357 newReadTempBuffer .put (sourceBuffer );
326358
@@ -331,13 +363,19 @@ protected void reAllocTempBuffers(
331363
332364 protected void allocTempBuffers (@ NotNull ByteBuffer sourceBuffer , int packetLength ) {
333365
334- LOGGER .debug (packetLength , sourceBuffer .remaining (), (length , part ) ->
335- "Request temp buffer to store a part: " + part + " of big packet with length: " + length );
366+ LOGGER .debug (
367+ packetLength ,
368+ sourceBuffer .remaining (),
369+ (length , part ) -> "Request temp buffer to store a part: " + part + " of big packet with length: " + length
370+ );
336371
337372 var readTempBuffer = bufferAllocator .takeBuffer (packetLength + readBuffer .capacity ());
338373
339- LOGGER .debug (sourceBuffer , readTempBuffer ,
340- (recBuf , buf ) -> "Put the part of packet: " + recBuf + " to mapped buffer: " + buf );
374+ LOGGER .debug (
375+ sourceBuffer ,
376+ readTempBuffer ,
377+ (recBuf , buf ) -> "Put the part of packet: " + recBuf + " to mapped buffer: " + buf
378+ );
341379
342380 readTempBuffer .put (sourceBuffer );
343381
@@ -367,6 +405,12 @@ protected void handleReadData(@NotNull Integer result) {
367405 return ;
368406 }
369407
408+ LOGGER .debug (
409+ result ,
410+ channel ,
411+ (bytes , ch ) -> "Received " + bytes + " bytes from channel \" " + NetworkUtils .getRemoteAddress (ch ) + "\" "
412+ );
413+
370414 var readBuffer = getBufferToReadFromChannel ();
371415 readBuffer .flip ();
372416 try {
0 commit comments