public void Stop() { Socket socket = this._socket; lock (this._receivedDatas) { if (this._socket == null) { return; } this._socket = null; this._receivedDatas.Clear(); } int count = this._tokens.Count; for (int i = 0; i < count; i++) { KCPUserToken token = this._tokens[i]; this.OnSocketEvent?.Invoke(new SocketEvent(SocketEvent.Type.Disconnect, "Server stoped", SocketError.Shutdown, token)); token.Close(); this._userTokenPool.Push(token); } this._tokens.Clear(); this._idToTokens.Clear(); socket.Shutdown(SocketShutdown.Both); socket.Close(); }
public void Update(long dt) { this._updateContext.deltaTime = dt; this._updateContext.time += dt; this.ProcessReceiveDatas(); this.CheckClientOverRange(); this.CheckClientAlive(); this.UpdateClients(); int count = this._tokens.Count; for (int i = 0; i < count; i++) { KCPUserToken token = this._tokens[i]; if (token.disconnectEvent == null) { continue; } this.OnSocketEvent?.Invoke(token.disconnectEvent.Value); token.Close(); this._tokens.RemoveAt(i); this._idToTokens.Remove(token.id); this._userTokenPool.Push(token); --i; --count; } if (this._closeEvent != null) { this.OnSocketEvent?.Invoke(this._closeEvent.Value); this._closeEvent = null; this.Stop(); } }
private void ProcessReceiveDatas() { this._receivedDatas.Switch(); while (!this._receivedDatas.isEmpty) { ReceivedData receivedData = this._receivedDatas.Pop(); byte[] data = receivedData.data; int offset = 0; int size = data.Length; if (!this.VerifyConnKey(data, ref offset, ref size)) { continue; } if (this.VerifyHandshake(data, ref offset, ref size)) { KCPUserToken newToken = this._userTokenPool.Pop(this); this._tokens.Add(newToken); this._idToTokens.Add(newToken.id, newToken); newToken.OnConnected(receivedData.remoteEndPoint, TimeUtils.utcTime); this.OnSocketEvent?.Invoke(new SocketEvent(SocketEvent.Type.Accept, $"Client connection accepted, Remote Address: {receivedData.remoteEndPoint}", SocketError.Success, newToken)); byte[] handshakeAckData = new byte[NetworkConfig.SIZE_OF_CONN_KEY + NetworkConfig.SIZE_OF_SIGNATURE + NetworkConfig.SIZE_OF_PEER_ID]; int handshakeAckOffset = ByteUtils.Encode32u(handshakeAckData, 0, NetworkConfig.CONN_KEY); handshakeAckOffset += ByteUtils.Encode16u(handshakeAckData, handshakeAckOffset, NetworkConfig.HANDSHAKE_SIGNATURE); handshakeAckOffset += ByteUtils.Encode16u(handshakeAckData, handshakeAckOffset, newToken.id); newToken.SendDirect(handshakeAckData, 0, handshakeAckOffset); continue; } ushort peerId = 0; if (!this.VerifyPeerId(data, ref offset, ref size, ref peerId)) { continue; } if (!this._idToTokens.TryGetValue(peerId, out KCPUserToken token)) { continue; } token.ProcessData(data, offset, size, packet => { if (packet.module == NetworkConfig.INTERNAL_MODULE && packet.command == 0) { token.Send(new PacketHeartBeat((( PacketHeartBeat )packet).localTime)); } else { this.OnSocketEvent?.Invoke(new SocketEvent(SocketEvent.Type.Receive, packet, token)); } }); } }
private void CheckClientOverRange() { int over = this._tokens.Count - this._maxClient; for (int i = 0; i < over; i++) { KCPUserToken token = this._tokens[this._tokens.Count - 1]; token.MarkToDisconnect("Client overrange", SocketError.SocketError); } }
internal void SendTo(KCPUserToken token, byte[] data, int offset, int size, EndPoint endPoint) { if (this._socket == null) { return; } try { this._socket.SendTo(data, offset, size, SocketFlags.None, endPoint); } catch (ObjectDisposedException) { } catch (SocketException e) { token.MarkToDisconnect(e.ToString(), e.SocketErrorCode); } }
public void Push(KCPUserToken token) { this._pool.Enqueue(token); }