public void StartListening() { int port = Server.ServerSettings.Port; if (!NetUtils.PortAvailability(port)) { ConsoleFunctions.WriteErrorLine("Port already in use... Shutting down server... [{0}]", port); Server.StopServer(); return; } ConsoleFunctions.WriteInfoLine("Starting server on port... {0}", port); _serverListener = new TcpListener(IPAddress.Any, port); if (_serverListener == null) { ConsoleFunctions.WriteErrorLine("An error occured when starting the client listener.. Null TCPListener.."); return; } _serverListener.Start(); ConsoleFunctions.WriteInfoLine("Ready & looking for client connections... "); ConsoleFunctions.WriteInfoLine("To shutdown the server safely press CTRL+C or use stop/shutdown!"); while (_serverListener.Server.IsBound) { TcpClient client = _serverListener.AcceptTcpClient(); ConsoleFunctions.WriteDebugLine("A new client has been accepted."); new Task(() => { HandleClientConnection(client); }).Start(); } }
public void SendData(byte[] data) { try { if (Encrypter != null) { var toEncrypt = data; data = new byte[toEncrypt.Length]; Encrypter.TransformBlock(toEncrypt, 0, toEncrypt.Length, data, 0); var a = TcpClient.GetStream(); a.Write(data, 0, data.Length); a.Flush(); } /* if (EncryptionEnabled) * { * AesStream aes = new AesStream(TcpClient.GetStream(), (byte[])SharedKey.Clone()); * aes.Write(data, 0, data.Length); * aes.Flush(); * }*/ else { var a = TcpClient.GetStream(); a.Write(data, 0, data.Length); a.Flush(); } } catch { ConsoleFunctions.WriteErrorLine("Failed to send a packet!"); } }
/* * TODO: Issues with this... Figure out whats causing it. */ public void Broadcast(Level level, bool self = true, Player source = null) { foreach (var player in level.GetOnlinePlayers) { try { if (player != null && player.Wrapper != null && player.Wrapper.TcpClient != null) { if (!self && player == source) { continue; } if (player.Wrapper.TcpClient.Connected) { Client = player.Wrapper; Buffer = new DataBuffer(player.Wrapper); Stream = player.Wrapper.TcpClient.GetStream(); //Exception here. (sometimes) Write(); } } else if (player != null) { player.Kick(); } } catch (Exception e) { ConsoleFunctions.WriteErrorLine("Exception thrown in Package.cs, Broadcast Function... " + e.StackTrace + " " + e.Message); //Catch any exception just to be sure the broadcast works. //TODO: Fix the exception. } } }
public static RegisteredListener RegisterListener(object obj, IPlugin plugin) { var registeredListener = new RegisteredListener(obj, plugin); if (PluginListeners.TryGetValue(plugin, out var list)) { list.Add(registeredListener); } else { PluginListeners.Add(plugin, new HashSet <RegisteredListener> { registeredListener }); } foreach (var(type, listeners) in registeredListener.Listeners) { foreach (var listener in listeners) { if (Events.TryGetValue(type, out var dict)) { dict[listener.Priority].Add(listener); } else { ConsoleFunctions.WriteErrorLine($"Type {type.Name} is not registered!"); break; } } } return(registeredListener); }
private void HandleClientCommNew(object client) { TcpClient tcpClient = (TcpClient)client; NetworkStream clientStream = tcpClient.GetStream(); ClientWrapper Client = new ClientWrapper(tcpClient); //Buffer size of 4096 Bytes, reason: I guess we don't need more? byte[] message = new byte[4096]; int bytesRead; while (true) { bytesRead = 0; try { //if (clientStream.DataAvailable) bytesRead = clientStream.Read(message, 0, 4096); if (bytesRead > 0) { ConsoleFunctions.WriteDebugLine("Packet received. Time: " + DateTime.Now.ToLocalTime()); ConsoleFunctions.WriteDebugLine("Packet ID: " + Globals.v2Int32(message, 1)[0]); PacketHandler.PacketHandler PH = new PacketHandler.PacketHandler(); Thread handler = new Thread(() => PH.HandlePacket(Client, message)); handler.Start(); } if (bytesRead == 0) { //Close connection with user. as he disconnected! break; } } catch (Exception ex) { ConsoleFunctions.WriteErrorLine("ERROR! \n" + ex.Message); break; } } ConsoleFunctions.WriteDebugLine("A client disconnected!"); if (Utils.PlayerHelper.isConnectedPlayer(Client)) { ConsoleFunctions.WriteInfoLine("Player '" + Utils.PlayerHelper.getPlayer(Client).Username + "' disconnected!"); Client._Player.SaveToFile(); Globals.Players.Remove(Utils.PlayerHelper.getPlayer(Client)); Globals.PlayerOnline--; Globals.updateTitle(); } tcpClient.Close(); Globals.ActiveConnections--; Globals.updateTitle(); }
public static void Disable(IPlugin plugin) { CheckPluginRegistration(plugin); try { plugin.Disable(); } catch (Exception ex) { ConsoleFunctions.WriteErrorLine( $"Failed to disable plugin: {plugin.GetName()} {plugin.GetVersion()} {ex}"); } Plugins[plugin] = PluginState.Disabled; }
public static void Load(IPlugin plugin) { CheckPluginRegistration(plugin); try { plugin.Load(); Plugins[plugin] = PluginState.Loaded; } catch (Exception ex) { ConsoleFunctions.WriteErrorLine( $"Failed to load plugin: {plugin.GetName()} {plugin.GetVersion()} {ex}"); Plugins[plugin] = PluginState.Failed; } }
/// <summary> /// Flush all data to the TCPClient NetworkStream. /// </summary> public void FlushData(bool quee = false) { try { var AllData = bffr.ToArray(); bffr.Clear(); if (Globals.UseCompression && _client.PacketMode == PacketMode.Play) { var mg = new MSGBuffer(_client); //ToWriteAllData var compressed = ZlibStream.CompressBuffer(AllData); mg.WriteVarInt(compressed.Length); mg.WriteVarInt(compressed.Length); mg.Write(compressed); _client.AddToQuee(mg.ExportWriter, quee); } else { WriteVarInt(AllData.Length); var Buffer = bffr.ToArray(); var data = new List <byte>(); foreach (var i in Buffer) { data.Add(i); } foreach (var i in AllData) { data.Add(i); } _client.AddToQuee(data.ToArray(), quee); } bffr.Clear(); } catch (Exception ex) { ConsoleFunctions.WriteErrorLine("Failed to send a packet!\n" + ex); } }
public static void SendResponse(ClientWrapper tcpClient, byte[] Data) { // List<byte> actData = new List<byte>(Data[0] + 1); // for (int i = 0; i < (Data[0] + 1); i++) //{ // actData.Add(Data[i]); // } try { tcpClient.Client.NoDelay = false; NetworkStream clientStream = tcpClient.Client.GetStream(); clientStream.Write(Data, 0, Data.Length); clientStream.Flush(); } catch (Exception ex) { ConsoleFunctions.WriteErrorLine("F**K, We failed to send a packet... The following error occured: " + ex.Message); } ConsoleFunctions.WriteDebugLine("Packet send with Packet ID: " + Data[1]); ConsoleFunctions.WriteDebugLine("Packet send with Packet Length: " + Data[0]); ConsoleFunctions.WriteDebugLine("Actual packet length: " + Data.Length); }
private static void UnhandledException(object sender, UnhandledExceptionEventArgs args) { var e = (Exception)args.ExceptionObject; ConsoleFunctions.WriteErrorLine("An unhandled exception occured! Error message: " + e.Message); }