public virtual TConnection Generate(Tcp.Connection connection, URL url, params string[] reqSubprotocols) { ClientConnection httpClient = new ClientConnection(connection); OutgoingRequest req = httpClient.Enqueue("HTTP/1.1", "GET", url); TConnection newConnection = GenerateConnection(connection, req.Head); string reqExtensions = NegotiatingExtension.Stringify(RequestExtensions()); string key = Secret.GenerateKey(); req["Connection"] = "Upgrade"; req["Upgrade"] = "websocket"; req["Sec-WebSocket-Version"] = "13"; req["Sec-WebSocket-Key"] = key; if (reqSubprotocols.Length > 0) { req["Sec-WebSocket-Protocol"] = SubprotocolNegotiation.Stringify(reqSubprotocols); } if (reqExtensions.Length > 0) { req["Sec-WebSocket-Extensions"] = reqExtensions; } req.OnResponse += (res) => { if (!VerifyResponseHead(res, key)) { httpClient.Terminate(); return; } string subprotocol = res["Sec-WebSocket-Protocol"]; if (subprotocol != null) { bool validSubprotocol = false; for (int i = 0; i < reqSubprotocols.Length; i++) { if (reqSubprotocols[i] == subprotocol) { validSubprotocol = true; break; } } if (!validSubprotocol) { httpClient.Terminate(); return; } } newConnection.SetSubprotocol(subprotocol); if ( !NegotiatingExtension.TryParse(res["Sec-WebSocket-Extensions"] ?? "", out NegotiatingExtension[] resExtensions) || !CheckExtensions(resExtensions) ) { httpClient.Terminate(); return; } byte[] trail = httpClient.Freeze(); if (!httpClient.End()) { return; } newConnection.Initiate(trail); };
public Connection(Tcp.Connection connection, IMode mode) { Mode = mode; Base = connection; Base.OnClose += FinishClose; Parser.OnCollect += OnParserCollect; Merger.OnCollect += OnMergerCollect; }
protected override void FireConnection(Tcp.Connection connection, RequestHead req, string subprotocol, byte[] trail) { Connection newConnection = new Connection(connection, req, new Definition.Connection.ServerMode()); newConnection.SetSubprotocol(subprotocol); OnConnection?.Invoke(newConnection); newConnection.Initiate(trail); }
public static void TcpSocketScalabilityTest(string[] args) { EndPoint clientEndPoint = new IPEndPoint(IPAddress.Loopback, 420); EndPoint serverendPoint = new IPEndPoint(IPAddress.Any, 420); byte[] sending = new byte[] { 1, 2, 3, 4, 5 }; TcpSocketScalabilityMetrics metrics = new TcpSocketScalabilityMetrics { attempts = 10000 }; TcpListener listener = new TcpListener { Backlog = metrics.attempts, BindEndPoint = serverendPoint }; listener.OnConnection += (server) => { Interlocked.Increment(ref metrics.serverActive); server.TimeoutAfter = new TimeSpan(0, 1, 0); server.OnData += (data) => Interlocked.Increment(ref metrics.datasReceieved); server.OnError += (e) => { Interlocked.Increment(ref metrics.serverSuccessful); Interlocked.Increment(ref metrics.serverError); }; server.OnClose += () => { Interlocked.Increment(ref metrics.serverSuccessful); Interlocked.Decrement(ref metrics.serverActive); }; server.OnTimeout += () => { Interlocked.Increment(ref metrics.serverTimeout); server.Terminate(); }; }; listener.Start(); Thread.Sleep(1000); DateTime start = DateTime.UtcNow; List <Tcp.Connection> clients = new List <Tcp.Connection>(); Console.WriteLine("CLIENT: created / active / success / error / total SERVER: active / timeout / success / error / total DATA: sent / recvd IO: connections / listeners / sockets / threads"); for (int i = 0; i < metrics.attempts; i++) { Interlocked.Increment(ref metrics.clientCreated); Tcp.Connection client = new Tcp.Connection(); client.OnDrain += () => Interlocked.Increment(ref metrics.datasSent); client.OnClose += () => { Interlocked.Increment(ref metrics.clientSuccessful); Interlocked.Decrement(ref metrics.clientActive); }; client.OnError += (e) => { if (client.State != TcpSocketState.Open) { Interlocked.Increment(ref metrics.clientActive); } Interlocked.Decrement(ref metrics.clientSuccessful); Interlocked.Increment(ref metrics.clientError); }; client.OnOpen += () => { Interlocked.Increment(ref metrics.clientActive); client.Write(sending); client.End(); }; clients.Add(client); if (metrics.clientCreated % 100 == 0) { Console.WriteLine("[{0}] {1}", (DateTime.UtcNow - start).TotalSeconds.ToString("F6").PadLeft(12), metrics.ToString()); } } Console.WriteLine("[{0}] {1} clients created", (DateTime.UtcNow - start).TotalSeconds.ToString("F6").PadLeft(12), metrics.attempts); for (int i = 0; i < metrics.attempts; i++) { clients[i].Connect(clientEndPoint); } Console.WriteLine("[{0}] {1} clients connecting", (DateTime.UtcNow - start).TotalSeconds.ToString("F6").PadLeft(12), metrics.attempts); Thread.Sleep(100); while (true) { Console.WriteLine("[{0}] {1}", (DateTime.UtcNow - start).TotalSeconds.ToString("F6").PadLeft(12), metrics.ToString()); if (IOControl.ConnectionCount == 0) { break; } Thread.Sleep(200); } listener.Stop(); Console.WriteLine("[{0}] done", (DateTime.UtcNow - start).TotalSeconds.ToString("F6").PadLeft(12)); Console.ReadKey(); }
private Task OpenConnection(IPAddress ip, int port) { Tcp.Connection connection = new Tcp.Connection(); return connection.AsyncOpen(ip, port).ContinueWith(t => { if (t.IsFaulted) { MessageBox.Show("Die Verbindung konnte nicht aufgebaut werden: " + t.Exception.MostInner().Message); throw t.Exception; return; } }, TaskScheduler.FromCurrentSynchronizationContext()); }
protected override Connection GenerateConnection(Tcp.Connection connection, RequestHead req) => new Connection(connection, req, new Definition.Connection.ClientMode());
internal Transciver(Tcp.Connection @base) { _base = @base; }
public Connection(Tcp.Connection connection, RequestHead req, IMode mode) : base(connection, mode) => Req = req;
public PacketTranslator(Tcp.Connection @base) { _base = @base; InitRxTools(); }
internal void Dispose() { if (IsDisposed) return; IsDisposed = true; OnRxBeginn = null; OnRxCompleted = null; _rxTimeout.Stop(); _rxTimeout.Dispose(); _cRx = null; _rxTempBytes.Clear(); _rxTempBytes = null; _base = null; }