-
Notifications
You must be signed in to change notification settings - Fork 77
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
1 parent
549355d
commit 38f5957
Showing
97 changed files
with
1,332 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> | ||
<classpathentry kind="lib" path="libs/netty-all-4.1.48.Final.jar"/> | ||
<classpathentry kind="lib" path="libs/gson-2.8.6.jar"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
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,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>Netty</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
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,2 @@ | ||
eclipse.preferences.version=1 | ||
encoding/<project>=UTF-8 |
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,11 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.8 | ||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.source=1.8 |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+559 Bytes
Netty/bin/com/wangyz/netty/client/bean/Msg$LoginResponseInfo$ResponseData.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+851 Bytes
Netty/bin/com/wangyz/netty/client/connection/ConnectionManager$ConnectionManagerInner.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+182 Bytes
Netty/bin/com/wangyz/netty/client/connection/IConnectionCallback.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+761 Bytes
Netty/bin/com/wangyz/netty/client/dispatcher/MsgQueue$MsgQueueInner.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+844 Bytes
Netty/bin/com/wangyz/netty/client/handler/HeartBeatMsgHandler.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+1016 Bytes
Netty/bin/com/wangyz/netty/client/wrapper/ChannelHandlerContextWrapper.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+559 Bytes
Netty/bin/com/wangyz/netty/server/bean/Msg$LoginResponseInfo$ResponseData.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+734 Bytes
Netty/bin/com/wangyz/netty/server/dispatcher/HeartBeatMsgHandler.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+1016 Bytes
Netty/bin/com/wangyz/netty/server/wrapper/ChannelHandlerContextWrapper.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,59 @@ | ||
package com.wangyz.netty; | ||
|
||
import com.google.gson.Gson; | ||
import com.wangyz.netty.client.bean.Msg; | ||
import com.wangyz.netty.client.bean.Msg.LoginRuquestInfo; | ||
import com.wangyz.netty.client.connection.ConnectionManager; | ||
import com.wangyz.netty.client.connection.IConnectionCallback; | ||
import com.wangyz.netty.client.connection.IMsgCallback; | ||
import com.wangyz.netty.client.connection.NettyConnection; | ||
import com.wangyz.netty.client.dispatcher.MsgDispatcher; | ||
import com.wangyz.netty.client.dispatcher.MsgQueue; | ||
import com.wangyz.netty.client.util.LogUtil; | ||
import com.wangyz.netty.server.NettyServer; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
try { | ||
String host = "127.0.0.1"; | ||
int port = 12345; | ||
NettyServer server = new NettyServer(port); | ||
server.run(); | ||
Thread.sleep(1000); | ||
ConnectionManager.initConnection(new NettyConnection()); | ||
ConnectionManager.getInstance().connect(host, port, new IConnectionCallback() { | ||
|
||
@Override | ||
public void onConnected() { | ||
LogUtil.log("Main,onConnected"); | ||
|
||
ConnectionManager.getInstance().registerMsgCallback(new IMsgCallback() { | ||
|
||
@Override | ||
public void onMsgReceived(Msg msg) { | ||
MsgQueue.getInstance().enqueueMsg(msg); | ||
MsgDispatcher.dispatch(); | ||
} | ||
}); | ||
|
||
Msg msg = new Msg(); | ||
msg.type = Msg.TYPE_LOGIN; | ||
|
||
Msg.LoginRuquestInfo request = new LoginRuquestInfo(); | ||
request.user = "wangyz"; | ||
request.pwd = "wangyz"; | ||
|
||
Gson gson = new Gson(); | ||
msg.msg = gson.toJson(request); | ||
|
||
ConnectionManager.getInstance().sendMsg(msg); | ||
} | ||
}); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
|
||
} |
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,7 @@ | ||
package com.wangyz.netty.client; | ||
|
||
public class Config { | ||
|
||
public static final String DATA_PACK_SEPARATOR = "#$&*"; | ||
|
||
} |
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,9 @@ | ||
package com.wangyz.netty.client; | ||
|
||
import com.google.gson.Gson; | ||
|
||
public class Global { | ||
|
||
public static final Gson sGson = new Gson(); | ||
|
||
} |
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,125 @@ | ||
package com.wangyz.netty.client.bean; | ||
|
||
public class Msg { | ||
|
||
/** | ||
* 欢迎 | ||
*/ | ||
public static final int TYPE_WELCOME = 0; | ||
|
||
/** | ||
* 心跳 | ||
*/ | ||
public static final int TYPE_HEART_BEAT = 1; | ||
|
||
/** | ||
* 登录 | ||
*/ | ||
public static final int TYPE_LOGIN = 2; | ||
|
||
public static final int TYPE_COMMAND_A = 3; | ||
|
||
public static final int TYPE_COMMAND_B = 4; | ||
|
||
public static final int TYPE_COMMAND_C = 5; | ||
|
||
public int type; | ||
|
||
public String msg; | ||
|
||
public int priority; | ||
|
||
public long time; | ||
|
||
/** | ||
* 登录请求信息 | ||
* | ||
* @author Administrator | ||
* | ||
*/ | ||
public static class LoginRuquestInfo { | ||
/** | ||
* 用户名 | ||
*/ | ||
public String user; | ||
|
||
/** | ||
* 密码 | ||
*/ | ||
public String pwd; | ||
|
||
@Override | ||
public String toString() { | ||
return "LoginRuquestInfo [user=" + user + ", pwd=" + pwd + "]"; | ||
} | ||
} | ||
|
||
/** | ||
* 登录响应信息 | ||
* | ||
* @author Administrator | ||
* | ||
*/ | ||
public static class LoginResponseInfo { | ||
|
||
/** | ||
* 登录成功 | ||
*/ | ||
public static final int CODE_SUCCESS = 0; | ||
|
||
/** | ||
* 登录失败 | ||
*/ | ||
public static final int CODE_FAILED = 100; | ||
|
||
/** | ||
* 响应码 | ||
*/ | ||
public int code; | ||
|
||
/** | ||
* 响应数据 | ||
*/ | ||
public String data; | ||
|
||
public static class ResponseData { | ||
public String token; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "LoginResponseInfo [code=" + code + ", data=" + data + "]"; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* 指令信息 | ||
* | ||
* @author Administrator | ||
* | ||
*/ | ||
public static class Command { | ||
/** | ||
* 指令id | ||
*/ | ||
public String commandId; | ||
|
||
/** | ||
* 指令内容 | ||
*/ | ||
public String command; | ||
|
||
@Override | ||
public String toString() { | ||
return "Command [commandId=" + commandId + ", command=" + command + "]"; | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Msg [type=" + type + ", msg=" + msg + ", priority=" + priority + ", time=" + time + "]"; | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
Netty/src/com/wangyz/netty/client/connection/ConnectionManager.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,49 @@ | ||
package com.wangyz.netty.client.connection; | ||
|
||
import com.wangyz.netty.client.bean.Msg; | ||
|
||
public class ConnectionManager implements IConnection { | ||
|
||
private static IConnection mConnection; | ||
|
||
private ConnectionManager() { | ||
|
||
} | ||
|
||
static class ConnectionManagerInner { | ||
private static ConnectionManager INSTANCE = new ConnectionManager(); | ||
} | ||
|
||
public static ConnectionManager getInstance() { | ||
return ConnectionManagerInner.INSTANCE; | ||
} | ||
|
||
public static void initConnection(IConnection connection) { | ||
mConnection = connection; | ||
} | ||
|
||
private void checkInit() { | ||
if (mConnection == null) { | ||
throw new IllegalAccessError("please invoke initConnection first!"); | ||
} | ||
} | ||
|
||
@Override | ||
public void connect(String host, int port, IConnectionCallback callback) { | ||
checkInit(); | ||
mConnection.connect(host, port, callback); | ||
} | ||
|
||
@Override | ||
public void sendMsg(Msg msg) { | ||
checkInit(); | ||
mConnection.sendMsg(msg); | ||
} | ||
|
||
@Override | ||
public void registerMsgCallback(IMsgCallback callback) { | ||
checkInit(); | ||
mConnection.registerMsgCallback(callback); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
Netty/src/com/wangyz/netty/client/connection/IConnection.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,30 @@ | ||
package com.wangyz.netty.client.connection; | ||
|
||
import com.wangyz.netty.client.bean.Msg; | ||
|
||
public interface IConnection { | ||
|
||
/** | ||
* 连接服务器 | ||
* | ||
* @param host 服务器地址 | ||
* @param port 端口 | ||
* @param callback 连接回调 | ||
*/ | ||
public void connect(String host, int port, IConnectionCallback callback); | ||
|
||
/** | ||
* 发送消息 | ||
* | ||
* @param msg | ||
*/ | ||
public void sendMsg(Msg msg); | ||
|
||
/** | ||
* 注册消息回调 | ||
* | ||
* @param callback | ||
*/ | ||
public void registerMsgCallback(IMsgCallback callback); | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
Netty/src/com/wangyz/netty/client/connection/IConnectionCallback.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,10 @@ | ||
package com.wangyz.netty.client.connection; | ||
|
||
public interface IConnectionCallback { | ||
|
||
/** | ||
* 连接成功 | ||
*/ | ||
public void onConnected(); | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
Netty/src/com/wangyz/netty/client/connection/IMsgCallback.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,14 @@ | ||
package com.wangyz.netty.client.connection; | ||
|
||
import com.wangyz.netty.client.bean.Msg; | ||
|
||
public interface IMsgCallback { | ||
|
||
/** | ||
* 接收到消息时的回调 | ||
* | ||
* @param msg | ||
*/ | ||
public void onMsgReceived(Msg msg); | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
Netty/src/com/wangyz/netty/client/connection/NettyConnection.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,35 @@ | ||
package com.wangyz.netty.client.connection; | ||
|
||
import com.wangyz.netty.client.bean.Msg; | ||
import com.wangyz.netty.client.connector.NettyClient; | ||
|
||
public class NettyConnection implements IConnection { | ||
|
||
private NettyClient mClient; | ||
|
||
@Override | ||
public void connect(String host, int port, IConnectionCallback callback) { | ||
if (mClient == null) { | ||
mClient = new NettyClient(host, port); | ||
mClient.setConnectionCallBack(callback); | ||
mClient.connect(); | ||
} | ||
} | ||
|
||
@Override | ||
public void sendMsg(Msg msg) { | ||
if (mClient == null) { | ||
throw new IllegalAccessError("please invoke connect first!"); | ||
} | ||
mClient.sendMsg(msg); | ||
} | ||
|
||
@Override | ||
public void registerMsgCallback(IMsgCallback callback) { | ||
if (mClient == null) { | ||
throw new IllegalAccessError("please invoke connect first!"); | ||
} | ||
mClient.registerMsgCallback(callback); | ||
} | ||
|
||
} |
Oops, something went wrong.