void OnTCPReceived(string endPointIp, short channelId, byte[] data, int size) { if (channelId == (short)PreservedChannelId.Beacon) { } else if (channelId == (short)PreservedChannelId.Health) { if (nodeMap.ContainsKey(endPointIp)) { ComNode node = nodeMap[endPointIp]; node.HealthLostCount = 0; } } else if (!dataChannelMap.ContainsKey(channelId)) { } else { BytePacker packer = new BytePacker(data); if (nodeMap.ContainsKey(endPointIp)) { ComNode node = nodeMap[endPointIp]; node.HealthLostCount = 0; IDataChannel channel = dataChannelMap[channelId]; object container = channel.FromStream(ref packer); channel.Received(node, container); } } }
public async Task <bool> Send <T>(ComNode node, short channelId, T data) { if (portNumber == 0) { return(false); } return(await Task.Run(async() => { IDataChannel channel; if (!dataChannelMap.TryGetValue(channelId, out channel)) { return false; } bool isRent = false; byte[] buffer = null; int bufferSize = 0; BuildBuffer(channel, data, ref buffer, ref bufferSize, ref isRent); await udpTerminal.Send(node.Ip, portNumber, bufferSize, buffer).ConfigureAwait(false); if (isRent) { arrayPool.Return(buffer); } return true; }).ConfigureAwait(false)); }
void GenerateId(ref ComNode node) { int id; while (true) { try { userNodeMapLock.AcquireWriterLock(1000); disconnectedUserNodeMapLock.AcquireReaderLock(1000); id = userIdRandom.Next(1, int.MaxValue); if (!userNodeMap.ContainsKey(id) && !disconnectedUserNodeMap.ContainsKey(id)) { userNodeMap.Add(id, node); node.UserId = id; break; } } finally { disconnectedUserNodeMapLock.ReleaseReaderLock(); userNodeMapLock.ReleaseWriterLock(); } } }
public void AddAcceptList(string ip) { IPAddress address = IPAddress.Parse(ip); ComNode node = new ComNode(new IPEndPoint(address, portNumber)); nodeUdpMap.Add(ip, node); }
public async Task <bool> Send <T>(ComNode node, short channelId, T data) { if (sendPortNumber == 0) { return(false); } return(await Task.Run(async() => { if (!dataChannelMap.ContainsKey(channelId)) { return false; } IDataChannel channel = dataChannelMap[channelId]; bool isRent = false; byte[] buffer = null; int bufferSize = 0; BuildBuffer(channel, data, ref buffer, ref bufferSize, ref isRent); await udpSender.Send(node.IP, bufferSize, buffer); if (isRent) { arrayPool.Return(buffer); } return true; })); }
public void Received(ComNode node, object data) { if (OnReceived != null) { OnReceived(node, (byte[])data); } }
void OnUDPReceived(string endPointIp, byte[] data, int size) { int head = 0; while (head < size) { BytePacker packer = new BytePacker(data); short datasize = packer.ReadShort(); #if DISABLE_CHANNEL_VARINT short channelId = packer.ReadShort(); #else int s = 0; short channelId = VarintBitConverter.ToShort(packer, out s); #endif if (channelId == (short)PreservedChannelId.Beacon) { } else if (channelId == (short)PreservedChannelId.Health) { if (nodeMap.ContainsKey(endPointIp)) { ComNode node = nodeMap[endPointIp]; node.HealthLostCount = 0; } } else if (!dataChannelMap.ContainsKey(channelId)) { } else { IDataChannel channel = dataChannelMap[channelId]; if (channel.CheckMode == CheckMode.Sequre) { if (nodeMap.ContainsKey(endPointIp)) { ComNode node = nodeMap[endPointIp]; node.HealthLostCount = 0; object container = channel.FromStream(ref packer); channel.Received(node, container); } } else { object container = channel.FromStream(ref packer); channel.Received(null, container); } } head += datasize + 4; } }
void OnDisconnectedInternal(TCPConnection connection) { if (OnDisconnected != null) { OnDisconnected(serverNode); } serverNode = null; Util.Log("Client:Disconnected"); }
public async Task <bool> Send <T>(ComNode node, short channelId, T data) { if (!nodeMap.ContainsKey(node.IP)) { return(false); } if (!dataChannelMap.ContainsKey(channelId)) { return(false); } IDataChannel channel = dataChannelMap[channelId]; bool isRent = true; int bufSize = channel.GetDataSize(data); byte[] buf = arrayPool.Rent(bufSize + 6); if (buf == null) { isRent = false; buf = new byte[bufSize + 6]; } BytePacker packer = new BytePacker(buf); packer.Write((short)bufSize); #if DISABLE_CHANNEL_VARINT packer.Write(channelId); #else int s = 0; VarintBitConverter.SerializeShort(channelId, packer, out s); #endif channel.ToStream(data, ref packer); int maxpos = (int)packer.Position; if (channel.Qos == QosType.Reliable) { await node.Connection.Send(maxpos, buf); } else if (channel.Qos == QosType.Unreliable) { await udpSender.Send(node.IP, maxpos, buf); } if (isRent) { arrayPool.Return(buf); } return(true); }
public async Task <bool> Send <T>(ComNode node, short channelId, T data) { if (!node.IsConnected) { return(false); } else { return(await SendInternal <T>(node, channelId, data)); } }
public bool Disconnect(ComNode node) { if (nodeMap.ContainsKey(node.IP)) { ((ComTCPNode)node).Connection.Disconnect(); return(true); } else { return(false); } }
void OnKeyExchange(ComNode node, AesKeyPair data) { var aes = Aes.Create(); aes.Padding = PaddingMode.PKCS7; aes.Key = data.Key; aes.IV = data.IV; node.AesEncrypter = new AesEncrypter(aes); node.AesDecrypter = new AesDecrypter(aes); SendInternal(node, (short)PreservedChannelId.KeyExchangeAck, 0); }
void OnConnectedInternal(string ip, TCPConnection connection) { if (connection != null) { connection.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); lock (serverNodeLocker) { serverNode = new ComSnowballNode(connection); } udpTerminal.ReceiveStart(); connection.OnDisconnected = OnDisconnectedInternal; connection.OnPoll = OnPoll; IssueIdData ldata = new IssueIdData(); if (UserId != 0) { if (previousServerNode != null && previousServerNode.AesEncrypter != null) { ldata.Id = UserId; ldata.encryptionData = previousServerNode.AesEncrypter.Encrypt(Global.ReconnectRawData); } else { UserId = 0; } } if (UserId == 0) { ldata.Id = UserId; } SendInternal((short)PreservedChannelId.IssueId, ldata); healthLostCount = 0; //Util.Log("Client:Connected"); isConnecting = false; } else { isConnecting = false; OnConnectError(ip); } }
async Task <bool> SendInternal <T>(ComNode node, short channelId, T data) { return(await Task.Run(async() => { if (!nodeTcpMap.ContainsKey(node.TcpEndPoint)) { return false; } IDataChannel channel; if (!dataChannelMap.TryGetValue(channelId, out channel)) { return false; } bool isRent = false; byte[] buffer = null; int bufferSize = 0; IEncrypter encrypter = null; if (channel.Encryption == Encryption.Aes) { encrypter = node.AesEncrypter; } BuildBuffer(channel, data, ref buffer, ref bufferSize, ref isRent, encrypter); ComSnowballNode snode = (ComSnowballNode)node; if (channel.Qos == QosType.Reliable) { await snode.Connection.Send(bufferSize, buffer).ConfigureAwait(false); } else if (channel.Qos == QosType.Unreliable) { if (snode.UdpEndPoint != null) { await udpTerminal.Send(snode.Ip, snode.UdpEndPoint.Port, bufferSize, buffer).ConfigureAwait(false); } } if (isRent) { arrayPool.Return(buffer); } return true; }).ConfigureAwait(false)); }
public bool Disconnect(ComNode node) { if (!node.IsDisconnecting && nodeTcpMap.ContainsKey(node.TcpEndPoint)) { node.IsDisconnecting = true; ComSnowballNode snode = (ComSnowballNode)node; snode.Connection.Disconnect(); return(true); } else { return(false); } }
void OnConnectedInternal(TCPConnection connection) { if (connection == null) { return; } lock (this) { ComNode node = new ComNode(connection); nodeMap.Add(node.IP, node); connection.OnDisconnected = OnDisconnectedInternal; connection.OnReceive = OnTCPReceived; Util.Log("Server:Connected"); } }
void OnDisconnectedInternal(TCPConnection connection) { lock (this) { if (nodeMap.ContainsKey(connection.IP)) { ComNode node = nodeMap[connection.IP]; nodeMap.Remove(connection.IP); if (OnDisconnected != null) { OnDisconnected(node); } Util.Log("Server:Disconnected"); } } }
void OnConnectedInternal(string ip, TCPConnection connection) { if (connection != null) { serverNode = new ComTCPNode(connection); connection.OnDisconnected = OnDisconnectedInternal; connection.OnPoll = OnPoll; Send((short)PreservedChannelId.Login, UserName); if (OnConnected != null) { OnConnected(serverNode); } Util.Log("Client:Connected"); } isConnecting = false; }
void OnDisconnectedInternal(TCPConnection connection) { IsConnected = false; if (OnDisconnected != null) { OnDisconnected(serverNode); } lock (serverNodeLocker) { previousServerNode = serverNode; serverNode = null; } if (udpTerminal != null) { udpTerminal.ReceiveStop(); } isDisconnecting = false; }
public async Task <bool> Send <T>(ComNode node, short channelId, T data) { return(await Task.Run(async() => { if (!nodeMap.ContainsKey(node.IP)) { return false; } if (!dataChannelMap.ContainsKey(channelId)) { return false; } IDataChannel channel = dataChannelMap[channelId]; bool isRent = false; byte[] buffer = null; int bufferSize = 0; BuildBuffer(channel, data, ref buffer, ref bufferSize, ref isRent); if (channel.Qos == QosType.Reliable) { await((ComTCPNode)node).Connection.Send(bufferSize, buffer); } else if (channel.Qos == QosType.Unreliable) { await udpSender.Send(node.IP, bufferSize, buffer); } if (isRent) { arrayPool.Return(buffer); } return true; })); }
public async Task <bool> Broadcast <T>(ComGroup group, short channelId, T data, ComNode exception = null) { return(await Task.Run(async() => { if (!dataChannelMap.ContainsKey(channelId)) { return false; } IDataChannel channel = dataChannelMap[channelId]; bool isRent = false; byte[] buffer = null; int bufferSize = 0; BuildBuffer(channel, data, ref buffer, ref bufferSize, ref isRent); foreach (var node in group.NodeList) { if (node == exception) { continue; } if (!nodeMap.ContainsKey(node.IP)) { continue; } if (channel.Qos == QosType.Reliable) { await((ComTCPNode)node).Connection.Send(bufferSize, buffer); } else if (channel.Qos == QosType.Unreliable) { await udpSender.Send(node.IP, bufferSize, buffer); } } if (isRent) { arrayPool.Return(buffer); } return true; })); }
public async Task <bool> Send <T>(ComNode node, short channelId, T data) { return(await com.Send(node, channelId, data)); }
public bool Disconnect(ComNode node) { return(com.Disconnect(node)); }
public async Task <bool> Broadcast <T>(ComGroup group, short channelId, T data, ComNode exception = null) { IDataChannel channel; if (!dataChannelMap.TryGetValue(channelId, out channel)) { return(false); } bool isRent = false; byte[] buffer = null; int bufferSize = 0; if (channel.Encryption == Encryption.None) { BuildBuffer(channel, data, ref buffer, ref bufferSize, ref isRent, null); } foreach (var node in group) { if (node == exception) { continue; } if (!nodeTcpMap.ContainsKey(node.TcpEndPoint)) { continue; } if (channel.Encryption == Encryption.Aes) { BuildBuffer(channel, data, ref buffer, ref bufferSize, ref isRent, node.AesEncrypter); } ComSnowballNode snode = (ComSnowballNode)node; if (channel.Qos == QosType.Reliable) { await snode.Connection.Send(bufferSize, buffer); } else if (channel.Qos == QosType.Unreliable) { if (snode.UdpEndPoint != null) { await udpTerminal.Send(snode.Ip, snode.UdpEndPoint.Port, bufferSize, buffer); } } } if (isRent) { arrayPool.Return(buffer); } return(true); }
void OnIssueId(ComNode node, IssueIdData data) { bool registerd = false; if (data.Id != 0 && data.encryptionData != null && data.encryptionData.Length > 0) { ComNode previousNode = null; try { userNodeMapLock.AcquireReaderLock(1000); ComNode n; if (userNodeMap.TryGetValue(data.Id, out n)) { previousNode = n; } } finally { userNodeMapLock.ReleaseReaderLock(); } try { disconnectedUserNodeMapLock.AcquireWriterLock(1000); ComNode n; if (previousNode == null && disconnectedUserNodeMap.TryGetValue(data.Id, out n)) { previousNode = n; disconnectedUserNodeMap.Remove(data.Id); } } finally { disconnectedUserNodeMapLock.ReleaseWriterLock(); } if (previousNode != null && previousNode.AesDecrypter != null) { try { byte[] decrypted = previousNode.AesDecrypter.Decrypt(data.encryptionData); if (decrypted.SequenceEqual(Global.ReconnectRawData)) { node.UserId = previousNode.UserId; try { userNodeMapLock.AcquireWriterLock(1000); userNodeMap.Add(node.UserId, node); } finally { userNodeMapLock.ReleaseWriterLock(); } registerd = true; } } catch { } } } if (!registerd) { GenerateId(ref node); registerd = true; } if (!registerd) { Disconnect(node); } else { IssueIdData ldata = new IssueIdData(); ldata.Id = node.UserId; string xml = rsaDecrypter.ToPublicKeyXmlString(); ldata.PublicKey = xml; ldata.encryptionData = new byte[0]; SendInternal(node, (short)PreservedChannelId.IssueId, ldata); } }