private void SendConnectRequest() { //Get connect key bytes byte[] keyData = Encoding.UTF8.GetBytes(_peerListener.ConnectKey); //Make initial packet var connectPacket = _packetPool.Get(PacketProperty.ConnectRequest, 12 + keyData.Length); //Add data FastBitConverter.GetBytes(connectPacket.RawData, 1, NetConstants.ProtocolId); FastBitConverter.GetBytes(connectPacket.RawData, NetConstants.RequestConnectIdIndex, _connectId); Buffer.BlockCopy(keyData, 0, connectPacket.RawData, 13, keyData.Length); //Send raw _peerListener.SendRawAndRecycle(connectPacket, _remoteEndPoint); }
private void DisconnectPeer( NetPeer peer, DisconnectReason reason, int socketErrorCode, bool sendDisconnectPacket, byte[] data, int start, int count) { if (sendDisconnectPacket) { if (count + 8 >= peer.Mtu) { //Drop additional data data = null; count = 0; NetUtils.DebugWriteError("[NM] Disconnect additional data size more than MTU - 8!"); } var disconnectPacket = _netPacketPool.Get(PacketProperty.Disconnect, 8 + count); FastBitConverter.GetBytes(disconnectPacket.RawData, 1, peer.ConnectId); if (data != null) { Buffer.BlockCopy(data, start, disconnectPacket.RawData, 9, count); } SendRawAndRecycle(disconnectPacket, peer.EndPoint); } var netEvent = CreateEvent(NetEventType.Disconnect); peer.SetDisconnectedState(); netEvent.Peer = peer; netEvent.AdditionalData = socketErrorCode; netEvent.DisconnectReason = reason; EnqueueEvent(netEvent); RemovePeer(peer.EndPoint); }
internal NetPeer(NetManager peerListener, NetEndPoint remoteEndPoint, long connectId) { Statistics = new NetStatistics(); _packetPool = peerListener.PacketPool; _peerListener = peerListener; _remoteEndPoint = remoteEndPoint; _avgRtt = 0; _rtt = 0; _pingSendTimer = 0; _reliableOrderedChannel = new ReliableChannel(this, true); _reliableUnorderedChannel = new ReliableChannel(this, false); _sequencedChannel = new SequencedChannel(this); _simpleChannel = new SimpleChannel(this); _holdedFragments = new Dictionary <ushort, IncomingFragments>(); _mergeData = _packetPool.Get(PacketProperty.Merged, NetConstants.MaxPacketSize); //if ID != 0 then we already connected _connectAttempts = 0; if (connectId == 0) { _connectId = DateTime.UtcNow.Ticks; SendConnectRequest(); } else { _connectId = connectId; _connectionState = ConnectionState.Connected; SendConnectAccept(); } NetUtils.DebugWrite(ConsoleColor.Cyan, "[CC] ConnectId: {0}", _connectId); }