【Netty】NIOネットワーク通信SelectionKey共通API概要
文書ディレクトリ
I . SelectionKeyの概要
1 . チャネル登録セレクタ:チャネル(Channel)はセレクタ(Selector)に登録され、このチャネルはセレクタ(Selector)管理の範疇に組み込まれ、セレクタ(Selector)はチャネルのイベントを傍受することができる.
登録されたチャネルの説明:このチャネル(Channel)は、サーバソケットチャネル(Server SocketChannel)であってもよいし、ソケットチャネル(SocketChannel)であってもよい.
2 . セレクタの真のタイプ:セレクタ(Selector)のSelectorクラスは抽象クラスであり、実例化された真のタイプはWindowsSelectorImplである.
3 . セレクタ(Selector)がチャネル(Channel)を管理する方法:チャネル(Channel)がセレクタ(Selector)に登録されると、SelectionKeyに戻り、セレクタ(Selector)に登録されているすべてのチャネルが表すSelectionKeyが格納されるセレクタ(Selector)のHashSet keysセットにこのSelector(Selector)が格納される.
4 . イベントが発生したチャネルに対応するSelectionKeyセットを取得する:セレクタ(Selector)がイベント発生を傍受した場合、イベント発生の個数しか傍受できず、具体的な状況が分からない.これは、セレクタ(Selector)のselectedKeys()メソッドを自分で呼び出す必要があります.この場合、同僚が複数のチャネルでイベントが発生する可能性があるため、複数のチャネルのイベントを一度に処理することができます.
II . SelectionKeyイベントの概要
SelectionKeyのイベントは、セレクタ(Selector)がチャネルを登録するときに、このチャネルを傍受するイベントを指定する必要があります.
SelectionKeyでは、データ読み出し(OP_READ)、データ書き出し(OP_WRITE)、接続(OP_CONNECT)、受信接続(OP_ACCEPT)の4つのイベントが定義されています.
1 . 接続を受け入れる(OP_ACCEPT)イベント:
①適用シーン:サーバー側サーバーソケットチャネル(ServerSocketChannel)はこのイベントをセレクタ(Selector)に登録し、セレクタ(Selector)はクライアントの接続要求を傍受することができる.
②コード例:次のコードの役割は、Server SocketChannelチャネルのSelectionKey.OP_ACCEPTイベントはセレクタ(Selector)に登録され、クライアントがサーバに接続されている場合、セレクタのリスニング方法がトリガーされます.
// serverSocketChannel ( Selector ),
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
③ OP_ACCEPT定数プロトタイプ:この定数はSelectionKeyで定義する.JAvaクラスでは、この値の大きさは1<<4,0 b 10000(バイナリ)、0 x 10(16進数)、16(10進数)である.
/**
* Operation-set bit for socket-accept operations.
*
* Suppose that a selection key's interest set contains
* OP_ACCEPT at the start of a selection operation. If the selector
* detects that the corresponding server-socket channel is ready to accept
* another connection, or has an error pending, then it will add
* OP_ACCEPT to the key's ready set and add the key to its
* selected-key set.
*/
public static final int OP_ACCEPT = 1 << 4;
2 . 読み出しデータ(OP_READ)イベント:
①適用シーン:サーバー側ソケットチャネル(SocketChannel)はこのイベントをセレクタ(Selector)に登録し、セレクタ(Selector)はクライアントのデータがサーバーに書き込まれるのを傍受することができ、つまりサーバー側はデータを読み取る作業を行う必要がある.
②コード例:次のコードの役割は、SocketChannelチャネルのSelectionKey.OP_READイベントはセレクタに登録され、クライアントがデータをアップロードすると、セレクタの傍受方法がトリガーされる.
// : SocketChannel ( Selector )
// : ,
// :
sc.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(1024));
③ OP_READ定数プロトタイプ:この定数はSelectionKeyに定義する.JAvaクラスでは、この値の大きさは1<<0,0 b 1(バイナリ)、0 x 1(16進数)、1(10進数)である.
/**
* Operation-set bit for read operations.
*
* Suppose that a selection key's interest set contains
* OP_READ at the start of a selection operation. If the selector
* detects that the corresponding channel is ready for reading, has reached
* end-of-stream, has been remotely shut down for further reading, or has
* an error pending, then it will add OP_READ to the key's
* ready-operation set and add the key to its selected-key set.
*/
public static final int OP_READ = 1 << 0;
3 . 書き出しデータ(OP_WRITE)イベント:
①適用シーン:チャンネル(Channel)をセレクタ(Selector)に登録し、その書き出しデータ(OP_WRITE)イベントを登録し、セレクタがイベントをトリガーした場合、そのチャンネル(Channel)にデータを書き出したことを示す.
② OP_WRITE定数プロトタイプ:この定数はSelectionKeyで定義する.JAvaクラスでは、この値の大きさは1<<2,0 b 100(バイナリ)、0 x 4(16進数)、4(10進数)である.
/**
* Operation-set bit for write operations.
*
* Suppose that a selection key's interest set contains
* OP_WRITE at the start of a selection operation. If the selector
* detects that the corresponding channel is ready for writing, has been
* remotely shut down for further writing, or has an error pending, then it
* will add OP_WRITE to the key's ready set and add the key to its
* selected-key set.
*/
public static final int OP_WRITE = 1 << 2;
4 . 接続(OP_CONNECT)イベント:
①適用シーン:チャネル(Channel)をセレクタ(Selector)に登録し、その接続(OP_CONNECT)イベントを登録し、セレクタがイベントをトリガした場合、その開始ネットワークSocketが接続されたことを示す.
② OP_WRITE定数プロトタイプ:この定数はSelectionKeyで定義する.JAvaクラスでは、この値の大きさは1<<3,0 b 1000(バイナリ)、0 x 8(16進数)、8(10進数)である.
/**
* Operation-set bit for socket-connect operations.
*
* Suppose that a selection key's interest set contains
* OP_CONNECT at the start of a selection operation. If the selector
* detects that the corresponding socket channel is ready to complete its
* connection sequence, or has an error pending, then it will add
* OP_CONNECT to the key's ready set and add the key to its
* selected-key set.
*/
public static final int OP_CONNECT = 1 << 3;
III . SelectionKey共通APIの概要
1 . NIOの3つのコンポーネントセレクタ(Selector)、チャネル(Channel)、バッファ(Buffer)メソッドを取得します.
①取得セレクタ(Selector):Selector selector()、このメソッドを呼び出して対応するセレクタ(Selector)を取得する.
②取得チャネル(Channel):SelectableChannel()、このメソッドを呼び出して対応するバインディングチャネル(Channel)を取得する.
③取得バッファ(Buffer):Object attach(Object ob)、このメソッドを呼び出して登録されたチャネルに対応するバッファ(Buffer)を取得する.
④コード例:前回のブログにおけるNIO通信事例におけるサーバー側のコードの一部であり、取得チャネルとバッファ操作に関するものである.
// ( Channel ) : SelectionKey
SocketChannel socketChannel = (SocketChannel) key.channel();
// ( Buffer ) : ( Channel ) ( Buffer )
ByteBuffer byteBuffer = (ByteBuffer) key.attachment();
2 . イベント関連メソッド:
①リスニングイベントの設定または変更:interestOps(int ops)、セレクタ関連イベントの設定または変更;
②判定イベントタイプ: