internal static async Task <Dataframe?> CreateDataframe(TcpConnectionService tcpConnection, CancellationToken ct) { var dataframe = new Dataframe(tcpConnection, ct); var oneByteArray = await dataframe.GetNextBytes(1); Debug.WriteLine($"First byte: {oneByteArray?[0]}"); if (oneByteArray is null) { return(null); } var bits = new BitArray(oneByteArray); return(dataframe with { FIN = bits[7], RSV1 = bits[6], RSV2 = bits[5], RSV3 = bits[4], Opcode = GetOpcode(), Fragment = oneByteArray[0] switch { (byte)FragmentKind.First => FragmentKind.First, (byte)FragmentKind.Last => FragmentKind.Last, _ => FragmentKind.None } });
public static TcpConnectionService BuildService(SystemEntity entity) { var service = new TcpConnectionService(); service.IpAddress = entity.IP; service.Port = int.Parse(entity.Port); return(service); }
static void Main(string[] args) { Console.WriteLine("Server starts--"); DatabaseContext db = new DatabaseContext(); IClientRoomRepository clientRoomRepository = new ClientRoomRepository(db); ISocketRepository socketRepository = new SocketRepository(db); var tcp = new TcpConnectionService(); Thread thread = new Thread(tcp.Connect); thread.Start(); }
public static void UpdateDeviceData(ref SystemEntity entity, TcpConnectionService service) { if (!string.IsNullOrWhiteSpace(service.IpAddress)) { entity.IP = service.IpAddress; } if (service.Port != 0) { entity.Port = service.Port.ToString(); } }
internal static async Task <WebsocketService> Create( Func <bool> isSecureConnectionSchemeFunc, Func <object, X509Certificate, X509Chain, SslPolicyErrors, bool> validateServerCertificateFunc, EventLoopScheduler eventLoopScheduler, IObserver <ConnectionStatus> observerConnectionStatus, MessageWebsocketRx messageWebSocketRx) { var tcpConnectionHandler = new TcpConnectionService( isSecureConnectionSchemeFunc: isSecureConnectionSchemeFunc, validateServerCertificateFunc: validateServerCertificateFunc, connectTcpClientFunc: ConnectTcpClient, ReadOneByteFromStream, //readOneByteFunc: (stream, bytes, cts) => RunOnScheduler(ReadOneByteFromStream(stream, bytes, cts), eventLoopScheduler), connectionStatusAction: ConnectionStatusAction, messageWebSocketRx.HasTransferSocketLifeCycleOwnership, tcpClient: messageWebSocketRx.TcpClient); var websocketServices = new WebsocketService( new WebsocketConnectionHandler( tcpConnectionHandler, new WebsocketParserHandler( tcpConnectionHandler), ConnectionStatusAction, (stream, connectionStatusAction) => new WebsocketSenderHandler( tcpConnectionHandler, ConnectionStatusAction, //WriteToStream (stream, bytes, cts) => RunOnScheduler(WriteToStream(stream, bytes, cts), eventLoopScheduler), messageWebSocketRx.ExcludeZeroApplicationDataInPong ) ) ); await Task.CompletedTask; return(websocketServices); void ConnectionStatusAction(ConnectionStatus status, Exception?ex) { if (status is ConnectionStatus.Disconnected) { observerConnectionStatus.OnCompleted(); } if (status is ConnectionStatus.Aborted) { observerConnectionStatus.OnError( ex ?? new WebsocketClientLiteException("Unknown error.")); } observerConnectionStatus.OnNext(status); } async Task <bool> WriteToStream(Stream stream, byte[] byteArray, CancellationToken ct) { await stream.WriteAsync(byteArray, 0, byteArray.Length, ct).ConfigureAwait(false); await stream.FlushAsync().ConfigureAwait(false); return(true); } async Task <int> ReadOneByteFromStream(Stream stream, byte[] byteArray, CancellationToken ct) { try { return(await stream.ReadAsync(byteArray, 0, byteArray.Length, ct).ConfigureAwait(false)); } catch (OperationCanceledException) { return(-1); } } async Task ConnectTcpClient(TcpClient tcpClient, Uri uri) => await tcpClient .ConnectAsync(uri.Host, uri.Port) .ConfigureAwait(false); // Running sends and/or writes on the Event Loop Scheduler serializes them. async Task <T> RunOnScheduler <T>(Task <T> task, IScheduler scheduler) => await task.ToObservable().ObserveOn(scheduler); }