public QuicStream OpenStream() { QuicStream stream = null; while (stream == null /* TODO: Or Timeout? */) { Packet packet = _pwt.ReadPacket(); if (packet is ShortHeaderPacket shp) { stream = ProcessFrames(shp.GetFrames()); } } return(stream); }
/// <summary> /// Blocks and waits for incomming connection. Does NOT block additional incomming packets. /// </summary> /// <returns>Returns an instance of QuicConnection.</returns> public QuicConnection AcceptQuicClient() { if (!_started) { throw new QuicListenerNotStartedException("Please call the Start() method before receving data."); } /* * Wait until there is initial packet incomming. * Otherwise we still need to orchestrate any other protocol or data pakcets. * */ while (true) { Packet packet = _pwt.ReadPacket(); if (packet is InitialPacket) { QuicConnection connection = ProcessInitialPacket(packet, _pwt.LastTransferEndpoint()); return(connection); } OrchestratePacket(packet); } }
/// <summary> /// Connect to a remote server. /// </summary> /// <param name="ip">Ip Address</param> /// <param name="port">Port</param> /// <returns></returns> public QuicConnection Connect(string ip, int port) { // Establish socket connection _peerIp = new IPEndPoint(IPAddress.Parse(ip), port); // Initialize packet reader _pwt = new PacketWireTransfer(_client, _peerIp); // Start initial protocol process InitialPacket connectionPacket = _packetCreator.CreateInitialPacket(0, 0); // Send the initial packet _pwt.SendPacket(connectionPacket); // Await response for sucessfull connection creation by the server InitialPacket packet = (InitialPacket)_pwt.ReadPacket(); HandleInitialFrames(packet); EstablishConnection(packet.SourceConnectionId, packet.SourceConnectionId); return(_connection); }
/// <summary> /// Starts the listener. /// </summary> public void Start() { _client = new UdpClient(_port); _started = true; _pwt = new PacketWireTransfer(_client, null); while (true) { Packet packet = _pwt.ReadPacket(); if (packet is InitialPacket) { QuicConnection connection = ProcessInitialPacket(packet, _pwt.LastTransferEndpoint()); OnClientConnected?.Invoke(connection); } if (packet is ShortHeaderPacket) { ProcessShortHeaderPacket(packet); } } }
/// <summary> /// Client only! /// </summary> /// <returns></returns> internal void ReceivePacket() { var packet = _pwt.ReadPacket(); if (packet is ShortHeaderPacket) { var shp = (ShortHeaderPacket)packet; ProcessFrames(shp.GetFrames()); } // If the connection has been closed if (_state == ConnectionState.Draining) { if (string.IsNullOrWhiteSpace(_lastError)) { _lastError = "Protocol error"; } TerminateConnection(); throw new QuicConnectivityException(_lastError); } }