Skip to content

Commit

Permalink
提交一把,lts nio层实现中
Browse files Browse the repository at this point in the history
  • Loading branch information
qq254963746 committed Feb 8, 2016
1 parent e04f324 commit a83e6b1
Show file tree
Hide file tree
Showing 44 changed files with 1,280 additions and 350 deletions.
25 changes: 25 additions & 0 deletions lts-core/src/main/java/com/lts/nio/NioClient.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
package com.lts.nio;

import com.lts.nio.codec.Decoder;
import com.lts.nio.codec.Encoder;
import com.lts.nio.config.NioClientConfig;
import com.lts.nio.handler.Futures;
import com.lts.nio.handler.NioHandler;
import com.lts.nio.processor.NioClientProcessor;

import java.net.SocketAddress;

/**
* @author Robert HG ([email protected]) on 1/30/16.
*/
public class NioClient {

private NioClientProcessor processor;

public NioClient(NioClientConfig clientConfig, NioHandler eventHandler, Encoder encoder, Decoder decoder) {
this.processor = new NioClientProcessor(clientConfig, eventHandler, encoder, decoder);
}


public Futures.ConnectFuture connect(SocketAddress remoteAddress) {

processor.start();

return processor.connect(remoteAddress);
}

public void shutdownGracefully() {
// TODO
}
}
18 changes: 6 additions & 12 deletions lts-core/src/main/java/com/lts/nio/NioServer.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package com.lts.nio;

import com.lts.core.constant.Constants;
import com.lts.nio.codec.Decoder;
import com.lts.nio.codec.Encoder;
import com.lts.nio.config.NioServerConfig;
import com.lts.nio.handler.EmptyHandler;
import com.lts.nio.handler.NioHandler;
import com.lts.nio.loop.FixedNioSelectorLoopPool;
import com.lts.nio.loop.NioSelectorLoop;
import com.lts.nio.loop.NioSelectorLoopPool;
import com.lts.nio.processor.NioServerProcessor;

import java.io.IOException;
Expand All @@ -24,18 +20,13 @@
public class NioServer {

private NioServerConfig serverConfig;
private NioSelectorLoop acceptSelectorLoop;
private NioServerProcessor processor;
private NioSelectorLoopPool readWriteSelectorPool;
private NioHandler eventHandler;

public NioServer(NioServerConfig serverConfig, NioHandler eventHandler, Encoder encoder, Decoder decoder) {
this.serverConfig = serverConfig;
setEventHandler(eventHandler);
// this.connection = new NioServerConnection(this.eventHandler);
this.acceptSelectorLoop = new NioSelectorLoop("AcceptSelectorLoop-I/O", processor);
this.processor = new NioServerProcessor(this.acceptSelectorLoop, serverConfig, this.eventHandler, encoder, decoder);
this.readWriteSelectorPool = new FixedNioSelectorLoopPool(Constants.AVAILABLE_PROCESSOR + 1, "Server", processor);
this.processor = new NioServerProcessor(serverConfig, this.eventHandler, encoder, decoder);
}

private void setEventHandler(NioHandler eventHandler) {
Expand All @@ -50,6 +41,8 @@ public void bind(InetSocketAddress localAddress) {
// 初始化
init();

processor.start();

// 注册
try {
processor.register();
Expand All @@ -63,8 +56,6 @@ public void bind(InetSocketAddress localAddress) {
} catch (IOException e) {
throw new NioException("bind channel error:" + e.getMessage(), e);
}

acceptSelectorLoop.start();
}

private void init() {
Expand All @@ -85,4 +76,7 @@ private void init() {
}
}

public void shutdownGracefully() {
// TODO
}
}
70 changes: 0 additions & 70 deletions lts-core/src/main/java/com/lts/nio/channel/AbstractNioChannel.java

This file was deleted.

39 changes: 0 additions & 39 deletions lts-core/src/main/java/com/lts/nio/channel/ChannelContainer.java

This file was deleted.

10 changes: 5 additions & 5 deletions lts-core/src/main/java/com/lts/nio/channel/NioChannel.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.lts.nio.channel;

import com.lts.nio.handler.CloseFuture;
import com.lts.nio.handler.WriteFuture;
import com.lts.nio.handler.Futures;

import java.net.SocketAddress;
import java.nio.channels.SocketChannel;
Expand All @@ -15,9 +14,11 @@ public interface NioChannel {

SocketAddress remoteAddress();

WriteFuture writeAndFlush(Object msg);
SocketAddress localAddress();

CloseFuture close();
Futures.WriteFuture writeAndFlush(Object msg);

Futures.CloseFuture close();

boolean isConnected();

Expand All @@ -30,5 +31,4 @@ public interface NioChannel {
void setLastReadTime(long lastReadTime);

void setLastWriteTime(long lastWriteTime);

}
61 changes: 0 additions & 61 deletions lts-core/src/main/java/com/lts/nio/channel/NioServerChannel.java

This file was deleted.

Loading

0 comments on commit a83e6b1

Please sign in to comment.