Ejemplo n.º 1
0
        public void StartUniqueClientType <TClient>(string savePath)
            where TClient : TcpClientSavedState
        {
            server.Start();
            OnServerStarted?.Invoke(this, null);

            while (isRunning)
            {
                var client          = server.AcceptTcpClient();
                var tcpOpenedStream = (TClient)Activator.CreateInstance(typeof(TClient), client, savePath);

                connectionManager.AddTcpClient(tcpOpenedStream);
            }

            OnServerClosed?.Invoke(this, null);
        }
Ejemplo n.º 2
0
 private void ServerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
 {
     if (e.Error != null)
     {
         OnServerClosed?.Invoke(this, new ServerClosedEventArgs("An unhandled exception has occured", ServerState.Error, e.Error.InnerException));
     }
     else if (e.Cancelled)
     {
         if (!serverClosed)
         {
             OnServerClosed?.Invoke(this, new ServerClosedEventArgs("An error occured", ServerState.Error, null));
         }
     }
     else
     {
         ServerClosedResult info = e.Result as ServerClosedResult;
         OnServerClosed?.Invoke(this, new ServerClosedEventArgs(info.Message, ServerState.Done, info.InnerException));
     }
     serverClosed = true;
 }
Ejemplo n.º 3
0
        /// <summary>
        ///     Stops the pipe server thread
        /// </summary>
        public void Stop()
        {
            if (_serverThread == null)
            {
                return;
            }

            _aborted = true;
            _serverThread.Abort();

            var client = PipeClient.CreatePipeClient(_pipeName);

            client.SendMessage("", 10);
            _serverThread = null;

            if (Mutex != null)
            {
                ReleaseMutex();
                Mutex.Close();
            }

            OnServerClosed?.Invoke(this, new EventArgs());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Runs the agent
        /// </summary>
        public void Run()
        {
            try
            {
                // If we are restarting, reset
                _manualReset.Reset();

                using (WebServer server = CreateWebServer(ApplicationSettings.Default.EmbeddedServerAddress))
                {
                    server.RunAsync();

                    // Wait
                    _manualReset.WaitOne();
                }

                Trace.WriteLine(new LogMessage("EmbeddedServer - Run", "Server Stopped"), LogType.Info.ToString());
            }
            catch (Exception e)
            {
                Trace.WriteLine(new LogMessage("EmbeddedServer - Run", "Exception running server: " + e.Message), LogType.Error.ToString());
            }

            OnServerClosed?.Invoke();
        }
Ejemplo n.º 5
0
 public void CloseServer()
 {
     _server.Close();
     OnServerClosed?.Invoke();
 }
Ejemplo n.º 6
0
        public void Update()
        {
            if (mode == ServerMode.Closed)
            {
                return;
            }

            long nowTime = FDateTime.Now.TimeStamp;

            var waitCloseWatchDog = 0; //仅当服务器的所有事件处理都完成后,才可以触发OnServerClosed

            //disconnect
            while (closeQueue.TryDequeue(out Tuple <TcpConnection, ConnectionCloseType> close))
            {
                waitCloseWatchDog++;
                connections.Remove(close.Item1.ConnectionId);
                OnClientDisconnected?.Invoke(close.Item1, close.Item2);
            }
            //connect
            while (acceptQueue.TryDequeue(out TcpConnection connection))
            {
                waitCloseWatchDog++;
                connections.Add(connection.ConnectionId, connection);
                connection.Begin(nowTime);
                OnClientConnected?.Invoke(connection);

                //服务器发起关闭后还未处理的连接,经过OnClientConnected后再OnClientDisconnected
                if (mode == ServerMode.WaitClose)
                {
                    connection.Close(ConnectionCloseType.ServerClose);
                }
            }
            //receive
            while (receiveQueue.TryDequeue(out Tuple <TcpConnection, byte[]> receive))
            {
                waitCloseWatchDog++;
                //服务器发起关闭后,不再接收数据
                if (mode == ServerMode.WaitClose)
                {
                    continue;
                }
                //连接断开后,不再接收数据
                if (receive.Item1.IsShutdown)
                {
                    continue;
                }
                OnClientReceive?.Invoke(receive.Item1, receive.Item2);
            }

            //update
            foreach (var conn in connections.Values)
            {
                waitCloseWatchDog++;
                //服务器发起关闭后,首先关闭所有连接
                if (mode == ServerMode.WaitClose)
                {
                    conn.Close(ConnectionCloseType.ServerClose);
                    continue;
                }
                //应用层主动关闭连接
                if (conn.IsShutdown)
                {
                    conn.Close(ConnectionCloseType.ApplicationClose);
                    continue;
                }
                conn.Update(nowTime);
            }

            //当所有连接都妥善处理并关闭后,触发OnServerClosed
            if (mode == ServerMode.WaitClose && waitCloseWatchDog == 0)
            {
                mode = ServerMode.Closed;
                OnServerClosed?.Invoke(this);
            }
        }