All Projects → huailiang → game_net

huailiang / game_net

Licence: MIT license
Unity游戏和C++服务器 socket通信demo, 数据格式采用google protobuf

Programming Languages

C++
36643 projects - #6 most used programming language
C#
18002 projects

Projects that are alternatives of or similar to game net

SocketIOUnity
A Wrapper for socket.io-client-csharp to work with Unity.
Stars: ✭ 69 (+30.19%)
Mutual labels:  socket
DatagramTunneler
Simple C++ cross-platform client/server app forwarding UDP datagrams through a TCP connection.
Stars: ✭ 116 (+118.87%)
Mutual labels:  socket
dystopia
Low to medium multithreaded Ubuntu Core honeypot coded in Python.
Stars: ✭ 59 (+11.32%)
Mutual labels:  socket
HiFramework.Unity
Based on component to manage project's core logic and module used in unity3d
Stars: ✭ 22 (-58.49%)
Mutual labels:  socket
LittleDrawBoard AN
基于socket实现的pc端和android端同步绘画板_ANDROID源码
Stars: ✭ 30 (-43.4%)
Mutual labels:  socket
react-webrtc-chat
React WebRTC chat
Stars: ✭ 39 (-26.42%)
Mutual labels:  socket
epoller
epoll implementation for connections in Linux, MacOS and Windows
Stars: ✭ 58 (+9.43%)
Mutual labels:  socket
chattt-backend
🖥 Backend for chattt
Stars: ✭ 17 (-67.92%)
Mutual labels:  socket
TweetMigration
A WebGL heatmap of global Twitter activity
Stars: ✭ 42 (-20.75%)
Mutual labels:  socket
ws2s
ws2s(websocket to socket)--bring socket to browser-side js.
Stars: ✭ 50 (-5.66%)
Mutual labels:  socket
video-chat
Simple Web Application that offer you to create video meeting room using WebRTC and Socket.
Stars: ✭ 32 (-39.62%)
Mutual labels:  socket
Socketify
Raw TCP and UDP Sockets API on Desktop Browsers
Stars: ✭ 67 (+26.42%)
Mutual labels:  socket
gosproto
基于云风的sproto二进制标准上的描述文件及代码生成工具
Stars: ✭ 52 (-1.89%)
Mutual labels:  google-protobuf
gctoolkit
Tool for parsing GC logs
Stars: ✭ 1,127 (+2026.42%)
Mutual labels:  gc
quick-net
This is a top level socket library, making servers and clients EASY!
Stars: ✭ 15 (-71.7%)
Mutual labels:  socket
AsyncSocket
Asynchronous socket (client+server) continues communications
Stars: ✭ 26 (-50.94%)
Mutual labels:  socket
TCP-IP-NP
《TCP/IP网络编程》((韩)尹圣雨) 学习笔记
Stars: ✭ 66 (+24.53%)
Mutual labels:  socket
video-group-meeting
WebRTC video chat for multi users using React and Node Express.
Stars: ✭ 40 (-24.53%)
Mutual labels:  socket
Picamera
基于树莓派的图传监控系统
Stars: ✭ 22 (-58.49%)
Mutual labels:  socket
asynchronous
A D port of Python's asyncio library
Stars: ✭ 35 (-33.96%)
Mutual labels:  socket

客户端是Unity2017.3.1f1, 采用c#语言,服务器端使用的是c++。

客户端采取的protobuf版本是:3.5.1 服务器对应的版本3.5.1

客户端

我们客户端protobuf来源于google官方的github仓库,对应本项目protobuf-lib文件夹,我们在生成的dll的时候通过后处理事件,同时生成对应的mdb文件,且copy到对应的unity目录。所以需要你配置环境变量: UnityInstallPath,即unity的安装路径,不包含到Editor

关于unity protobuf的信息可以参考:protocolbuffers/protobuf#644

通过修改上面的源码,在这里我们优化消除了protobuf在序列化和反序列化时候产生gc的状况。原生的代码使用了大量的.net4和.net6的语法, 我们也直接修改成net3以下 unity直接支持的语法。

之前的产生gc

修改之后消除gc:

优化操作:

在优化的时候,为了避免反序列化产生的实例(new),我们在每一个proto Message对应的类中加了一个共享的对象,这个对象只会new 一次,下次使用的时候直接改对象里的成员值,而不是直接new一个对象。

还有就是重写了CodeInputStream里的代码,内存中也是只会存在一个共享的CodeInputStream, 而不是每次都根据bytes[]或者stream来new一个新对象。

工具

tools目录:

在tools/ProtobufTool/目录中:

运行build.bat 会生成cs proto文件,并将拷贝到对应的unity目录中

运行build_server.bat 会生成.h .cc文件, 并拷贝到Server目录

服务器

请将项目需要的.proto 全部定义在game.proto文件中,自动生成的代码全部保存在PBMessage.cs中

运行build—server.bat 会生成服务器端对应的代码。

服务端采用的protobuf版本是:protobuf-cpp-3.5.1, 对应的下载地址:https://github.com/google/protobuf/releases/download/v3.5.1/protobuf-cpp-3.5.1.zip

服务器用到的protobuf编译生成的libprotocd.lib、libprotobufd.lib、protoc.exe由于文件太大, 没有上传。 读者自行生成,可以参照csdn这篇paper: https://blog.csdn.net/program_anywhere/article/details/77365876

服务器socket测试只支持windows平台,其他平台(linux)没有测试

运行效果如下图:

最后

我们在提供unity使用protobuff v3.0的同时,也提供了对protobuff v2.0的支持,对应的分支是: client_protobuf_v2

可以在终端上敲击下面命令,进入到

git clone https://github.com/huailiang/game_socket_protobuf.git

git checkout client_protobuf_v2

其他分支:

ierator_rcv:接收的消息的时候在协程里处理,这样就不会卡住主线程,处理回调的时候也不用考虑回调里使用unity api的问题

git checkout ierator_rcv

asyn_recv: 使用Socket自带的api异步方法: BeginReceive和EndReceive 这两个是对偶出现的。 c#内部实现是基于线程池(thread pool)的,所以不能直接再receive回调里使用unity api

git checkout asyn_recv

mutthread: 手动开启一个线程用来接收socket消息,不能直接再receive回调里使用unity api

git checkout mutthread
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].