funny / snet Goto Github PK
View Code? Open in Web Editor NEWA long lasting connection protocol for mobile applications that supports reconnection and encryption.
License: Do What The F*ck You Want To Public License
A long lasting connection protocol for mobile applications that supports reconnection and encryption.
License: Do What The F*ck You Want To Public License
可以简单说一下吗?
Unity .net 库里面的 TcpClient 需要在构造函数显示设置 AddressFamily
改进点:
新建连接,上行:
新建连接时,客户端先发送一个全0的字节告知服务端这是一个新连接
接着客户端发送8个字节的握手请求
PublicKey为DH密钥交换用的公钥
type HandshakeRequest struct {
PublicKey uint64
}
新建连接,下行:
当服务端收到新建连接请求后,下发16个字节的握手响应
PublicKey为DH密钥交换用的公钥
CryptedConnID为加密后的连接ID,加密所需密钥通过DH密钥交换算法计算得出
type HandshakeResponse struct {
PublicKey uint64
CryptedConnID uint64
}
重连,上行:
当客户端尝试重连时,新建一个TCP/IP连接,并发送一个全1的字节告知服务端这是一个重连
接着客户端40个字节的重连请求
ConnID为连接ID
WriteCount为客户端已发送字节数
ReadCount为客户端已接收字节数
MD5Hash为消息前24个字节加通讯密钥计算得出的MD5哈希值
type ReconnRequest struct {
ConnID uint64
WriteCount uint64
ReadCount uint64
MD5Hash [16]byte
}
重连,下行:
当服务端接收到重连请求时,对连接的合法性进行验证
服务端下发16个字节的重连响应
验证失败:
验证成功:
紧接着服务端立即下发需要重传的数据
客户端在收到重连响应后,比较收发字节数差值来读取服务端下发的重传数据
type ReconnResponse struct {
WriteCount uint64
ReadCount uint64
}
如题
现在在C#端, 就算调用方是单线程在调用发送, 底下会把发送放到个线程池中, 短时间内发送多条消息时, 更容易发生消息的顺序改变的情况.
这样的话, 就要求使用snet的项目对C#发过来的消息的顺序不能有要求, 甚至可能在逻辑上会有问题, 比如客户端是先跳再放的技能, 服务器先收到了放技能的消息.
public override IAsyncResult BeginWrite (byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
AsyncResult ar1 = new AsyncResult (callback, state);
ThreadPool.QueueUserWorkItem ((object ar2) => {
AsyncResult ar3 = (AsyncResult)ar2;
try {
Write(buffer, offset, count);
} catch(Exception ex) {
ar3.Error = ex;
}
ar3.IsCompleted = true;
((ManualResetEvent)ar3.AsyncWaitHandle).Set();
if (ar3.Callback != null)
ar3.Callback(ar3);
}, ar1);
return ar1;
}
tryReconn里会触调用conn.Write
,同时会加c.reconnMutex.Lock()
锁,而Write方法里会加
c.reconnMutex.RLock()
,造成重入问题。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.