-
Notifications
You must be signed in to change notification settings - Fork 4
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
Showing
17 changed files
with
815 additions
and
6 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
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
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,15 @@ | ||
package com.lenovohit.lrouter; | ||
|
||
import com.lenovohit.lrouter_api.annotation.ioc.Action; | ||
import com.lenovohit.lrouter_api.core.socket.server.LRSocketAction; | ||
|
||
/** | ||
* Created by yuzhijun on 2017/6/13. | ||
*/ | ||
@Action(name = "socketAction",provider = "main") | ||
public class SocketAction extends LRSocketAction{ | ||
@Override | ||
public String socketInvoke(String receiveStr) { | ||
return receiveStr; | ||
} | ||
} |
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
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
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
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
12 changes: 12 additions & 0 deletions
12
lrouter-api/src/main/java/com/lenovohit/lrouter_api/core/socket/Const.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,12 @@ | ||
package com.lenovohit.lrouter_api.core.socket; | ||
|
||
/** | ||
* 用于放服务器连接的常量 | ||
* Created by yuzhijun on 2017/6/13. | ||
*/ | ||
public class Const { | ||
//读超时时间 | ||
public final static int SOCKET_READ_TIMOUT = 15 * 1000; | ||
//如果没有连接无服务器。读线程的sleep时间 | ||
public final static int SOCKET_SLEEP_SECOND = 3 ; | ||
} |
50 changes: 50 additions & 0 deletions
50
lrouter-api/src/main/java/com/lenovohit/lrouter_api/core/socket/LRSocketThreadManager.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,50 @@ | ||
package com.lenovohit.lrouter_api.core.socket; | ||
|
||
import android.os.Handler; | ||
|
||
import com.lenovohit.lrouter_api.core.callback.IRequestCallBack; | ||
import com.lenovohit.lrouter_api.core.socket.client.LRSocketClient; | ||
import com.lenovohit.lrouter_api.core.socket.client.LRSocketReceiveThread; | ||
import com.lenovohit.lrouter_api.core.socket.client.LRSocketSendThread; | ||
|
||
/** | ||
* 线程管理器 | ||
* Created by yuzhijun on 2017/6/13. | ||
*/ | ||
public class LRSocketThreadManager { | ||
private LRSocketThreadManager mSocketThreadManager = null; | ||
private LRSocketReceiveThread mReceiveThread = null; | ||
private LRSocketSendThread mSendThread = null; | ||
private LRSocketClient mLRSocketClient = null; | ||
|
||
public LRSocketThreadManager(String hostIP,int hostPort,IRequestCallBack requestCallBack){ | ||
mLRSocketClient = new LRSocketClient(hostIP,hostPort); | ||
mReceiveThread = new LRSocketReceiveThread(hostIP,hostPort,mLRSocketClient,requestCallBack); | ||
mSendThread = new LRSocketSendThread(hostIP,hostPort,mLRSocketClient); | ||
startThreads(); | ||
} | ||
|
||
private void startThreads(){ | ||
mReceiveThread.start(); | ||
mSendThread.start(); | ||
mReceiveThread.setStart(true); | ||
mSendThread.setStart(true); | ||
} | ||
|
||
public void stopThreads() { | ||
mReceiveThread.setStart(false); | ||
mSendThread.setStart(false); | ||
} | ||
|
||
public void releaseInstance() { | ||
if (mSocketThreadManager != null) { | ||
mSocketThreadManager.stopThreads(); | ||
mSocketThreadManager = null; | ||
} | ||
} | ||
|
||
public void sendMsg(byte [] buffer, Handler handler) { | ||
MsgObject entity = new MsgObject(buffer, handler); | ||
mSendThread.addMsg2Queue(entity); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
lrouter-api/src/main/java/com/lenovohit/lrouter_api/core/socket/MsgObject.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,29 @@ | ||
package com.lenovohit.lrouter_api.core.socket; | ||
|
||
import android.os.Handler; | ||
|
||
/** | ||
* 发送的实体 | ||
* Created by yuzhijun on 2017/6/13. | ||
*/ | ||
public class MsgObject { | ||
//要发送的消息 | ||
private byte [] bytes; | ||
//错误处理的handler | ||
private Handler mHandler; | ||
|
||
public MsgObject(byte [] bytes, Handler handler){ | ||
this.bytes = bytes; | ||
mHandler = handler; | ||
} | ||
|
||
public byte [] getBytes() | ||
{ | ||
return this.bytes; | ||
} | ||
|
||
public Handler getHandler() | ||
{ | ||
return mHandler; | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
lrouter-api/src/main/java/com/lenovohit/lrouter_api/core/socket/client/LRSocketClient.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,106 @@ | ||
package com.lenovohit.lrouter_api.core.socket.client; | ||
|
||
import com.lenovohit.lrouter_api.core.socket.Const; | ||
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.SelectionKey; | ||
import java.nio.channels.Selector; | ||
import java.nio.channels.SocketChannel; | ||
|
||
/** | ||
* 用于跟服务端(各个模块)连接 | ||
* Created by yuzhijun on 2017/6/13. | ||
*/ | ||
public class LRSocketClient { | ||
// 信道选择器 | ||
private Selector selector; | ||
// 与服务器通信的信道 | ||
SocketChannel socketChannel; | ||
//连接的服务器IP地址 | ||
private String hostIP; | ||
//连接的服务器监听的接口 | ||
private int hostPort; | ||
|
||
private boolean init = false; | ||
|
||
public LRSocketClient(String hostIP,int hostPort){ | ||
this.hostIP = hostIP; | ||
this.hostPort = hostPort; | ||
|
||
try{ | ||
init(); | ||
this.init = true; | ||
}catch (IOException e){ | ||
this.init = false; | ||
e.printStackTrace(); | ||
}catch (Exception e){ | ||
this.init = false; | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
* 初始化 | ||
* @throws IOException | ||
* */ | ||
public void init() throws IOException{ | ||
boolean done = false; | ||
|
||
try { | ||
// 打开监听信道并设置为非阻塞模式 | ||
socketChannel = SocketChannel.open(new InetSocketAddress(hostIP, hostPort)); | ||
if (socketChannel != null) { | ||
socketChannel.socket().setTcpNoDelay(false); | ||
socketChannel.socket().setKeepAlive(true); | ||
// 设置 读socket的timeout时间 | ||
socketChannel.socket().setSoTimeout(Const.SOCKET_READ_TIMOUT); | ||
socketChannel.configureBlocking(false); | ||
|
||
// 打开并注册选择器到信道 | ||
selector = Selector.open(); | ||
if (selector != null) { | ||
socketChannel.register(selector, SelectionKey.OP_READ); | ||
done = true; | ||
} | ||
} | ||
}finally { | ||
if (!done && selector != null) { | ||
selector.close(); | ||
} | ||
if (!done) { | ||
socketChannel.close(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 发送数据 | ||
* @param bytes | ||
* @throws IOException | ||
*/ | ||
public void sendMsg(byte[] bytes) throws IOException { | ||
ByteBuffer writeBuffer = ByteBuffer.wrap(bytes); | ||
|
||
if (socketChannel == null) { | ||
throw new IOException(); | ||
} | ||
socketChannel.write(writeBuffer); | ||
} | ||
|
||
/** | ||
* Socket连接是否是正常的 | ||
*/ | ||
public boolean isConnect() { | ||
boolean isConnect = false; | ||
if (this.init) { | ||
isConnect = this.socketChannel.isConnected(); | ||
} | ||
return isConnect; | ||
} | ||
|
||
public synchronized Selector getSelector() { | ||
return this.selector; | ||
} | ||
} |
Oops, something went wrong.