public async Task ProcessRecControl(TcpCSSocketControl control) { byte newControl = 0; bool close = false; if (!Accepted) { if (control.CheckFlag(TcpCSSocketControl.AcceptConnection)) { Accepted = true; newControl |= TcpCSSocketControl.Ready; } else if (control.CheckFlag(TcpCSSocketControl.RefuseConnection)) { newControl |= TcpCSSocketControl.Close; close = true; } } if (control.CheckFlag(TcpCSSocketControl.Close)) { newControl |= TcpCSSocketControl.Close; close = true; } await Connection.Stream.WriteAsync((new TcpCSSocketControl(SocketId, newControl)).Build()); if (close) { Connections.Remove(IdToString(SocketId)); } }
public static async Task StandardSocketControlHandler(Packet packet) { var pa = new TcpCSSocketControl(packet); var ep = ((EntryPoint)packet.Connection.Ref); Log.Debug("Socket Control Detail for socket {Socket}: [{Flags}] {Extra}", pa.SocketId, pa.ReadableFlags, pa.InstanceId); AppConnection x = null; ep.Connections.TryGetValue(AppConnection.IdToString(pa.SocketId), out x); StoredApp app = null; if (pa.CheckFlag(TcpCSSocketControl.OpenRequest)) { if (x == null) { x = new AppConnection(pa.InstanceId, ep); x.SocketId = pa.SocketId; } else { x.Loopback = true; Log.Debug("Connection {Sid} switched to loopback mode", pa.SocketId); } app = ep._registeredApps.FirstOrDefault(e => e.InstanceId.SequenceEqual(x.InstanceId)); ep.Connections.Add(AppConnection.IdToString(pa.SocketId), x); await app.Callback(x); } if (x == null) { return; } if (pa.CheckFlag(TcpCSSocketControl.Ready)) { x.Open = true; x.ConnectCompletionSource.SetResult(true); } if (pa.CheckFlag(TcpCSSocketControl.Close)) { if (!x.Open) { x.ConnectCompletionSource.SetResult(false); } _ = Task.Run(async() => { await Task.Delay(500); await x.Close(); }); } }
/// <summary> /// Initiates the connection to the app /// </summary> /// <returns>An async Task</returns> /// <exception cref="Exception">Thrown if the connection is refused</exception> public async Task Connect() { if (SocketId != null) { return; } ConnectCompletionSource = new TaskCompletionSource <bool>(); uint nonce = Packet.WatchNonce(async pk => { await Task.Run(() => { TcpCSSocketControl control = new TcpCSSocketControl(pk); SocketId = control.SocketId; EP.Connections.Add(IdToString(control.SocketId), this); Requestor = true; Stream = new NRStream(this); if (control.CheckFlag(TcpCSSocketControl.Close)) { ConnectCompletionSource.SetResult(false); } }); }); EP.TransmitRaw(new TcpCOpenSocket(InstanceId, nonce)); bool success = await ConnectCompletionSource.Task; if (!success) { await Close(false); throw new Exception("Connect to distant end was refused."); } }
public async Task ProcessReqControl(TcpCSSocketControl control) { byte newControl = 0; if (control.CheckFlag(TcpCSSocketControl.Close)) { newControl |= TcpCSSocketControl.Close; Connections.Remove(IdToString(SocketId)); } await App.Connection.Stream.WriteAsync((new TcpCSSocketControl(SocketId, newControl)).Build()); }