锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
]]>
Wiki涓婇潰瀵逛簬method chaining鐨勫畾涔夊涓嬶細
Method chaining, is a common technique for invoking multiple method calls in object-oriented programming languages. Each method returns an object (possibly the current object itself), allowing the calls to be chained together in a single statement.
鍦ㄩ潰鍚戝璞$殑緙栫▼璇█涓紝method chaining鏄竴縐嶇敤浜庤皟鐢ㄥ涓柟娉曠殑甯哥敤鎶鏈傛瘡涓柟娉曢兘浼氳繑鍥炰竴涓璞★紙鍙兘鏄綋鍓嶅璞℃湰韜級錛岃繖鏍峰仛鐨勫ソ澶勫氨鏄紝閫氳繃涓鏉¤鍙ワ紝灝卞彲浠ヨ鎵鏈夌殑鏂規硶璋冪敤閾炬帴鍦ㄤ竴璧楓?/span>
瀹氫箟鏈変竴浜涙娊璞★紝閭d箞鍙互閫氳繃瀹為檯鐨勪緥瀛愭潵榪涜鐞嗚В鍜屾秷鍖栥?/span>
鍋囪鏈変竴涓?/span>StringBuffer鐨勭被錛岀被涓湁涓涓?/span>append()鏂規硶
榪欎釜綾繪湁緙虹偣灝辨槸璇達紝鍦ㄥ悜StringBuffer瀵硅薄榪藉姞鏂扮殑瀛楃涓茬殑鏃跺欙紝闇瑕佸湪澶氭潯璇彞涓紝涓嶆柇鐨勮皟鐢?/span>append()鏂規硶銆?/span>
StringBuffer sb = new StringBuffer();
sb.append("Hello ");
sb.append(name);
sb.append("! Welcome!");
鑰?/span>method chaining鐨勫疄鐜幫紝灝卞彲浠ラ伩鍏嶈繖涓己闄楓?/span>
public class StringBuffer {
public StringBuffer append(String str) {
//useful code
return this;
}
}
鐜板湪鎴戜滑灝卞彲浠ュ啀涓鏉¤鍙ヤ腑瀹炵幇絎竴涓緥瀛愮殑鍔熻兘浜嗐?/span>
StringBuffer sb = new StringBuffer();
sb.append("Hello ").append(name).append("! Welcome!");
AbstractBootstrap鎶借薄綾?/span> 錛堥儴鍒嗕唬鐮侊級
AbstractBootstrap澹版槑浜嗗叚涓鏈夌殑鎴愬憳鍙橀噺錛?/span>EventLoopGroup瀵硅薄錛?/span>ChannelFacotry瀵硅薄錛?/span>SockteAddress瀵硅薄錛?/span>Map<ChannelOption<?>,Object>瀵硅薄錛?/span>Map<Attribute<?>,Object>瀵硅薄錛?/span>ChannelHandler瀵硅薄銆傚茍涓旀湁鐩稿搴旂殑鏂規硶錛屾潵璁劇疆榪欎簺瀵硅薄錛屽
閫氳繃浼犺繘鏉ョ殑group瀵硅薄錛岀劧鍚庡皢AbstractBootstrap涓殑group鎸囧悜浼犺繘鏉ョ殑group銆傛渶緇堣繑鍥炵殑榪樻槸AbstractBootstrap榪欎釜褰撳墠瀵硅薄銆?/span>
鍏釜瀵硅薄鐨勪綔鐢?/span>
EventLoopGroup group | 鐢ㄤ簬澶勭悊灝嗚琚垱寤虹殑鎵鏈?/span>event |
ChannelFactory factory | 寤虹珛涓涓伐鍘傦紝鐢ㄤ簬浠ュ悗鐨?/span>Channel鍒涘緩 |
SocketAddress localAddress; | 鐢ㄤ簬緇戝畾鏈湴鐨勭綉緇滃湴鍧 |
Map<ChannelOption<?>, Object> options | 鎸囧畾鎵鍒涘緩Channel鐨勯夐」錛屽TCP_NODELAY錛?/span>AIO_READ_TIMEOUT |
Map<AttributeKey<?>, Object> attrs | 鎸囧畾鎵鍒涘緩Channel鐨勫睘鎬?/span> |
ChannelHandler handler | 鐢ㄤ簬澶勭悊鍚勭被璇鋒眰 |
AbstractBootstrap鎶借薄綾葷殑鎺ュ彛
AbstractBootstrap鎶借薄綾?/span>UML鍥?/span>
ServerBootstrap鍜?/span>Bootstrap
鍦?/span>Netty 4.0涓紝ServerBootstrap鐢ㄤ簬涓烘湇鍔″櫒鍚姩ServerChannel錛?/span>Bootstrap鐢ㄤ簬涓哄鏈嶇鍚姩Channel銆傝繖涓や釜綾繪槸AbstractBootstrap鐨勫叿浣撳疄鐜般?br />
EventLoop鎺ュ彛
EventLoop鎺ュ彛UML鍥?/span>
parent()鏂規硶
榪斿洖綆$悊榪欎釜EventLoop鐨?/span>EventLoopGroup銆?/span>
EventLoopGroup鎺ュ彛
EventLoopGroup鎺ュ彛UML鍥?/span>
next()
榪斿洖涓嬩竴涓?/span>EventLoop
register(Channel channel)
鎸囧畾涓涓?/span>EventLoopGroup鏉ユ敞鍐屾煇涓?/span>Channel銆?br /> ChannelBuf鎺ュ彛 ByteBuf鎺ュ彛 ByteBuf Index ByteBuf閫氳繃涓や釜鎸囬拡鏉ュ崗鍔?/span>I/O鐨勮鍐欐搷浣滐紝璇繪搷浣滅殑readIndex鍜屽啓鎿嶄綔鐨?/span>writeIndex readerIndex鍜?/span>writerIndex閮芥槸涓寮濮嬮兘鏄?/span>0錛岄殢鐫鏁版嵁鐨勫啓鍏?/span>writerIndex浼氬鍔狅紝璇誨彇鏁版嵁浼氫嬌readerIndex澧炲姞錛屼絾鏄粬涓嶄細瓚呰繃writerIndx錛屽湪璇誨彇涔嬪悗錛?/span>0-readerIndex鐨勫氨琚涓?/span>discard鐨?/span>.璋冪敤discardReadBytes鏂規硶,鍙互閲婃斁榪欓儴鍒嗙┖闂?/span>,浠栫殑浣滅敤綾諱技ByeBuffer鐨?/span>compact鏂規硶; 璇誨拰鍐欑殑鏃跺?/span>Index鏄垎寮鐨勶紝鍥犳涔熷氨娌″繀瑕佸啀姣忔璇誨畬浠ュ悗璋冪敤flip鏂規硶錛屽彟澶栬繕鏈?/span>indexOf銆?/span>bytesBefore絳変竴浜涙柟渚跨殑鏂規硶錛?/span> ByteBuf鐨勫嚑涓噸瑕佹柟娉?/span> discardReadBytes() 璋冪敤 discardReadBytes()鏂規硶鍚?/span> clear() 璋冪敤clear()涔嬪悗 ChannelHandlerContext鎺ュ彛UML綾?/span> ChannelHandlerContext鎺ュ彛鐨勫嚑涓噸瑕佹柟娉?/span> ChannelPipeline pipeline(); 榪斿洖灞炰簬褰撳墠ChannelHandlerContext鐨?/span>ChannelPipeline銆?/span> EventExecutor executor(); EnventExcutor鐢ㄤ簬璋冨害EventLoop涓殑event錛岃繖涓柟娉曡繑鍥炲綋鍓嶇殑EventExecutor銆?/span> 涓涓?/span>Handler鍙互鏈夊涓?/span>Context 涓涓?/span>Handler鍙互琚坊鍔犲埌澶氫釜ChannelPipeline銆傝繖灝辨剰鍛崇潃涓涓?/span>ChannelHandler鍙互鏈夊涓?/span>ChannelHandlerContext銆?/span> ChannelHandler鎺ュ彛 ChannelHandler綾?/span>UML鍥?/span> ChannelHandler涓殑鍑犱釜閲嶈鏂規硶 ChannelHandler鏈変袱涓瓙鎺ュ彛錛?/span>ChannelUpstreamHandler鍜?/span>ChannelDownstreamHandler銆?/span> 1. ChannelUpstreamHandler鐢ㄤ簬澶勭悊鍜屾嫤鎴?/span>upstream涓殑ChannelEvent 2. ChannelDownstreamHandler鐢ㄤ簬澶勭悊鍜屾嫤鎴?/span>downstream涓殑ChannelEvent 鐘舵佷俊鎭?/span> ChannelHandler涓鑸渶瑕佸瓨鍌ㄤ竴浜涚姸鎬佷俊鎭紝浠ヤ笅鏄愬畼鏂規帹鑽愩戠殑鏂規硶錛屼嬌鐢ㄦ垚鍛樺彉閲忋?/span> 鎴栬呬嬌鐢?/span>ChannelLocal錛屼唬鐮佸涓?/span> Line 1: 澹版槑涓涓?/span>EchoServerHandler, 騫朵笖緇ф壙浜?/span>ChannelInboundByteHandlerAdapter銆?/span> 榪欐牱EchoServerHandler灝卞彲浠ュ鐞?/span>client鍙戦佽繃鏉ョ殑request銆?/span> Line 6: 閲嶅啓inboundBufferUpdated鏂規硶錛屽client鍙戦佽繃鏉ョ殑request榪涜瀵?/span>ByteBuffer瀵硅薄鐨勬搷浣溿傚叧浜?/span>ByteBuffer鐨勬蹇靛皢鍦ㄤ互鍚庣珷鑺傝璁恒?/span> Line 7: ctx.nextOutboundByteBuffer()灝嗚繑鍥炰竴涓?/span>ByteBuffer瀵硅薄銆傚鏋滆瀵硅薄涓嶅瓨鍦紝閭d箞鎶涘嚭UnsupportedOperationException寮傚父銆?/span> Line 14: 閲嶅啓exceptionCaught鍦?/span>server绔崟鑾峰紓甯搞?/span> Line 11: 閫氳繃ServerBootStrap瀵硅薄錛屾潵鍚姩鏈嶅姟鍣?/span> Line 13: 閫氳繃group鏂規硶錛屾潵緇戝畾EventLoopGroup錛?/span>EventLoopGroup鐢ㄦ潵澶勭悊SocketChannel鍜?/span>Channel涓婇潰鐨勬墍鏈夋椂闂村拰IO銆?/span> Line 16: localAddress鏂規硶鐢ㄤ簬緇戝畾鏈嶅姟鍣ㄥ湴鍧鍜岀鍙c?/span> Line 18,19: handler鏂規硶鍜?/span>childhandler鏂規硶鐢ㄤ簬鎸囧畾鍚勭ChannelHandler瀵硅薄錛屾寚瀹氱殑ChannelHandler瀵硅薄灝嗙敤浜庡鐞?/span>client绔潵鐨?/span>request銆?/span> Line 21: 鍒濆鍖栦竴涓?/span>Channel瀵硅薄錛屽茍涓旂粦瀹氫箣鍓嶅畾涔夌殑EchoServerHandler綾匯?/span> Line 22: 灝?/span>EchoServerHandler娣誨姞鍒?/span>Pipeline涓?/span> Line 29: ServerBootstrap瀵硅薄鍑嗗灝辯華錛屽惎鍔?/span>server錛?/span> Line 32: 絳夊緟鐩村埌server socket鍏抽棴 EchoClientHandler綾葷殑瀹炵幇涓?/span>EchoServerHandler綾諱技錛屼篃閮界戶鎵夸簡ChannelInboundByteHandlerAdapter銆備笉鍚屽湪浜庨噸鍐欎簡channelActive()鏂規硶銆?/span> Line 20: 鎸囧畾榪滅▼鏈嶅姟鍣ㄧ殑鍦板潃鍜岀鍙o紙localhost:8080錛?br />
]]>
public interface ChannelBuf {
ChannelBufType type();
boolean isPooled();
}
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
public interface ByteBuf extends ChannelBuf, Comparable<ByteBuf> {
int capacity();
ByteBuf capacity(int newCapacity);
int maxCapacity();
ByteOrder order();
ByteBuf order(ByteOrder endianness);
boolean isDirect();
int readerIndex();
ByteBuf readerIndex(int readerIndex);
int writerIndex();
ByteBuf writerIndex(int writerIndex);
ByteBuf setIndex(int readerIndex, int writerIndex);
int readableBytes();
int writableBytes();
boolean readable();
boolean writable();
ByteBuf clear();
ByteBuf markReaderIndex();
ByteBuf resetReaderIndex();
ByteBuf markWriterIndex();
ByteBuf resetWriterIndex();
ByteBuf discardReadBytes();
ByteBuf ensureWritableBytes(int minWritableBytes);
int ensureWritableBytes(int minWritableBytes, boolean force);
boolean getBoolean(int index);
byte getByte(int index);
short getUnsignedByte(int index);
short getShort(int index);
int getUnsignedShort(int index);
int getMedium(int index);
int getUnsignedMedium(int index);
int getInt(int index);
long getUnsignedInt(int index);
long getLong(int index);
char getChar(int index);
float getFloat(int index);
double getDouble(int index);
ByteBuf getBytes(int index, ByteBuf dst);
ByteBuf getBytes(int index, ByteBuf dst, int length);
ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length);
ByteBuf getBytes(int index, byte[] dst);
ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length);
ByteBuf getBytes(int index, ByteBuffer dst);
ByteBuf getBytes(int index, OutputStream out, int length) throws IOException;
int getBytes(int index, GatheringByteChannel out, int length) throws IOException;
ByteBuf setBoolean(int index, boolean value);
ByteBuf setByte(int index, int value);
ByteBuf setShort(int index, int value);
ByteBuf setMedium(int index, int value);
ByteBuf setInt(int index, int value);
ByteBuf setLong(int index, long value);
ByteBuf setChar(int index, int value);
ByteBuf setFloat(int index, float value);
ByteBuf setDouble(int index, double value);
ByteBuf setBytes(int index, ByteBuf src);
ByteBuf setBytes(int index, ByteBuf src, int length);
ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length);
ByteBuf setBytes(int index, byte[] src);
ByteBuf setBytes(int index, byte[] src, int srcIndex, int length);
ByteBuf setBytes(int index, ByteBuffer src);
int setBytes(int index, InputStream in, int length) throws IOException;
int setBytes(int index, ScatteringByteChannel in, int length) throws IOException;
ByteBuf setZero(int index, int length);
boolean readBoolean();
byte readByte();
short readUnsignedByte();
short readShort();
int readUnsignedShort();
int readMedium();
int readUnsignedMedium();
int readInt();
long readUnsignedInt();
long readLong();
char readChar();
float readFloat();
double readDouble();
ByteBuf readBytes(int length);
ByteBuf readSlice(int length);
ByteBuf readBytes(ByteBuf dst);
ByteBuf readBytes(ByteBuf dst, int length);
ByteBuf readBytes(ByteBuf dst, int dstIndex, int length);
ByteBuf readBytes(byte[] dst);
ByteBuf readBytes(byte[] dst, int dstIndex, int length);
ByteBuf readBytes(ByteBuffer dst);
ByteBuf readBytes(OutputStream out, int length) throws IOException;
int readBytes(GatheringByteChannel out, int length) throws IOException;
ByteBuf skipBytes(int length);
ByteBuf writeBoolean(boolean value);
ByteBuf writeByte(int value);
ByteBuf writeShort(int value);
ByteBuf writeMedium(int value);
ByteBuf writeInt(int value);
ByteBuf writeLong(long value);
ByteBuf writeChar(int value);
ByteBuf writeFloat(float value);
ByteBuf writeDouble(double value);
ByteBuf writeBytes(ByteBuf src);
ByteBuf writeBytes(ByteBuf src, int length);
ByteBuf writeBytes(ByteBuf src, int srcIndex, int length);
ByteBuf writeBytes(byte[] src);
ByteBuf writeBytes(byte[] src, int srcIndex, int length);
ByteBuf writeBytes(ByteBuffer src);
int writeBytes(InputStream in, int length) throws IOException;
int writeBytes(ScatteringByteChannel in, int length) throws IOException;
ByteBuf writeZero(int length);
int indexOf(int fromIndex, int toIndex, byte value);
int indexOf(int fromIndex, int toIndex, ByteBufIndexFinder indexFinder);
int bytesBefore(byte value);
int bytesBefore(ByteBufIndexFinder indexFinder);
int bytesBefore(int length, byte value);
int bytesBefore(int length, ByteBufIndexFinder indexFinder);
int bytesBefore(int index, int length, byte value);
int bytesBefore(int index, int length, ByteBufIndexFinder indexFinder);
ByteBuf copy();
ByteBuf copy(int index, int length);
ByteBuf slice();
ByteBuf slice(int index, int length);
ByteBuf duplicate();
boolean hasNioBuffer();
ByteBuffer nioBuffer();
ByteBuffer nioBuffer(int index, int length);
boolean hasNioBuffers();
ByteBuffer[] nioBuffers();
ByteBuffer[] nioBuffers(int offset, int length);
boolean hasArray();
byte[] array();
int arrayOffset();
String toString(Charset charset);
String toString(int index, int length, Charset charset);
@Override
int hashCode();
@Override
boolean equals(Object obj);
@Override
int compareTo(ByteBuf buffer);
@Override
String toString();
Unsafe unsafe();
interface Unsafe {
ByteBuffer nioBuffer();
ByteBuffer[] nioBuffers();
ByteBuf newBuffer(int initialCapacity);
void discardSomeReadBytes();
void acquire();
void release();
}
}
涓㈠純宸茶鐨勫唴瀹廣傚叾鎵ц榪囩▼濡備笅錛?/span>
璋冪敤discardReadBytes()涔嬪墠錛?/span>
涓㈠純鎵鏈夌殑鏁版嵁錛屽茍灝?span style="font-size: 6.5pt; font-family: 'Courier New'; background-position: initial initial; background-repeat: initial initial;">readerIndex鍜?/span>writerIndex閲嶇疆涓?/span>0銆?/span>
璋冪敤clear()涔嬪墠
澶囨敞:鍥犱負絎旇呭紑濮嬪啓Netty婧愮爜鍒嗘瀽鐨勬椂鍊欙紝Netty 4.0榪樻槸澶勪簬Alpha闃舵錛屼箣鍚庣殑API鍙兘榪樹細鏈夋敼鍔紝絎旇呭皢浼氬強鏃舵洿鏀廣備嬌鐢ㄥ紑婧愬凡緇忔湁濂藉嚑騫寸殑鏃墮棿浜嗭紝涓鐩存病鏈夋椂闂村拰綺懼姏鏉ュ叿浣撶爺絀舵煇涓紑婧愰」鐩殑鍏蜂綋瀹炵幇錛岃繖嬈℃槸絎竴嬈″啓寮婧愰」鐩殑婧愮爜鍒嗘瀽錛屽鏋滄枃涓湁閿欒鐨勫湴鏂癸紝嬈㈣繋璇昏呭彲浠ョ暀璦鎸囧嚭銆傚浜庤漿杞界殑璇昏咃紝璇鋒敞鏄庢枃绔犵殑鍑哄銆?/p>
]]>
2 import io.netty.buffer.ByteBuf;
3 import io.netty.buffer.MessageBuf;
4 import io.netty.util.AttributeMap;
5 import java.nio.channels.Channels;
6 import java.util.Set;
7 public interface ChannelHandlerContext
8 extends AttributeMap, ChannelFutureFactory,
9 ChannelInboundInvoker, ChannelOutboundInvoker {
10 Channel channel();
11 ChannelPipeline pipeline();
12 EventExecutor executor();
13 String name();
14 ChannelHandler handler();
15 Set<ChannelHandlerType> types();
16 boolean hasInboundByteBuffer();
17 boolean hasInboundMessageBuffer();
18 ByteBuf inboundByteBuffer();
19 <T> MessageBuf<T> inboundMessageBuffer();
20 boolean hasOutboundByteBuffer();
21 boolean hasOutboundMessageBuffer();
22 ByteBuf outboundByteBuffer();
23 <T> MessageBuf<T> outboundMessageBuffer();
24 ByteBuf replaceInboundByteBuffer(ByteBuf newInboundByteBuf);
25 <T> MessageBuf<T> replaceInboundMessageBuffer(MessageBuf<T> newInboundMsgBuf);
26 ByteBuf replaceOutboundByteBuffer(ByteBuf newOutboundByteBuf);
27 <T> MessageBuf<T> replaceOutboundMessageBuffer(MessageBuf<T> newOutboundMsgBuf);
28 boolean hasNextInboundByteBuffer();
29 boolean hasNextInboundMessageBuffer();
30 ByteBuf nextInboundByteBuffer();
31 MessageBuf<Object> nextInboundMessageBuffer();
32 boolean hasNextOutboundByteBuffer();
33 boolean hasNextOutboundMessageBuffer();
34 ByteBuf nextOutboundByteBuffer();
35 MessageBuf<Object> nextOutboundMessageBuffer();
36 boolean isReadable();
37 void readable(boolean readable);
38 }
2
3 import io.netty.channel.group.ChannelGroup;
4 import java.lang.annotation.Documented;
5 import java.lang.annotation.ElementType;
6 import java.lang.annotation.Inherited;
7 import java.lang.annotation.Retention;
8 import java.lang.annotation.RetentionPolicy;
9 import java.lang.annotation.Target;
10 import java.nio.channels.Channels;
11
12 public interface ChannelHandler {
13 void beforeAdd(ChannelHandlerContext ctx) throws Exception;
14 void afterAdd(ChannelHandlerContext ctx) throws Exception;
15 void beforeRemove(ChannelHandlerContext ctx) throws Exception;
16 void afterRemove(ChannelHandlerContext ctx) throws Exception;
17 void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception;
18 void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception;
19 @Inherited
20 @Documented
21 @Target(ElementType.TYPE)
22 @Retention(RetentionPolicy.RUNTIME)
23 @interface Sharable {
24 // no value
25 }
26 }
private boolean loggedIn;
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
Channel ch = e.getChannel();
Object o = e.getMessage();
if (o instanceof LoginMessage) {
authenticate((LoginMessage) o);
loggedIn = true;
} else (o instanceof GetDataMessage) {
if (loggedIn) {
ch.write(fetchSecret((GetDataMessage) o));
} else {
fail();
}
}
}
}
// Create a new handler instance per channel.
// See ClientBootstrap#setPipelineFactory(ChannelPipelineFactory).
public class DataServerPipelineFactory implements ChannelPipelineFactory {
public ChannelPipeline getPipeline() {
return Channels.pipeline(new DataServerHandler());
}
}
}
@Sharable
public class DataServerHandler extends SimpleChannelHandler {
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
Channel ch = e.getChannel();
Object o = e.getMessage();
if (o instanceof LoginMessage) {
authenticate((LoginMessage) o);
DataServerState.loggedIn.set(ch, true);
} else (o instanceof GetDataMessage) {
if (DataServerState.loggedIn.get(ch)) {
ctx.getChannel().write(fetchSecret((GetDataMessage) o));
} else {
fail();
}
}
}
}
// Print the remote addresses of the authenticated clients:
ChannelGroup allClientChannels = ;
for (Channel ch: allClientChannels) {
if (DataServerState.loggedIn.get(ch)) {
System.out.println(ch.getRemoteAddress());
}
}
澶囨敞:鍥犱負絎旇呭紑濮嬪啓Netty婧愮爜鍒嗘瀽鐨勬椂鍊欙紝Netty 4.0榪樻槸澶勪簬Alpha闃舵錛屼箣鍚庣殑API鍙兘榪樹細鏈夋敼鍔紝絎旇呭皢浼氬強鏃舵洿鏀廣備嬌鐢ㄥ紑婧愬凡緇忔湁濂藉嚑騫寸殑鏃墮棿浜嗭紝涓鐩存病鏈夋椂闂村拰綺懼姏鏉ュ叿浣撶爺絀舵煇涓紑婧愰」鐩殑鍏蜂綋瀹炵幇錛岃繖嬈℃槸絎竴嬈″啓寮婧愰」鐩殑婧愮爜鍒嗘瀽錛屽鏋滄枃涓湁閿欒鐨勫湴鏂癸紝嬈㈣繋璇昏呭彲浠ョ暀璦鎸囧嚭銆傚浜庤漿杞界殑璇昏咃紝璇鋒敞鏄庢枃绔犵殑鍑哄銆?甯屾湜鍜屽箍澶х殑寮鍙戣?寮婧愮埍濂借呰繘琛屼氦嫻侊紝嬈㈣繋澶у鐨勭暀璦鍜岃璁恒?/div>
]]>
]]>
2 private static final Logger logger = Logger.getLogger(
3 EchoServerHandler.class.getName());
4
5 @Override
6 public void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in) {
7 ByteBuf out = ctx.nextOutboundByteBuffer();
8 out.discardReadBytes();
9 out.writeBytes(in);
10 ctx.flush();
11 }
12
13 @Override
14 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
15 // Close the connection when an exception is raised.
16 logger.log(Level.WARNING, "Unexpected exception from downstream.", cause);
17 ctx.close();
18 }
19 }
2
3 private final int port;
4
5 public EchoServer(int port) {
6 this.port = port;
7 }
8
9 public void run() throws Exception {
10 // Configure the server.
11 ServerBootstrap b = new ServerBootstrap();
12 try {
13 b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
14 .channel(NioServerSocketChannel.class)
15 .option(ChannelOption.SO_BACKLOG, 100)
16 .localAddress(new InetSocketAddress(port))
17 .childOption(ChannelOption.TCP_NODELAY, true)
18 .handler(new LoggingHandler(LogLevel.INFO))
19 .childHandler(new ChannelInitializer<SocketChannel>() {
20 @Override
21 public void initChannel(SocketChannel ch) throws Exception {
22 ch.pipeline().addLast(
23 new LoggingHandler(LogLevel.INFO),
24 new EchoServerHandler());
25 }
26 });
27
28 // Start the server.
29 ChannelFuture f = b.bind().sync();
30
31 // Wait until the server socket is closed.
32 f.channel().closeFuture().sync();
33 } finally {
34 // Shut down all event loops to terminate all threads.
35 b.shutdown();
36 }
37 }
38
39 public static void main(String[] args) throws Exception {
40 int port;
41 if (args.length > 0) {
42 port = Integer.parseInt(args[0]);
43 } else {
44 port = 8080;
45 }
46 new EchoServer(port).run();
47 }
48 }
private static final Logger logger = Logger.getLogger(
EchoClientHandler.class.getName());
private final ByteBuf firstMessage;
/**
* Creates a client-side handler.
*/
public EchoClientHandler(int firstMessageSize) {
if (firstMessageSize <= 0) {
throw new IllegalArgumentException("firstMessageSize: " + firstMessageSize);
}
firstMessage = Unpooled.buffer(firstMessageSize);
for (int i = 0; i < firstMessage.capacity(); i ++) {
firstMessage.writeByte((byte) i);
}
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.write(firstMessage);
}
@Override
public void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in) {
ByteBuf out = ctx.nextOutboundByteBuffer();
out.discardReadBytes();
out.writeBytes(in);
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
// Close the connection when an exception is raised.
logger.log(Level.WARNING, "Unexpected exception from downstream.", cause);
ctx.close();
}
}
2
3 private final String host;
4 private final int port;
5 private final int firstMessageSize;
6
7 public EchoClient(String host, int port, int firstMessageSize) {
8 this.host = host;
9 this.port = port;
10 this.firstMessageSize = firstMessageSize;
11 }
12
13 public void run() throws Exception {
14 // Configure the client.
15 Bootstrap b = new Bootstrap();
16 try {
17 b.group(new NioEventLoopGroup())
18 .channel(NioSocketChannel.class)
19 .option(ChannelOption.TCP_NODELAY, true)
20 .remoteAddress(new InetSocketAddress(host, port))
21 .handler(new ChannelInitializer<SocketChannel>() {
22 @Override
23 public void initChannel(SocketChannel ch) throws Exception {
24 ch.pipeline().addLast(
25 new LoggingHandler(LogLevel.INFO),
26 new EchoClientHandler(firstMessageSize));
27 }
28 });
29
30 // Start the client.
31 ChannelFuture f = b.connect().sync();
32
33 // Wait until the connection is closed.
34 f.channel().closeFuture().sync();
35 } finally {
36 // Shut down the event loop to terminate all threads.
37 b.shutdown();
38 }
39 }
40
41 public static void main(String[] args) throws Exception {
42 // Print usage if no argument is specified.
43 if (args.length < 2 || args.length > 3) {
44 System.err.println(
45 "Usage: " + EchoClient.class.getSimpleName() +
46 " <host> <port> [<first message size>]");
47 return;
48 }
49
50 // Parse options.
51 final String host = args[0];
52 final int port = Integer.parseInt(args[1]);
53 final int firstMessageSize;
54 if (args.length == 3) {
55 firstMessageSize = Integer.parseInt(args[2]);
56 } else {
57 firstMessageSize = 256;
58 }
59
60 new EchoClient(host, port, firstMessageSize).run();
61 }
62 }
]]>
1. 涓嬭澆jdk + elipse + egit
2. 鐩稿叧jar鍖呬笅杞?br />3. cynwin + telnet/ssh (setup.exe keywork:inetutils / ssh )
3. 浠巈xample鍖呭紑濮嬮槄璇?br />
]]>
主站蜘蛛池模板:
久久精品国产亚洲AV|
婷婷亚洲天堂影院|
成人影片一区免费观看|
亚洲国产欧洲综合997久久|
亚洲国产精品嫩草影院在线观看|
免费国内精品久久久久影院|
成年人网站在线免费观看|
色欲A∨无码蜜臀AV免费播|
国产裸体美女永久免费无遮挡|
国产精品成人亚洲|
2020亚洲男人天堂精品|
亚洲的天堂av无码|
亚洲一区二区三区首页|
亚洲成av人在线视|
亚洲中文久久精品无码ww16|
亚洲av区一区二区三|
国产jizzjizz免费视频|
韩国18福利视频免费观看|
91在线品视觉盛宴免费|
久热中文字幕在线精品免费|
99re免费视频|
国产亚洲色视频在线|
免费大黄网站在线观|
免费看国产曰批40分钟|
免费一级特黄特色大片在线|
国产成人精品123区免费视频|
免费看www视频|
四虎影永久在线高清免费|
爽爽日本在线视频免费|
国产一级淫片视频免费看|
国产裸模视频免费区无码|
国产成人精品免费直播|
免费一级毛片在线播放不收费|
亚洲成AV人网址|
中文字幕无码播放免费|
久久不见久久见免费视频7|
久久久免费精品re6|
h视频在线观看免费完整版|
国产一卡2卡3卡4卡2021免费观看|
国产精品成人免费福利|
免费精品国产自产拍在|