private static void Obj_OnDataReceived(QuicContext obj) { System.Console.WriteLine("Data received"); foreach (byte b in obj.Data) { System.Console.Write(string.Format("{0},", b)); } }
static void Main(string[] args) { QuicClient client = new QuicClient(); QuicContext context = client.Connect("127.0.0.1", 11000); // Connect to peer (Server) QuicStreamContext sc = client.CreateStream(); // Create a data stream sc.Send(Encoding.UTF8.GetBytes("Hello from Client!")); // Send Data sc.Close(); // Close the stream after processing }
public static bool AttachContext(UInt32 id, QuicContext context) { if (_pool.ContainsKey(id) == false) { return(false); } _pool[id].AttachContext(context); return(true); }
private void ProcessInitialPacket(Packet packet, IPEndPoint endPoint) { UInt32 availableConnectionId; byte[] data; // Unsupported version. Version negotiation packet is sent only on initial connection. All other packets are dropped. (5.2.2 / 16th draft) if (packet.Version != QuicVersion.CurrentVersion || !QuicVersion.SupportedVersions.Contains(packet.Version)) { VersionNegotiationPacket vnp = _packetCreator.CreateVersionNegotiationPacket(); data = vnp.Encode(); _client.Send(data, data.Length, endPoint); return; } InitialPacket cast = packet as InitialPacket; InitialPacket ip = _packetCreator.CreateInitialPacket(0, cast.SourceConnectionId); // Protocol violation if the initial packet is smaller than the PMTU. (pt. 14 / 16th draft) if (cast.Encode().Length < QuicSettings.PMTU) { ip.AttachFrame(new ConnectionCloseFrame(ErrorCode.PROTOCOL_VIOLATION, "PMTU have not been reached.")); } else if (ConnectionPool.AddConnection(cast.SourceConnectionId, out availableConnectionId) == true) { // Tell the peer the available connection id ip.SourceConnectionId = (byte)availableConnectionId; // We're including the maximum possible stream id during the connection handshake. (4.5 / 16th draft) ip.AttachFrame(new MaxStreamsFrame(QuicSettings.MaximumStreamId, StreamType.ServerBidirectional)); } else { // Not accepting connections. Send initial packet with CONNECTION_CLOSE frame. // TODO: Buffering. The server might buffer incomming 0-RTT packets in anticipation of late delivery InitialPacket. // Maximum buffer size should be set in QuicSettings. ip.AttachFrame(new ConnectionCloseFrame(ErrorCode.SERVER_BUSY, "The server is too busy to process your request.")); } data = ip.Encode(); int dataSent = _client.Send(data, data.Length, endPoint); if (dataSent > 0) { // Create a QuicContext to represent the connected client. QuicContext context = new QuicContext(_client, endPoint); ConnectionPool.AttachContext(ip.SourceConnectionId, context); OnClientConnected?.Invoke(context); } }
public QuicContext Connect(string ip, int port) { // Establish socket connection _peerIp = new IPEndPoint(IPAddress.Parse(ip), port); // Start initial protocol process InitialPacket connectionPacket = _packetCreator.CreateInitialPacket(0, 0); byte[] data = connectionPacket.Encode(); // Send the initial packet _client.Send(data, data.Length, _peerIp); // Await response for sucessfull connection creation by the server byte[] peerData = _client.Receive(ref _peerIp); if (peerData == null) { throw new QuicConnectivityException("Server did not respond properly."); } Packet packet = _unpacker.Unpack(peerData); if ((packet is InitialPacket) == false) { throw new QuicConnectivityException("Server did not respond properly."); } InitialPacket ini = (InitialPacket)packet; HandleInitialFrames(packet); EstablishConnection(ini.SourceConnectionId, ini.SourceConnectionId); // Create the QuicContext QuicContext context = new QuicContext(_client, _peerIp); // Cross reference with Connection _connection.AttachContext(context); return(context); }
public void AttachContext(QuicContext context) { Context = context; Context.Connection = this; }
private static void Listener_OnClientConnected(QuicContext obj) { System.Console.WriteLine("Client connected."); obj.OnDataReceived += Obj_OnDataReceived; }
public void AttachContext(QuicContext context) { Context = context; }