コード例 #1
0
ファイル: Client.cs プロジェクト: akohli74/coder.net
        private async Task SendAsync(Memory <byte> packet)
        {
            try
            {
                var safeStream = Stream.Synchronized(Sender.GetStream());
                var writer     = new BinaryWriter(safeStream);
                writer.Write(packet.ToArray());

                if (!await WaitForAckAsync(safeStream))
                {
                    Logger.LogWarning($"Server has not responded with an ack.  The last message sent by client {Name} may have been lost on {IpAddress}:{Port}.");
                }
            }
            catch (ObjectDisposedException ode)
            {
                Logger.LogError(ode, $"The client {Name} socket was disposed before the Send operation completed to {IpAddress}:{Port}");
            }
            catch (OperationCanceledException)
            {
                StopToken?.Dispose();
                StopToken = new CancellationTokenSource();
                EventHub.Publish(new StartMessage(UniqueIdentifier, true));
            }
            catch (SocketException sox)
            {
                Logger.LogError(sox, $"Socket Exception on Client {Name} sending on {IpAddress}:{Port}.");
            }
            catch (Exception ex)
            {
                Logger.LogError($"Client {Name} failed to Send to host from {IpAddress}:{Port}.");
                OnError(ex);
            }
        }
コード例 #2
0
        public virtual async Task Restart()
        {
            try
            {
                Restarting = true;

                Logger.LogWarning($"Restarting Task with Id {UniqueIdentifier} and Name {Name}.");

                if (!Stopped)
                {
                    StopToken.Cancel();

                    await this.Timeout(5);

                    StopToken.Dispose();
                    StopToken = new CancellationTokenSource();
                }

                if (Stopped)
                {
                    EventHub.Publish(new StartMessage(UniqueIdentifier, true));
                }
            }
            catch (OperationCanceledException oce)
            {
                Logger.LogWarning(oce, $"Task running with Id {UniqueIdentifier} and Name {Name} is stopping.  Cannot restart a stopping Task.");
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, $"Exception while restarting Task with Id {UniqueIdentifier} and Name {Name}.");
                EventHub.Publish(new StartMessage(UniqueIdentifier, true));
            }

            Restarting = false;
        }
コード例 #3
0
ファイル: TcpServer.cs プロジェクト: akohli74/coder.net
        public override async Task <bool> Run()
        {
            try
            {
                await SpawnProcess().ConfigureAwait(false);
            }
            catch (OperationCanceledException)
            {
                Stopped = true;
                Logger.LogInformation($"Server {Name} on {IpAddress}:{Port} has shut down.  Attempting to restart...");
                StopToken?.Dispose();

                StopToken = new CancellationTokenSource();
                EventHub.Publish(new StartMessage(UniqueIdentifier, true));
                return(false);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, $"Thread running server {Name} on {IpAddress}:{Port} has crashed.  Attempting to restart it...");
                OnError(ex);
                return(false);
            }

            return(true);
        }
コード例 #4
0
ファイル: TcpServer.cs プロジェクト: akohli74/coder.net
        protected async Task <Memory <byte> > ReceiveAsync()
        {
            Logger.LogDebug($"Server {Name} received connection request from {Client?.Client?.RemoteEndPoint?.ToString()}.");

            var data = new byte[1024];

            try
            {
                if (Client != null)
                {
                    int bytes      = 0;
                    var safeStream = Stream.Synchronized(Client.GetStream());
                    var readTask   = safeStream.ReadAsync(data, 0, 1024, StopToken.Token);

                    if (ReadTimeout > 0)
                    {
                        var delayTask = Task.Delay(ReadTimeout, StopToken.Token);
                        var t         = await Task.WhenAny(readTask, delayTask);

                        if (t.Equals(readTask))
                        {
                            bytes = await readTask;
                        }
                        else
                        {
                            // if ReadTimeout is defined and exceeded, restart the server by cancelling the token
                            StopToken.Cancel();
                        }
                    }
                    else
                    {
                        bytes = await readTask;
                    }
                }
            }
            catch (ObjectDisposedException ode)
            {
                Logger.LogError(ode, $"The server {Name} socket was disposed before the Read operation completed on {IpAddress}:{Port}.");
            }
            catch (OperationCanceledException)
            {
                StopToken.Dispose();
                StopToken = new CancellationTokenSource();
                EventHub.Publish(new StartMessage(UniqueIdentifier, true));
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, $"Error while processing request from {Client?.Client?.RemoteEndPoint?.ToString()} on {Name} at {IpAddress}:{Port}.");
                OnError(ex);
            }

            return(new Memory <byte>(data));
        }
コード例 #5
0
        protected virtual void Dispose(bool disposing)
        {
            try
            {
                if (Disposed)
                {
                    return;
                }

                if (disposing)
                {
                    StopToken.Dispose();
                }

                Disposed = true;
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, $"Disposing of the Runnable Task with Id {UniqueIdentifier} and Name {Name} has caused an exception.");
                throw;
            }
        }