Skip to content

Commit

Permalink
fix: too many files open error
Browse files Browse the repository at this point in the history
  • Loading branch information
TGRHavoc committed May 17, 2019
1 parent 0974a65 commit 345e266
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 17 deletions.
45 changes: 45 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -2,9 +2,54 @@ Changelog
=========


v2.2.7 (22-11-2017)
-------------------
- Possibly fixed "Too many files open" error. [Jordan Dalton]


v2.2.6 (21-11-2017)
-------------------

Changes
~~~~~~~
- Update to v2.2.6. [Jordan Dalton]

Other
~~~~~
- Potentially fixed too many connections crashing server. [Jordan
Dalton]

Hoopefully this fixes the issue where the server crashes when too many people join.


v2.2.5 (17-11-2017)
-------------------

New
~~~
- Add access-control to sockets. [Jordan Dalton]

If sockets don't come from the domain set in the CFG file, they get rejected. I don't know why I didn't implement this sooner but, it's here now :)

Changes
~~~~~~~
- Update version and changelog. [Jordan Dalton]
- Update socket library. [Jordan Dalton]

Socket library is at version 4.1.3.0 now

Other
~~~~~
- Merge branch 'develop' [Jordan Dalton]


v2.2.4 (03-11-2017)
-------------------

Changes
~~~~~~~
- Update changelog. [Jordan Dalton]

Fix
~~~
- Fixed #21. [Jordan Dalton]
Expand Down
Binary file modified deniszykov.WebSocketListener.dll
Binary file not shown.
10 changes: 7 additions & 3 deletions src/Live Map/Live Map.csproj
Expand Up @@ -42,8 +42,8 @@
<Reference Include="CitizenFX.Core">
<HintPath>lib\CitizenFX.Core.dll</HintPath>
</Reference>
<Reference Include="deniszykov.WebSocketListener, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7f78616efb4a208d, processorArchitecture=MSIL">
<HintPath>..\packages\deniszykov.WebSocketListener.4.0.0\lib\net45\deniszykov.WebSocketListener.dll</HintPath>
<Reference Include="deniszykov.WebSocketListener, Version=4.1.3.0, Culture=neutral, PublicKeyToken=7f78616efb4a208d, processorArchitecture=MSIL">
<HintPath>..\packages\deniszykov.WebSocketListener.4.1.3\lib\net45\deniszykov.WebSocketListener.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
Expand All @@ -52,6 +52,10 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Threading.Tasks.Dataflow, Version=4.5.24.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Tpl.Dataflow.4.5.24\lib\portable-net45+win8+wpa81\System.Threading.Tasks.Dataflow.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down Expand Up @@ -82,7 +86,7 @@
</PropertyGroup>
<PropertyGroup>
<PostBuildEvent>
</PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
5 changes: 5 additions & 0 deletions src/Live Map/LiveMap.cs
Expand Up @@ -31,6 +31,9 @@ public class LiveMap : BaseScript
// The current debug level (from the "livemap_debug" convar)
public static int debugLevel = 0;

// Access-origin
public static string accessOrigin = "*";

public static int waitSeconds = 500;

public enum LogLevel
Expand Down Expand Up @@ -65,6 +68,8 @@ public LiveMap()

waitSeconds = API.GetConvarInt("livemap_milliseconds", 500);

accessOrigin = API.GetConvar("livemap_access_control", "*");

Log(LogLevel.Basic, "Starting on port {0}", port);

server = new WebSocketServer(port);
Expand Down
36 changes: 27 additions & 9 deletions src/Live Map/SocketHandler.cs
Expand Up @@ -53,7 +53,7 @@ public SocketHandler(WebSocketServer server)
private void Server_OnMessage(WebSocket ws, string msg)
{
string[] args = msg.Split(' ');
LiveMap.Log(LiveMap.LogLevel.All, "Recieved message from client {0}:\n\t\"{1}\"", ws.RemoteEndpoint.ToString(), msg);
LiveMap.Log(LiveMap.LogLevel.Basic, "Recieved message from client {0}:\n\t\"{1}\"", ws.RemoteEndpoint.ToString(), msg);
}

private void Server_OnError(WebSocket ws, Exception ex)
Expand All @@ -72,9 +72,7 @@ private void Server_OnError(WebSocket ws, Exception ex)
{
LiveMap.Log(LiveMap.LogLevel.Basic, "Couldn't remove {0} from the clients dic.", ws.RemoteEndpoint.ToString());
}

}

}

private void Server_OnDisconnect(WebSocket ws)
Expand Down Expand Up @@ -127,6 +125,7 @@ public async Task SendWebsocketData()
await Task.Delay(TimeSpan.FromMilliseconds(LiveMap.waitSeconds)).ConfigureAwait(false);

//LiveMap.Log(LiveMap.LogLevel.All, "Checking send queue");
List<string> disconnectedClients = new List<string>();
if (sendQueue.Count != 0)
{
JObject payload;
Expand All @@ -138,12 +137,16 @@ public async Task SendWebsocketData()
string endpoint = pair.Key;
WebSocket ws = pair.Value;

LiveMap.Log(LiveMap.LogLevel.All, "Sending payload of \"{0}\" to {1}", payload["type"], endpoint);
if (!ws.IsConnected)
{
disconnectedClients.Add(endpoint);
continue;
}

await ws.WriteStringAsync(payload.ToString(Newtonsoft.Json.Formatting.None)).ConfigureAwait(false);
LiveMap.Log(LiveMap.LogLevel.All, "Sending payload of \"{0}\" to {1}", payload["type"], endpoint);

await ws.WriteStringAsync(payload.ToString(Newtonsoft.Json.Formatting.None), CancellationToken.None).ConfigureAwait(false);
}

}
else
{
Expand Down Expand Up @@ -179,13 +182,28 @@ public async Task SendWebsocketData()
string endpoint = pair.Key;
WebSocket ws = pair.Value;

//LiveMap.Log(LiveMap.LogLevel.All, "Sending payload of \"{0}\" to {1}", payload["type"], endpoint);
if (!ws.IsConnected)
{
disconnectedClients.Add(endpoint);
continue;
}

await ws.WriteStringAsync(payload.ToString(Newtonsoft.Json.Formatting.None)).ConfigureAwait(false);
//LiveMap.Log(LiveMap.LogLevel.All, "Sending payload of \"{0}\" to {1}", payload["type"], endpoint);
await ws.WriteStringAsync(payload.ToString(Newtonsoft.Json.Formatting.None), CancellationToken.None).ConfigureAwait(false);
}
} // end of payload sending.. Time to disconnect clients that are no longer connected

//LiveMap.Log(LiveMap.LogLevel.All, "Client size: {0}", clients.Count);
foreach(string endpoint in disconnectedClients)
{
WebSocket des;
if(clients.TryRemove(endpoint, out des))
{
LiveMap.Log(LiveMap.LogLevel.All, "Garbage cleanup.. Removed disconnected client {0}", endpoint);
des.Dispose();
}
}

}
}

Expand Down
21 changes: 18 additions & 3 deletions src/Live Map/WebSocketServer.cs
Expand Up @@ -5,6 +5,7 @@
using System.Threading;
using System.Threading.Tasks;
using vtortola.WebSockets;
using vtortola.WebSockets.Http;
using vtortola.WebSockets.Rfc6455;

/*
Expand Down Expand Up @@ -35,16 +36,30 @@ public WebSocketServer(int port)
{
WebSocketListenerOptions opts = new WebSocketListenerOptions()
{
SubProtocols = new string[] { "text" }
SubProtocols = new string[] { "text" },
//NegotiationQueueCapacity = 128,
ParallelNegotiations = 16,
HttpAuthenticationHandler = async (request, response) => {
await Task.Delay(0); // To shut the fucking IDE up
if (LiveMap.accessOrigin == "*") // If they're allowing anyone
{
return true;
}
// Check if the origin is the same as the accessOrigin in CFG file
return request.Headers["Origin"].Equals(LiveMap.accessOrigin, StringComparison.CurrentCultureIgnoreCase);
}
};
opts.Standards.RegisterRfc6455();

//opts.HttpAuthenticationHandler = OnHttpNegotiationDelegate;

listener = new WebSocketListener(new System.Net.IPEndPoint(System.Net.IPAddress.Any, port), opts);

LiveMap.Log(LiveMap.LogLevel.Basic, "Created websocket server");
}


public async void Start()
{
await listener.StartAsync();
Expand All @@ -70,7 +85,7 @@ public async Task ListenAsync()

}catch(Exception ex)
{
LiveMap.Log(LiveMap.LogLevel.Basic, "Error in ListenAsync:\n{0}\n---Inner:\n{1} ", ex.Message, ex.InnerException);
//LiveMap.Log(LiveMap.LogLevel.Basic, "Error in ListenAsync:\n{0}\n---Inner:\n{1} ", ex.Message, ex.InnerException);
if (OnError != null)
OnError.Invoke(null, ex);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Live Map/packages.config
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="deniszykov.WebSocketListener" version="4.0.0" targetFramework="net452" />
<package id="deniszykov.WebSocketListener" version="4.1.3" targetFramework="net452" />
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net452" />
</packages>
2 changes: 1 addition & 1 deletion version.json
@@ -1,3 +1,3 @@
{
"resource" : "2.2.4"
"resource" : "2.2.7"
}

0 comments on commit 345e266

Please sign in to comment.