├── .classpath ├── .gitignore ├── .project ├── .settings ├── org.eclipse.core.resources.prefs ├── org.eclipse.jdt.core.prefs └── org.eclipse.m2e.core.prefs ├── README.md ├── pom.xml ├── tiny-client ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.core.resources.prefs │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── pom.xml └── src │ ├── main │ ├── java │ │ └── tiny │ │ │ └── client │ │ │ ├── ClientMain.java │ │ │ ├── handler │ │ │ ├── child │ │ │ │ └── L2CChannelInitializer.java │ │ │ └── msg │ │ │ │ └── C2SMessageInit.java │ │ │ └── net │ │ │ ├── ClientManager.java │ │ │ └── ClientServer.java │ └── resources │ │ └── log4j2.xml │ └── test │ └── java │ └── tiny │ └── client │ └── AppTest.java ├── tiny-config ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.core.resources.prefs │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── dbGen │ ├── db.xml │ ├── dbGen.jar │ ├── deGen.bat │ └── tmp │ │ └── auto │ │ ├── bean │ │ ├── Friend.java │ │ ├── Guild.java │ │ └── Role.java │ │ └── table │ │ ├── Guilds.java │ │ ├── Roles.java │ │ └── _Tables_.java ├── pom.xml ├── properties │ └── net.properties ├── protoGen │ ├── gen.bat │ ├── protoc.exe │ └── protocol │ │ ├── C2LMessage.proto │ │ ├── G2LMessage.proto │ │ ├── L2CMessage.proto │ │ ├── L2GMessage.proto │ │ ├── enum.proto │ │ └── role.proto └── src │ └── main │ └── java │ ├── auto │ └── proto │ │ ├── C2LMessageProto.java │ │ ├── Enum.java │ │ ├── G2LMessageProto.java │ │ ├── L2CMessageProto.java │ │ ├── L2GMessageProto.java │ │ └── RoleProto.java │ └── tools │ └── ProtocolUtil.java ├── tiny-db ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.core.resources.prefs │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── pom.xml └── src │ ├── main │ └── java │ │ └── org │ │ └── tiny │ │ ├── auto │ │ ├── bean │ │ │ ├── Friend.java │ │ │ ├── Guild.java │ │ │ └── Role.java │ │ └── table │ │ │ ├── Guilds.java │ │ │ ├── Roles.java │ │ │ └── _Tables_.java │ │ ├── base │ │ ├── Bean.java │ │ ├── DBBase.java │ │ ├── ICacheTrigger.java │ │ ├── MongoBase.java │ │ ├── Procedure.java │ │ ├── Table.java │ │ ├── Tables.java │ │ └── Transaction.java │ │ ├── db │ │ ├── App.java │ │ ├── DBBootstrap.java │ │ ├── DBMessage.java │ │ ├── DBOption.java │ │ ├── DBThread.java │ │ ├── DBThreadExecutor.java │ │ └── TransactionThread.java │ │ └── util │ │ ├── SerializationUtil.java │ │ └── TestClone.java │ └── test │ └── java │ └── org │ └── tiny │ └── db │ ├── AppTest.java │ └── TestMongoDB.java ├── tiny-dbGen ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.core.resources.prefs │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── db.xml ├── pom.xml └── src │ ├── main │ └── java │ │ ├── auto │ │ ├── bean │ │ │ ├── Friend.java │ │ │ ├── Guild.java │ │ │ └── Role.java │ │ └── table │ │ │ ├── Guilds.java │ │ │ ├── Roles.java │ │ │ └── _Tables_.java │ │ └── org │ │ └── tiny │ │ ├── bean │ │ ├── BeanMeta.java │ │ ├── IMeta.java │ │ ├── TableMeta.java │ │ ├── TransferUtil.java │ │ └── VariableMeta.java │ │ ├── dbGen │ │ ├── BeanMetaGenerator.java │ │ └── Gen.java │ │ └── test │ │ └── PTestProcedure.java │ └── test │ └── java │ └── org │ └── tiny │ └── dbGen │ └── AppTest.java ├── tiny-gs ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.core.resources.prefs │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── pom.xml └── src │ ├── main │ ├── java │ │ └── tiny │ │ │ └── gs │ │ │ ├── ServerMain.java │ │ │ ├── aop │ │ │ └── HandlerAspectJAdvice.java │ │ │ ├── base │ │ │ └── AppContext.java │ │ │ ├── config │ │ │ ├── Conf.java │ │ │ └── ConfigManager.java │ │ │ ├── db │ │ │ ├── MongoBase.java │ │ │ ├── RoleDao.java │ │ │ └── RoleDaoImpl.java │ │ │ ├── exception │ │ │ └── UnCheckedException.java │ │ │ ├── handler │ │ │ ├── MsgHandler.java │ │ │ ├── ProtocolHandler.java │ │ │ ├── ProtocolHandlerRegisterManager.java │ │ │ ├── child │ │ │ │ └── C2GChannelInitializer.java │ │ │ └── msg │ │ │ │ ├── ProtocolHandlerManager.java │ │ │ │ └── role │ │ │ │ ├── C2SAddItemHandler.java │ │ │ │ ├── C2SComplexTestHandler.java │ │ │ │ ├── C2SRoleInfoHandler.java │ │ │ │ └── procedure │ │ │ │ ├── PAddItem.java │ │ │ │ └── PUpdateRoleInfo.java │ │ │ ├── net │ │ │ ├── GSManager.java │ │ │ └── GameServer.java │ │ │ ├── task │ │ │ ├── TaskPool.java │ │ │ └── UserTask.java │ │ │ └── util │ │ │ ├── AssertUtil.java │ │ │ └── ExceptionUtil.java │ └── resources │ │ ├── applicationContext.xml │ │ ├── jdbc.properties │ │ ├── log4j2.xml │ │ └── mongo.xml │ └── test │ └── java │ └── tiny │ └── gs │ └── AppTest.java ├── tiny-link ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.core.resources.prefs │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── pom.xml └── src │ ├── main │ ├── java │ │ └── org │ │ │ └── tiny │ │ │ └── link │ │ │ ├── channel │ │ │ └── ChannelManager.java │ │ │ ├── init │ │ │ ├── C2LChannelInitializer.java │ │ │ ├── G2LChannelInitializer.java │ │ │ └── handler │ │ │ │ ├── C2LHandlerAdapter.java │ │ │ │ ├── G2LHandlerAdapter.java │ │ │ │ └── ReSend.java │ │ │ └── net │ │ │ ├── LinkManager.java │ │ │ └── LinkServer.java │ └── resources │ │ └── log4j2.xml │ └── test │ └── java │ └── org │ └── tiny │ └── link │ └── AppTest.java └── tiny-net ├── .classpath ├── .gitignore ├── .project ├── .settings ├── org.eclipse.core.resources.prefs ├── org.eclipse.jdt.core.prefs └── org.eclipse.m2e.core.prefs ├── pom.xml └── src ├── main ├── java │ └── org │ │ └── tiny │ │ └── net │ │ ├── App.java │ │ ├── base │ │ ├── AppContext.java │ │ └── IMessage.java │ │ ├── core │ │ ├── AbstractChannelHandlerAdapter.java │ │ ├── AbstractChannelInitializer.java │ │ ├── AbstractIoService.java │ │ ├── AbstractManager.java │ │ ├── Acceptor.java │ │ └── Connector.java │ │ ├── event │ │ ├── Event.java │ │ ├── EventHandler.java │ │ ├── EventManager.java │ │ ├── eventimpl │ │ │ └── BiLogin.java │ │ └── handlerimpl │ │ │ └── BiHandler.java │ │ ├── log │ │ ├── AspectJAdvice.java │ │ ├── LogLevel.java │ │ ├── TinyLogger.java │ │ └── TinyLoggerAdvice.java │ │ ├── thread │ │ ├── AbstractMessageThread.java │ │ └── NamedThreadFactory.java │ │ └── tool │ │ ├── CollectionUtil.java │ │ └── ShortUUID.java └── resources │ └── applicationContext.xml └── test └── java └── org └── tiny └── net └── AppTest.java /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /tiny-bi 2 | *.log 3 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | TinyGameServer 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding/=UTF-8 3 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 3 | org.eclipse.jdt.core.compiler.compliance=1.5 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.5 6 | -------------------------------------------------------------------------------- /.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TinyGameServer 2 | 3 | ---- 4 | (Dota比较喜欢小小,tiny由此而来:sunglasses:) 5 | 6 | ### 相关技能 7 | 8 | - Maven、Spring 9 | - Java1.8、netty4、protobuf3(syntax2, 习惯用2了, 慢慢学习3) 10 | - spring-data-mongo 11 | 12 | ### maven结构 13 | 14 | - parent:tiny.gameserver 15 | - child: 16 | ``` 17 | tiny-gs   // 服务器主逻辑 18 | tiny-client // 测试用客户端 19 | tiny-config // 表格、协议等相关配置信息 20 | tiny-link // link服务器,服务器和客户端的中转 21 | tiny-net //  网络相关公用类 22 | tiny-db // 内存数据库 23 | ``` 24 | 25 | ### 项目功能 26 | 实现简单的游戏服务器功能,完成与客户端(用项目下的client测试)消息的通信 27 | 28 | ### 架构思想 29 | gs-link-client 30 | 31 | ### 使用介绍 32 | 33 | - 生成协议 tiny-config/protoGen/gen.bat  生成相关的协议文件 34 | - 生成数据可以bean tiny-config/dbGen/dbGen.bat 生成bean文件 (11.16更新为了方便测试 现在在AppContext中加入测试的方法) 35 | - 依次启动 ServerMain 、LinkServer、ClientMain,ClientMain控制台输入1或者2(C2SMessageInit编写)进行消息测试 36 | 37 | ### 后续工作 38 | 39 | - 加入打表工具,发便应用相关配置 40 | - aop接入日志 41 | - 建立内存数据库,接入nosql(redis、mongo) 42 | 43 | ### 最近更新 44 | 11.16更新 45 | - dbGen 数据库Bean生成工具(不断根据业务需求更新) 46 | - spring-data-mongo(查了半天的问题, 原来是版本没选对....) 47 | 48 | 11.22更新 49 | - mongo版本由2.x改为3.x(网上稍早的教程都是2.x的,改的时候一堆坑) 50 | - 将解码后的消息封装成task,派发到业务线程池中,以保证NIO线程被尽快释放 51 | 52 | 1.10 更新 53 | - 添加存储过程相关,事务还没弄完,后续继续完善。。 54 | 55 | 1.12 更新 56 | - 事务基本完成,异常回滚,先实现一种回滚(代码中的rollback0),有待优化 57 | 58 | ### 联系我 59 | 60 | - wechat `tiny_9892` 61 | - 欢迎沟通,共同进步 62 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | tiny.gameserver 6 | TinyGameServer 7 | 0.0.1-SNAPSHOT 8 | pom 9 | 10 | TinyGameServer 11 | http://maven.apache.org 12 | 13 | 14 | tiny-gs 15 | tiny-client 16 | tiny-config 17 | tiny-link 18 | tiny-net 19 | tiny-dbGen 20 | tiny-db 21 | tiny-bi 22 | 23 | 24 | 25 | UTF-8 26 | 4.1.16.Final 27 | 3.1.0 28 | 4.3.9.RELEASE 29 | 2.7 30 | 3.2.2 31 | 1.13.6.RELEASE 32 | 1.10.4.RELEASE 33 | 34 | 35 | 36 | 37 | 38 | io.netty 39 | netty-all 40 | ${netty.version} 41 | 42 | 43 | com.google.protobuf 44 | protobuf-java 45 | ${protobuf.version} 46 | 47 | 48 | 49 | org.springframework 50 | spring-beans 51 | ${spring.version} 52 | 53 | 54 | org.springframework 55 | spring-context 56 | ${spring.version} 57 | 58 | 59 | org.springframework 60 | spring-aop 61 | ${spring.version} 62 | 63 | 64 | 65 | org.springframework 66 | spring-core 67 | ${spring.version} 68 | 69 | 70 | 71 | org.springframework 72 | spring-aspects 73 | ${spring.version} 74 | 75 | 76 | 77 | 78 | 79 | org.apache.logging.log4j 80 | log4j-api 81 | ${log4j2.version} 82 | 83 | 84 | org.apache.logging.log4j 85 | log4j-core 86 | ${log4j2.version} 87 | 88 | 89 | org.apache.logging.log4j 90 | log4j-slf4j-impl 91 | ${log4j2.version} 92 | 93 | 94 | 95 | junit 96 | junit 97 | 3.8.1 98 | test 99 | 100 | 101 | 102 | 103 | org.mongodb 104 | mongo-java-driver 105 | ${mongodriver.version} 106 | 107 | 108 | 109 | org.springframework.data 110 | spring-data-commons 111 | ${springdatacommons.version} 112 | 113 | 114 | org.springframework.data 115 | spring-data-mongodb 116 | ${springdatamongodb.version} 117 | 118 | 119 | org.springframework.data 120 | spring-data-commons-core 121 | 1.4.1.RELEASE 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | org.apache.maven.plugins 132 | maven-compiler-plugin 133 | 134 | 1.8 135 | 1.8 136 | UTF-8 137 | 138 | 139 | 140 | 141 | org.apache.maven.plugins 142 | maven-resources-plugin 143 | 144 | 145 | copy-resources 146 | validate 147 | 148 | copy-resources 149 | 150 | 151 | ${project.build.directory}/ 152 | 153 | 154 | src/main/resources 155 | false 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | -------------------------------------------------------------------------------- /tiny-client/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /tiny-client/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /tiny-client/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | tiny-client 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /tiny-client/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/main/resources=UTF-8 4 | encoding//src/test/java=UTF-8 5 | encoding/=UTF-8 6 | -------------------------------------------------------------------------------- /tiny-client/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.compliance=1.8 5 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 6 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 7 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 8 | org.eclipse.jdt.core.compiler.source=1.8 9 | -------------------------------------------------------------------------------- /tiny-client/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /tiny-client/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | tiny.gameserver 8 | TinyGameServer 9 | 0.0.1-SNAPSHOT 10 | 11 | 12 | tiny-client 13 | 14 | tiny-client 15 | http://maven.apache.org 16 | 17 | UTF-8 18 | 19 | 20 | 21 | tiny.gameserver 22 | tiny-net 23 | 0.0.1-SNAPSHOT 24 | 25 | 26 | 27 | tiny.gameserver 28 | tiny-config 29 | 0.0.1-SNAPSHOT 30 | 31 | 32 | 33 | io.netty 34 | netty-all 35 | 36 | 37 | com.google.protobuf 38 | protobuf-java 39 | 40 | 41 | junit 42 | junit 43 | test 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /tiny-client/src/main/java/tiny/client/ClientMain.java: -------------------------------------------------------------------------------- 1 | package tiny.client; 2 | 3 | import org.tiny.net.log.TinyLogger; 4 | 5 | import tiny.client.net.ClientServer; 6 | 7 | public class ClientMain { 8 | public static void main(String[] args) { 9 | try { 10 | ClientServer.start(); 11 | TinyLogger.LOG.debug("Client Start SUCCESS !"); 12 | } catch (Exception e) { 13 | TinyLogger.LOG.error("Client Start ERROR !"); 14 | e.printStackTrace(); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tiny-client/src/main/java/tiny/client/handler/child/L2CChannelInitializer.java: -------------------------------------------------------------------------------- 1 | package tiny.client.handler.child; 2 | 3 | import org.tiny.net.core.AbstractChannelInitializer; 4 | 5 | import auto.proto.L2CMessageProto; 6 | import io.netty.channel.socket.SocketChannel; 7 | import io.netty.handler.codec.protobuf.ProtobufDecoder; 8 | import io.netty.handler.codec.protobuf.ProtobufEncoder; 9 | import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder; 10 | 11 | public class L2CChannelInitializer extends AbstractChannelInitializer{ 12 | 13 | @Override 14 | public void initChannelBefore(SocketChannel ch) { 15 | ch.pipeline().addLast(new ProtobufVarint32FrameDecoder()); 16 | ch.pipeline().addLast(new ProtobufDecoder(L2CMessageProto.L2CMessage.getDefaultInstance())); 17 | // ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender()); 18 | ch.pipeline().addLast(new ProtobufEncoder()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tiny-client/src/main/java/tiny/client/handler/msg/C2SMessageInit.java: -------------------------------------------------------------------------------- 1 | package tiny.client.handler.msg; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.Scanner; 6 | 7 | import org.tiny.net.core.AbstractChannelHandlerAdapter; 8 | import org.tiny.net.log.TinyLogger; 9 | 10 | import auto.proto.Enum.PROTO_KEY; 11 | import auto.proto.RoleProto; 12 | import auto.proto.RoleProto.C2SComplexTest.Option; 13 | import io.netty.channel.ChannelHandlerContext; 14 | import tools.ProtocolUtil; 15 | 16 | public class C2SMessageInit extends AbstractChannelHandlerAdapter { 17 | 18 | @Override 19 | public void doActice(ChannelHandlerContext ctx) { 20 | TinyLogger.LOG.debug("=====channelActive"); 21 | // RoleProto.Role.Builder builder = RoleProto.Role.newBuilder(); 22 | // builder.setId(1); 23 | // builder.setLevel(10); 24 | // builder.setNickName("first"); 25 | 26 | Scanner sc = new Scanner(System.in); 27 | TinyLogger.LOG.error("等待Console输入指令(1/2)"); 28 | while (sc.hasNextLine()) { 29 | String[] in = sc.nextLine().split(" "); 30 | 31 | int order = Integer.parseInt(in[0]); 32 | switch (order) { 33 | case 1: 34 | RoleProto.C2SRoleInfo.Builder builder = RoleProto.C2SRoleInfo.newBuilder(); 35 | builder.setId(11); 36 | builder.setContent("Hello World 1"); 37 | 38 | ctx.writeAndFlush(ProtocolUtil.toC2LMessage(PROTO_KEY.C2SRoleInfo_Key_VALUE, builder.build())); 39 | break; 40 | case 2: 41 | RoleProto.C2SComplexTest.Builder c2sComplexTest = RoleProto.C2SComplexTest.newBuilder(); 42 | c2sComplexTest.setRoleId(5748L); 43 | c2sComplexTest.setNickName("Tiny"); 44 | 45 | List