Changeset 499 for TFP-WebServer


Ignore:
Timestamp:
Jul 23, 2024, 6:49:56 PM (5 months ago)
Author:
alloc
Message:

*Fixed: Chat code
*Fixed: SSE connection counting, added connection set up logging

Location:
TFP-WebServer
Files:
18 edited

Legend:

Unmodified
Added
Removed
  • TFP-WebServer/CommandExtensions/ModInfo.xml

    r487 r499  
    55        <Description value="Additional commands for server operation" />
    66        <Author value="The Fun Pimps LLC" />
    7         <Version value="1.1.0.0" />
     7        <Version value="1.1.0.1" />
    88        <Website value="" />
    99</xml>
  • TFP-WebServer/CommandExtensions/src/ChatHelpers.cs

    r485 r499  
    1010                        }
    1111
    12                         _receiver.SendPackage (NetPackageManager.GetPackage<NetPackageChat> ().Setup (EChatType.Whisper, -1, _message,
    13                                 $"{senderName} (PM)", null));
     12                        _receiver.SendPackage (NetPackageManager.GetPackage<NetPackageChat> ().Setup (EChatType.Whisper, -1,
     13                                $"{senderName} (PM): {_message}", null,
     14                                EMessageSender.None));
    1415                        string receiverName = _receiver.playerName;
    1516                        receiverName = receiverName != null ? $"\"{receiverName}\"" : "unknownName";
  • TFP-WebServer/CommandExtensions/src/Commands/Give.cs

    r487 r499  
    5151                        }
    5252
    53                         int quality = Constants.cItemMaxQuality;
     53                        ushort quality = Constants.cItemMaxQuality;
    5454
    5555                        if (_params.Count == 4) {
    56                                 if (!int.TryParse (_params [3], out quality) || quality <= 0) {
     56                                if (!ushort.TryParse (_params [3], out quality) || quality > Constants.cItemMaxQuality) {
    5757                                        SdtdConsole.Instance.Output ("Quality is not an integer or not greater than zero.");
    5858                                        return;
  • TFP-WebServer/WebServer/ModInfo.xml

    r490 r499  
    55        <Description value="Integrated Webserver for the Web Dashboard and server APIs" />
    66        <Author value="The Fun Pimps LLC" />
    7         <Version value="1.1.0.2" />
     7        <Version value="1.1.0.3" />
    88        <Website value="" />
    99</xml>
  • TFP-WebServer/WebServer/src/SSE/AbsEvent.cs

    r487 r499  
    3636
    3737                        openClients.Add (_client);
     38                       
     39                        logConnectionState ("Connection opened", _client);
    3840                }
    3941
     
    6466                                                bytesToSend = Encoding.UTF8.GetBytes (output, 0, output.Length, buf, 0);
    6567                                        } catch (ArgumentException e) {
    66                                                 logError ("Exception while encoding data for output, most likely exceeding buffer size:", false);
     68                                                Log.Error ($"[Web] [SSE] '{Name}': Exception while encoding data for output, most likely exceeding buffer size:");
    6769                                                Log.Exception (e);
    6870                                                return;
     
    7981                private void sendBufToListeners (byte[] _bytes, int _bytesToSend) {
    8082                        for (int i = openClients.Count - 1; i >= 0; i--) {
    81                                 ESseClientWriteResult writeResult = openClients [i].Write (_bytes, _bytesToSend);
    82                                 if (writeResult == ESseClientWriteResult.Ok) {
    83                                         continue;
    84                                 }
    85 
    86                                 currentlyOpen--;
    87                                 totalClosed++;
    88 
    89                                 if (writeResult == ESseClientWriteResult.Error) {
    90                                         logError ("Can not write to endpoint, closing", true);
    91                                 }
     83                                openClients [i].Write (_bytes, _bytesToSend);
    9284                        }
    93                 }
    94 
    95                 protected void logError (string _message, bool _printConnections) {
    96                         Log.Error (_printConnections
    97                                 ? $"[Web] [SSE] '{Name}': {_message} (Left open: {currentlyOpen}, total opened: {totalOpened}, closed: {totalClosed})"
    98                                 : $"[Web] [SSE] '{Name}': {_message}");
    9985                }
    10086
    10187                public virtual int DefaultPermissionLevel () => 0;
    10288
     89                private void logConnectionState (string _message, SseClient _client) {
     90                        Log.Out ($"[Web] [SSE] '{Name}': {_message} from {_client.RemoteEndpoint} (Left open: {currentlyOpen}, total opened: {totalOpened}, closed: {totalClosed})");
     91                }
     92
    10393                public void ClientClosed (SseClient _client) {
    104                         openClients.Remove (_client);
     94                        if (openClients.Remove (_client)) {
     95                                currentlyOpen--;
     96                                totalClosed++;
     97
     98                                logConnectionState ("Closed connection", _client);
     99                        }
    105100                }
    106101        }
  • TFP-WebServer/WebServer/src/SSE/SseClient.cs

    r487 r499  
    11using System;
    22using System.IO;
     3using System.Net;
    34using System.Net.Sockets;
    45using System.Text;
    5 using SpaceWizards.HttpListener;
    66using Webserver.UrlHandlers;
     7using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;
    78
    89namespace Webserver.SSE {
     
    1718        private static readonly byte[] keepAliveData = Encoding.UTF8.GetBytes (": KeepAlive\n\n");
    1819
     20        public readonly IPEndPoint RemoteEndpoint;
    1921        private readonly SseHandler parent;
    2022        private readonly HttpListenerResponse response;
    2123        private DateTime lastMessageSent = DateTime.Now;
    2224
    23         public SseClient (SseHandler _parent, HttpListenerResponse _response) {
     25        public SseClient (SseHandler _parent, RequestContext _context) {
    2426            parent = _parent;
    25             response = _response;
     27            response = _context.Response;
     28            RemoteEndpoint = _context.Request.RemoteEndPoint;
    2629           
    2730            // Keep the request open
    28             _response.SendChunked = true;
     31            response.SendChunked = true;
    2932
    30             _response.AddHeader ("Content-Type", "text/event-stream");
    31             _response.OutputStream.Flush ();
     33            response.AddHeader ("Content-Type", "text/event-stream");
     34            response.OutputStream.Flush ();
    3235        }
    3336
  • TFP-WebServer/WebServer/src/UrlHandlers/SseHandler.cs

    r487 r499  
    6868                        SseClient client;
    6969                        try {
    70                                 client = new SseClient(this, _context.Response);
     70                                client = new SseClient(this, _context);
    7171                        } catch (Exception e) {
    7272                                Log.Error ($"[Web] [SSE] In {nameof (SseHandler)}.HandleRequest(): Could not create client:");
  • TFP-WebServer/bin/Mods/TFP_CommandExtensions/ModInfo.xml

    r487 r499  
    55        <Description value="Additional commands for server operation" />
    66        <Author value="The Fun Pimps LLC" />
    7         <Version value="1.1.0.0" />
     7        <Version value="1.1.0.1" />
    88        <Website value="" />
    99</xml>
  • TFP-WebServer/bin/Mods/TFP_WebServer/ModInfo.xml

    r490 r499  
    55        <Description value="Integrated Webserver for the Web Dashboard and server APIs" />
    66        <Author value="The Fun Pimps LLC" />
    7         <Version value="1.1.0.2" />
     7        <Version value="1.1.0.3" />
    88        <Website value="" />
    99</xml>
Note: See TracChangeset for help on using the changeset viewer.