/// <summary> /// Attempt to disconnect from the server /// </summary> /// <returns></returns> public bool Disconnect() { // We are not connected, so we are already disconnect :D if (!IsConnected) { return(true); } TangoMod.Log(PluginManager.MessageType.Message, "Disconnecting..."); try { _netClient.Disconnect("TANGO_DISCONNECT"); } catch { return(false); } finally { // Reconfiguration ResetConfig(); _netClient = new NetClient(_netPeerConfiguration); _isConnected = false; } return(true); }
private void ProcessMessage(object obj) { try { var netClient = (NetClient)obj; NetIncomingMessage message; TangoMod.Log(PluginManager.MessageType.Message, "Started client processing thread."); while (_isConnected) { while ((message = netClient.ReadMessage()) != null) { switch (message.MessageType) { // Debug case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.ErrorMessage: TangoMod.Log(PluginManager.MessageType.Warning, "Debug Message: " + message.ReadString()); break; // Client disconnected or connected case NetIncomingMessageType.StatusChanged: var state = (NetConnectionStatus)message.ReadByte(); if (state == NetConnectionStatus.Connected) { _isConnected = true; } else { _isConnected = false; } break; case NetIncomingMessageType.Data: var type = message.ReadInt32(); TangoMod.Log(PluginManager.MessageType.Message, "Type: " + type); break; } } } } catch (Exception e) { TangoMod.Log(PluginManager.MessageType.Error, "Client thread crashes: " + e.Message); } finally { TangoMod.Log(PluginManager.MessageType.Message, "Client thread stopped."); } }
/// <summary> /// Stop the server /// </summary> public void StopServer() { // Only shutdown server if it is // all ready running. if (!IsServerStarted) { return; } TangoMod.Log(PluginManager.MessageType.Message, "Stopping server..."); try { _netServer.Shutdown("TANGO_DISCONNECT"); } finally { IsServerStarted = false; } }
/// <summary> /// /// </summary> /// <param name="port"></param> /// <param name="password"></param> public bool StartServer(int port = 4230, string password = "") { // Server already started if (IsServerStarted) { return(true); } _port = port; TangoMod.Log(PluginManager.MessageType.Message, $"Starting server on port {_port}..."); _natPeerConfiguration = new NetPeerConfiguration("Tango") { Port = _port, AutoFlushSendQueue = false, ConnectionTimeout = 5, AcceptIncomingConnections = true }; _natPeerConfiguration.EnableMessageType(NetIncomingMessageType.ConnectionApproval); _netServer = new NetServer(_natPeerConfiguration); _netServer.Start(); if (_netServer.Status == NetPeerStatus.Running) { IsServerStarted = true; _messageProcessingThread = new Thread(_pts); _messageProcessingThread.Start(_netServer); TangoMod.Log(PluginManager.MessageType.Message, "Server started."); return(true); } TangoMod.Log(PluginManager.MessageType.Message, "Server not started..."); return(false); }
private void ProcessMessage(object obj) { try { var netServer = (NetServer)obj; NetIncomingMessage message; TangoMod.Log(PluginManager.MessageType.Message, "Started server processing thread."); while (IsServerStarted) { while ((message = netServer.ReadMessage()) != null) { switch (message.MessageType) { // Debug case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.ErrorMessage: TangoMod.Log(PluginManager.MessageType.Warning, "Debug Message: " + message.ReadString()); break; case NetIncomingMessageType.ConnectionApproval: message.SenderConnection.Approve(); break; } } } } catch (Exception e) { TangoMod.Log(PluginManager.MessageType.Error, "Server thread crashes: " + e.Message); } finally { TangoMod.Log(PluginManager.MessageType.Message, "Server thread stopped."); } }
/// <summary> /// Attempt to connect to a server, /// </summary> /// <param name="ipAddress">Server IP address</param> /// <param name="port">Port that the server is running on</param> /// <param name="username">Username to use on the server</param> /// <param name="password">The server password (if required)</param> /// <returns></returns> public ConnectionResult Connect(string ipAddress, int port, string username, string password = "") { if (IsConnected) { // Disconnect first. var disconnectResult = Disconnect(); // Could not disconnect, so we do not connect if (!disconnectResult) { TangoMod.Log(PluginManager.MessageType.Warning, "Could not disconnect old game."); return(new ConnectionResult(false, "Could not disconnect old game.")); } } _serverIp = ipAddress; _serverPort = port; _serverPassword = password; _userName = username; // First message to send to server. The server will read this info and // decide to let us connect or not. var approvalMessage = _netClient.CreateMessage(); approvalMessage.Write("TANGO_CONNECT_REQUEST"); approvalMessage.Write(_serverPassword); approvalMessage.Write(_userName); approvalMessage.Write(PluginManager.instance.enabledModCount); TangoMod.Log(PluginManager.MessageType.Message, "Client Connecting..."); try { // Start the client _netClient.Start(); // Connect to the requested server _netClient.Connect(_serverIp, _serverPort, approvalMessage); } catch (Exception e) { TangoMod.Log(PluginManager.MessageType.Warning, e.Message); return(new ConnectionResult(false, e.Message)); } // Is the client connected? if (_netClient.ConnectionStatus == NetConnectionStatus.Connected) { TangoMod.Log(PluginManager.MessageType.Message, "Client Connected"); _isConnected = true; _messageProcessingThread = new Thread(_pts); _messageProcessingThread.Start(_netClient); return(new ConnectionResult(true)); } TangoMod.Log(PluginManager.MessageType.Warning, "Could not connect to server."); return(new ConnectionResult(false, "Could not connect to server.")); }