Skip to content

Commit

Permalink
看到有朋友还是在关注github上的,所以定期同步一下最新的代码
Browse files Browse the repository at this point in the history
  • Loading branch information
tywo45 committed Dec 2, 2018
1 parent 0c7c637 commit a2bdfed
Show file tree
Hide file tree
Showing 304 changed files with 16,570 additions and 10,176 deletions.
4 changes: 4 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/

Portions copyright 2017-2018, talent-tan
Portions copyright (c) 2006-2018 Paul Hammant & ThoughtWorks Inc
Portions copyright (c) 2000-2007 INRIA, France Telecom

TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

1. Definitions.
Expand Down
7 changes: 7 additions & 0 deletions bin/clean.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
cd ..\src\parent
call mvn clean
cd ..\..\bin
dir

cd ..\src\zoo\http\client
call mvn clean
cd ..\..\..\..\bin
dir

pause
Binary file not shown.
Binary file modified docs/git/git使用小记-github.docx
Binary file not shown.
Binary file added docs/git/~$t使用小记-github.docx
Binary file not shown.
15 changes: 15 additions & 0 deletions loc.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
call loc.exe .\src

call loc.exe .\src\utils

call loc.exe .\src\core

call loc.exe .\src\zoo\http

call loc.exe .\src\zoo\websocket

call loc.exe .\src\zoo\webpack

call loc.exe .\src\zoo\flash-policy-server

pause
Binary file added loc.exe
Binary file not shown.
59 changes: 13 additions & 46 deletions src/core/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>tio-core</artifactId>
Expand All @@ -7,7 +8,7 @@
<parent>
<groupId>org.t-io</groupId>
<artifactId>tio-parent</artifactId>
<version>3.0.6.v20180626-RELEASE</version>
<version>3.2.2.v20181122-RELEASE</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>

Expand All @@ -17,61 +18,27 @@
<artifactId>tio-utils</artifactId>
</dependency>

<!-- slf4j-logback绑定 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>provided</scope>
</dependency>
<!-- redisson, tio内置集群会用到 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<scope>provided</scope>
</dependency>


<!-- redirect apache commons logging -->
<!-- org.tio.monitor.RateLimiterWrap会用到 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<scope>provided</scope>
</dependency>
<!-- redirect jdk util logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<scope>provided</scope>
</dependency>
<!-- redirect log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<scope>provided</scope>
</dependency>

<!-- redisson, 集群会用到 -->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- slf4j-logback绑定 -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public class ClientChannelContext extends ChannelContext {
public ClientChannelContext(GroupContext groupContext, AsynchronousSocketChannel asynchronousSocketChannel) {
super(groupContext, asynchronousSocketChannel);
}

/**
* 创建一个虚拟ChannelContext,主要用来模拟一些操作,真实场景中用得少
* @param groupContext
*/
public ClientChannelContext(GroupContext groupContext) {
super(groupContext);
}

/**
* @see org.tio.core.ChannelContext#createClientNode(java.nio.channels.AsynchronousSocketChannel)
Expand Down
15 changes: 14 additions & 1 deletion src/core/src/main/java/org/tio/client/ClientGroupContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class ClientGroupContext extends GroupContext {

private ClientAioListener clientAioListener = null;

protected ReconnConf reconnConf;//重连配置

private ConnectionCompletionHandler connectionCompletionHandler = new ConnectionCompletionHandler();

Expand Down Expand Up @@ -157,6 +158,15 @@ public void setConnectionCompletionHandler(ConnectionCompletionHandler connectio
public void setReconnConf(ReconnConf reconnConf) {
this.reconnConf = reconnConf;
}

/**
*
* @return
* @author tanyaowu
*/
public ReconnConf getReconnConf() {
return reconnConf;
}

/**
* @return
Expand All @@ -166,5 +176,8 @@ public void setReconnConf(ReconnConf reconnConf) {
public boolean isServer() {
return false;
}

@Override
public String toString() {
return "ClientGroupContext [name=" + name + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ private void handler(Void result, ConnectionCompletionVo attachment, Throwable t
} else {
channelContext = new ClientChannelContext(clientGroupContext, asynchronousSocketChannel);
channelContext.setServerNode(serverNode);
channelContext.stat.timeClosed = SystemTimer.currentTimeMillis();
}

channelContext.setBindIp(bindIp);
Expand All @@ -101,14 +100,13 @@ private void handler(Void result, ConnectionCompletionVo attachment, Throwable t

log.info("connected to {}", serverNode);
if (isConnected && !isReconnect) {
channelContext.stat.setTimeFirstConnected(SystemTimer.currentTimeMillis());
channelContext.stat.setTimeFirstConnected(SystemTimer.currTime);
}
} else {
log.error(throwable.toString(), throwable);
if (channelContext == null) {
channelContext = new ClientChannelContext(clientGroupContext, asynchronousSocketChannel);
channelContext.setServerNode(serverNode);
channelContext.stat.setTimeClosed(SystemTimer.currentTimeMillis());
}

if (!isReconnect) //不是重连,则是第一次连接,需要把channelContext加到closeds行列
Expand All @@ -130,16 +128,20 @@ private void handler(Void result, ConnectionCompletionVo attachment, Throwable t
try {
channelContext.setReconnect(isReconnect);

if (SslUtils.isSsl(channelContext)) {
if (SslUtils.isSsl(channelContext.groupContext)) {
if (isConnected) {
// channelContext.sslFacadeContext.beginHandshake();
SslFacadeContext sslFacadeContext = new SslFacadeContext(channelContext);
sslFacadeContext.beginHandshake();
} else {
clientAioListener.onAfterConnected(channelContext, isConnected, isReconnect);
if (clientAioListener != null) {
clientAioListener.onAfterConnected(channelContext, isConnected, isReconnect);
}
}
} else {
clientAioListener.onAfterConnected(channelContext, isConnected, isReconnect);
if (clientAioListener != null) {
clientAioListener.onAfterConnected(channelContext, isConnected, isReconnect);
}
}

GroupContext groupContext = channelContext.groupContext;
Expand Down
4 changes: 2 additions & 2 deletions src/core/src/main/java/org/tio/client/ReconnConf.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static boolean isNeedReconn(ClientChannelContext clientChannelContext, bo
if (reconnConf != null && reconnConf.getInterval() > 0) {
if (reconnConf.getRetryCount() <= 0 || reconnConf.getRetryCount() >= clientChannelContext.getReconnCount()) {
if (putIfTrue) {
clientChannelContext.stat.timeInReconnQueue = SystemTimer.currentTimeMillis();
clientChannelContext.stat.timeInReconnQueue = SystemTimer.currTime;
reconnConf.getQueue().add(clientChannelContext);
}
return true;
Expand Down Expand Up @@ -56,7 +56,7 @@ public static void put(ClientChannelContext clientChannelContext) {
/**
* 用来重连的线程池
*/
private ThreadPoolExecutor threadPoolExecutor = null;
private volatile ThreadPoolExecutor threadPoolExecutor = null;

/**
*
Expand Down
40 changes: 18 additions & 22 deletions src/core/src/main/java/org/tio/client/TioClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.client.intf.ClientAioHandler;
Expand All @@ -24,6 +23,7 @@
import org.tio.core.ssl.SslFacadeContext;
import org.tio.core.stat.ChannelStat;
import org.tio.utils.SystemTimer;
import org.tio.utils.hutool.StrUtil;
import org.tio.utils.lock.SetWithLock;

/**
Expand Down Expand Up @@ -61,23 +61,23 @@ public void run() {
WriteLock writeLock = closeLock.writeLock();
writeLock.lock();
try {
if (!channelContext.isClosed()) //已经连上了,不需要再重连了
if (!channelContext.isClosed) //已经连上了,不需要再重连了
{
return;
}
long start = SystemTimer.currentTimeMillis();
long start = SystemTimer.currTime;
tioClient.reconnect(channelContext, 2);
long end = SystemTimer.currentTimeMillis();
long end = SystemTimer.currTime;
long iv = end - start;
if (iv >= 100) {
log.error("{},重连耗时:{} ms", channelContext, iv);
} else {
log.info("{},重连耗时:{} ms", channelContext, iv);
}

if (channelContext.isClosed()) {
if (channelContext.isClosed) {
channelContext.setReconnCount(channelContext.getReconnCount() + 1);
// cacheMap.put(channelContext.getServerNode(), SystemTimer.currentTimeMillis());
// cacheMap.put(channelContext.getServerNode(), SystemTimer.currTime);
return;
}
} catch (java.lang.Throwable e) {
Expand Down Expand Up @@ -215,9 +215,9 @@ private ClientChannelContext connect(Node serverNode, String bindIp, Integer bin
boolean isReconnect = initClientChannelContext != null;
// ClientAioListener clientAioListener = clientGroupContext.getClientAioListener();

long start = SystemTimer.currentTimeMillis();
long start = SystemTimer.currTime;
asynchronousSocketChannel = AsynchronousSocketChannel.open(channelGroup);
long end = SystemTimer.currentTimeMillis();
long end = SystemTimer.currTime;
long iv = end - start;
if (iv >= 100) {
log.error("{}, open 耗时:{} ms", channelContext, iv);
Expand All @@ -229,7 +229,7 @@ private ClientChannelContext connect(Node serverNode, String bindIp, Integer bin

InetSocketAddress bind = null;
if (bindPort != null && bindPort > 0) {
if (StringUtils.isNotBlank(bindIp)) {
if (false == StrUtil.isBlank(bindIp)) {
bind = new InetSocketAddress(bindIp, bindPort);
} else {
bind = new InetSocketAddress(bindPort);
Expand All @@ -242,7 +242,7 @@ private ClientChannelContext connect(Node serverNode, String bindIp, Integer bin

channelContext = initClientChannelContext;

start = SystemTimer.currentTimeMillis();
start = SystemTimer.currTime;

InetSocketAddress inetSocketAddress = new InetSocketAddress(serverNode.getIp(), serverNode.getPort());

Expand Down Expand Up @@ -345,10 +345,10 @@ public void run() {
readLock.lock();
try {
Set<ChannelContext> set = setWithLock.getObj();
long currtime = SystemTimer.currentTimeMillis();
long currtime = SystemTimer.currTime;
for (ChannelContext entry : set) {
ClientChannelContext channelContext = (ClientChannelContext) entry;
if (channelContext.isClosed() || channelContext.isRemoved()) {
if (channelContext.isClosed || channelContext.isRemoved) {
continue;
}

Expand All @@ -358,7 +358,9 @@ public void run() {
if (interval >= clientGroupContext.heartbeatTimeout / 2) {
Packet packet = aioHandler.heartbeatPacket();
if (packet != null) {
log.info("{}发送心跳包", channelContext.toString());
if (log.isInfoEnabled()) {
log.info("{}发送心跳包", channelContext.toString());
}
Tio.send(channelContext, packet);
}
}
Expand Down Expand Up @@ -417,7 +419,7 @@ public void run() {
// return;
}

if (channelContext.isRemoved()) //已经删除的,不需要重新再连
if (channelContext.isRemoved) //已经删除的,不需要重新再连
{
continue;
}
Expand All @@ -427,7 +429,7 @@ public void run() {
sslFacadeContext.setHandshakeCompleted(false);
}

long sleeptime = reconnConf.getInterval() - (SystemTimer.currentTimeMillis() - channelContext.stat.timeInReconnQueue);
long sleeptime = reconnConf.getInterval() - (SystemTimer.currTime - channelContext.stat.timeInReconnQueue);
//log.info("sleeptime:{}, closetime:{}", sleeptime, timeInReconnQueue);
if (sleeptime > 0) {
try {
Expand All @@ -437,7 +439,7 @@ public void run() {
}
}

if (channelContext.isRemoved() || !channelContext.isClosed()) //已经删除的和已经连上的,不需要重新再连
if (channelContext.isRemoved || !channelContext.isClosed) //已经删除的和已经连上的,不需要重新再连
{
continue;
}
Expand Down Expand Up @@ -469,18 +471,12 @@ public boolean stop() {
} catch (Exception e1) {
log.error(e1.toString(), e1);
}
try {
clientGroupContext.tioCloseExecutor.shutdown();
} catch (Exception e1) {
log.error(e1.toString(), e1);
}


clientGroupContext.setStopped(true);
try {
ret = ret && clientGroupContext.groupExecutor.awaitTermination(6000, TimeUnit.SECONDS);
ret = ret && clientGroupContext.tioExecutor.awaitTermination(6000, TimeUnit.SECONDS);
ret = ret && clientGroupContext.tioCloseExecutor.awaitTermination(6000, TimeUnit.SECONDS);
} catch (InterruptedException e) {
log.error(e.getLocalizedMessage(), e);
}
Expand Down
Loading

0 comments on commit a2bdfed

Please sign in to comment.