-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Unknow
authored and
Unknow
committed
Oct 6, 2024
1 parent
12f6560
commit 2afad09
Showing
18 changed files
with
554 additions
and
436 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
unknow-server-nio/src/main/java/unknow/server/nio/NIOConnectionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package unknow.server.nio; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.net.ssl.SSLEngine; | ||
|
||
import unknow.server.util.io.Buffers; | ||
|
||
public interface NIOConnectionHandler { | ||
|
||
/** | ||
* called after the connection is initialized | ||
* | ||
* @param sslEngine the sslEngine for ssl connection null for other | ||
* @throws InterruptedException on interrupt | ||
*/ | ||
void onInit(NIOConnectionAbstract co, SSLEngine sslEngine) throws InterruptedException; | ||
|
||
/** | ||
* called when the handshake process finish | ||
* @throws InterruptedException on interrupt | ||
*/ | ||
void onHandshakeDone(SSLEngine sslEngine) throws InterruptedException; | ||
|
||
/** | ||
* called after some data has been read | ||
* | ||
* @throws InterruptedException on interrupt | ||
* @throws IOException on io exception | ||
*/ | ||
void onRead(Buffers b) throws InterruptedException, IOException; | ||
|
||
/** | ||
* called after data has been written | ||
* | ||
* @throws InterruptedException on interrupt | ||
* @throws IOException on io exception | ||
*/ | ||
void onWrite() throws InterruptedException, IOException; | ||
|
||
/** | ||
* check if the connection is closed and should be stoped | ||
* | ||
* @param now System.currentMillis() | ||
* @param stop if true the server is in stop phase | ||
* @return true is the collection is closed | ||
*/ | ||
boolean closed(long now, boolean stop); | ||
|
||
/** | ||
* called when the connection is free | ||
* | ||
* @throws IOException on io exception | ||
*/ | ||
void onFree() throws IOException; | ||
} |
105 changes: 105 additions & 0 deletions
105
unknow-server-nio/src/main/java/unknow/server/nio/NIOConnectionPlain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/** | ||
* | ||
*/ | ||
package unknow.server.nio; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.SelectionKey; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* used to handle raw data | ||
* | ||
* @author unknow | ||
*/ | ||
public class NIOConnectionPlain extends NIOConnectionAbstract { | ||
private static final Logger logger = LoggerFactory.getLogger(NIOConnectionPlain.class); | ||
|
||
/** | ||
* create new connection | ||
* | ||
* @param key the selectionKey | ||
*/ | ||
public NIOConnectionPlain(SelectionKey key, NIOConnectionHandler handler) { | ||
super(key, handler); | ||
} | ||
|
||
/** | ||
* called after the connection is initialized | ||
* | ||
* @throws InterruptedException on interrupt | ||
*/ | ||
@Override | ||
protected final void onInit() throws InterruptedException { // for override | ||
handler.onInit(this, null); | ||
} | ||
|
||
/** | ||
* read data from the channel and try to handles it | ||
* | ||
* @param buf output buffer | ||
* | ||
* @throws InterruptedException on interrupt | ||
* @throws IOException on io exception | ||
*/ | ||
@Override | ||
protected final void readFrom(ByteBuffer buf) throws InterruptedException, IOException { | ||
int l; | ||
lastRead = System.currentTimeMillis(); | ||
while (true) { | ||
l = channel.read(buf); | ||
if (l == -1) { | ||
in.close(); | ||
return; | ||
} | ||
if (l == 0) | ||
return; | ||
buf.flip(); | ||
|
||
if (logger.isTraceEnabled()) { | ||
buf.mark(); | ||
byte[] bytes = new byte[buf.remaining()]; | ||
buf.get(bytes); | ||
logger.trace("read {}", new String(bytes)); | ||
buf.reset(); | ||
} | ||
pendingRead.write(buf); | ||
handler.onRead(pendingRead); | ||
} | ||
} | ||
|
||
/** | ||
* write pending data to the channel | ||
* | ||
* @param buf local cache | ||
* | ||
* @throws InterruptedException on interrupt | ||
* @throws IOException on io exception | ||
*/ | ||
@Override | ||
protected final void writeInto(ByteBuffer buf) throws InterruptedException, IOException { | ||
lastWrite = System.currentTimeMillis(); | ||
while (pendingWrite.read(buf, false)) { | ||
buf.flip(); | ||
|
||
if (logger.isTraceEnabled()) { | ||
buf.mark(); | ||
byte[] bytes = new byte[buf.remaining()]; | ||
buf.get(bytes); | ||
logger.trace("writ {}", new String(bytes)); | ||
buf.reset(); | ||
} | ||
|
||
channel.write(buf); | ||
if (buf.hasRemaining()) { | ||
pendingWrite.prepend(buf); | ||
break; | ||
} | ||
} | ||
toggleKeyOps(); | ||
handler.onWrite(); | ||
} | ||
} |
Oops, something went wrong.