Skip to content

Commit

Permalink
add Netty
Browse files Browse the repository at this point in the history
  • Loading branch information
milovetingting committed Apr 12, 2020
1 parent 549355d commit 38f5957
Show file tree
Hide file tree
Showing 97 changed files with 1,332 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Netty/.classpath
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>
17 changes: 17 additions & 0 deletions Netty/.project
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>
2 changes: 2 additions & 0 deletions Netty/.settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8
11 changes: 11 additions & 0 deletions Netty/.settings/org.eclipse.jdt.core.prefs
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 added Netty/bin/com/wangyz/netty/Main$1$1.class
Binary file not shown.
Binary file added Netty/bin/com/wangyz/netty/Main$1.class
Binary file not shown.
Binary file added Netty/bin/com/wangyz/netty/Main.class
Binary file not shown.
Binary file added Netty/bin/com/wangyz/netty/client/Config.class
Binary file not shown.
Binary file added Netty/bin/com/wangyz/netty/client/Global.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added Netty/bin/com/wangyz/netty/client/bean/Msg.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 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 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 Netty/bin/com/wangyz/netty/client/util/LogUtil.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added Netty/bin/com/wangyz/netty/server/Config.class
Binary file not shown.
Binary file added Netty/bin/com/wangyz/netty/server/Global.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 Netty/bin/com/wangyz/netty/server/bean/Msg.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 Netty/bin/com/wangyz/netty/server/util/LogUtil.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added Netty/libs/gson-2.8.6.jar
Binary file not shown.
Binary file added Netty/libs/netty-all-4.1.48.Final.jar
Binary file not shown.
59 changes: 59 additions & 0 deletions Netty/src/com/wangyz/netty/Main.java
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();
}

}

}
7 changes: 7 additions & 0 deletions Netty/src/com/wangyz/netty/client/Config.java
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 = "#$&*";

}
9 changes: 9 additions & 0 deletions Netty/src/com/wangyz/netty/client/Global.java
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();

}
125 changes: 125 additions & 0 deletions Netty/src/com/wangyz/netty/client/bean/Msg.java
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 + "]";
}

}
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 Netty/src/com/wangyz/netty/client/connection/IConnection.java
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);

}
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 Netty/src/com/wangyz/netty/client/connection/IMsgCallback.java
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 Netty/src/com/wangyz/netty/client/connection/NettyConnection.java
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);
}

}
Loading

0 comments on commit 38f5957

Please sign in to comment.