/// <summary> /// Shutdowns this instance. /// </summary> public override void Shutdown() { base.Shutdown(); ConsoleLogger.WriteToLog("Shutting Server down..."); IsRunning = false; TcpClientConnected.Set(); if (ListenThread != null) { ListenThread.Join(); } while (NumClientThreads > 0) { Clients.Last().Value.ThisThread.Join(); } Clients.Clear(); if (Server != null) { Server.Stop(); } ConsoleLogger.WriteToLog("Server successfully Shutdown."); }
public virtual void OnTcpClientConnect(IAsyncResult asyn) { TcpClientConnected?.Invoke(this, new TWSTcpClientConnectedEventArgs(this, null)); try { // Here we complete/end the BeginAccept() asynchronous call // by calling EndAccept() - which returns the reference to // a new Socket object var tc = _listener.EndAcceptTcpClient(asyn); _clientCount++; //var s = new BufferedReadStream(tc.GetStream(), DEFAULT_BUFFER_SIZE); var s = tc.GetStream(); var connection = new TWSServerClientHandler(this, s); lock (_clients) { _clients.Add(connection); } connection.Start(); // Since the main Socket is now free, it can go back and wait for // other clients who are attempting to connect _listener.BeginAcceptTcpClient(_connectCallback, null); } catch (ObjectDisposedException) { Debugger.Log(0, "1", "\n OnClientConnection: Socket has been closed\n"); } catch (SocketException se) { OnError(new TWSError(TWSErrors.NO_VALID_CODE, se.Message)); } }
/// <summary> /// Events the TCP client accept. /// </summary> /// <param name="ar">The async result.</param> private void EventTcpClientAccept(IAsyncResult ar) { try { TcpListener listener = (TcpListener)ar.AsyncState; TcpClient newClient = listener.EndAcceptTcpClient(ar); newClient.ReceiveTimeout = 0; Thread t = new Thread(new ParameterizedThreadStart(HandleClient)); t.IsBackground = true; int uniqueID = CreateUniqueClientID(); ClientConnection sC = new ClientConnection(); sC.ThisID = uniqueID; sC.ThisClient = newClient; sC.ThisThread = t; switch (ConnectionType) { case EConnectionType.SERVER: sC.ConnectionType = EConnectionType.CLIENT; break; } Clients.Add(uniqueID, sC); Clients[uniqueID].ThisThread.Start(Clients[uniqueID]); ConsoleLogger.WriteToLog("Client ID: " + uniqueID + " Connected.", true); PacketDesc_SetClientID sidpkg = new PacketDesc_SetClientID(); sidpkg.PacketTarget = sC.ConnectionType; sidpkg.Id = uniqueID; sidpkg.SenderID = UniqueID; sidpkg.PacketOriginClientID = UniqueID; SendPacketToClient(sidpkg, uniqueID); PacketDesc_ClientConnected copkg = new PacketDesc_ClientConnected(); copkg.PacketTarget = sC.ConnectionType; copkg.OtherClientID = uniqueID; copkg.PacketOriginClientID = UniqueID; SendPacketToAllClients(copkg, uniqueID); UpdateTitle(); TcpClientConnected.Set(); if (OnClientAccepted != null) { OnClientAccepted(uniqueID); } } catch (ObjectDisposedException) { return; } }
/// <summary> /// Listens for clients. /// </summary> private void Listen() { ConsoleLogger.WriteToLog("Listening at Port: " + Port); while (IsRunning) { TcpClientConnected.Reset(); ConsoleLogger.WriteToLog("Waiting for Client Connections..."); Server.BeginAcceptTcpClient(new AsyncCallback(EventTcpClientAccept), Server); TcpClientConnected.WaitOne(); if (!IsRunning) { break; } } ConsoleLogger.WriteToLog("Stopped Listening for Clients."); }
/// <summary> /// Starts this instance. /// </summary> public void Start(IPAddress listenAddress) { ConsoleLogger.WriteToLog("Starting Server...", true); while (!IsRunning) { try { Server = new TcpListener(listenAddress, Port); Server.Start(); IsRunning = true; } catch (SocketException) { Port++; } } ListenThread = new Thread(Listen); ListenThread.IsBackground = true; ListenThread.Start(); ConsoleLogger.WriteToLog("Server successfully Started...", true); TcpClientConnected.WaitOne(200); Thread console = new Thread(new ThreadStart(RunConsoleMenu)); console.IsBackground = true; console.Start(); Thread title = new Thread(new ThreadStart(UpdateTitle)); title.IsBackground = true; title.Start(); }