├── .gitignore ├── ClientExample ├── ClientExample.csproj └── Program.cs ├── FUNDING.yml ├── LICENCE ├── MLAPI.ServerList.Client ├── MLAPI.ServerList.Client.csproj ├── ServerConnection.cs └── ServerModel.cs ├── MLAPI.ServerList.Server ├── Configuration.cs ├── MLAPI.ServerList.Server.csproj ├── Program.cs ├── QueryParser.cs ├── ServerContract.cs └── ServerModel.cs ├── MLAPI.ServerList.Shared ├── ContractType.cs ├── HashCode.cs ├── MLAPI.ServerList.Shared.projitems ├── MLAPI.ServerList.Shared.shproj └── MessageType.cs ├── MLAPI.ServerList.sln └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # globs 2 | Makefile.in 3 | *.userprefs 4 | *.usertasks 5 | config.make 6 | config.status 7 | aclocal.m4 8 | install-sh 9 | autom4te.cache/ 10 | *.tar.gz 11 | tarballs/ 12 | test-results/ 13 | 14 | # Mac bundle stuff 15 | *.dmg 16 | *.app 17 | 18 | # content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore 19 | # General 20 | .DS_Store 21 | .AppleDouble 22 | .LSOverride 23 | 24 | # Icon must end with two \r 25 | Icon 26 | 27 | 28 | # Thumbnails 29 | ._* 30 | 31 | # Files that might appear in the root of a volume 32 | .DocumentRevisions-V100 33 | .fseventsd 34 | .Spotlight-V100 35 | .TemporaryItems 36 | .Trashes 37 | .VolumeIcon.icns 38 | .com.apple.timemachine.donotpresent 39 | 40 | # Directories potentially created on remote AFP share 41 | .AppleDB 42 | .AppleDesktop 43 | Network Trash Folder 44 | Temporary Items 45 | .apdisk 46 | 47 | # content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore 48 | # Windows thumbnail cache files 49 | Thumbs.db 50 | ehthumbs.db 51 | ehthumbs_vista.db 52 | 53 | # Dump file 54 | *.stackdump 55 | 56 | # Folder config file 57 | [Dd]esktop.ini 58 | 59 | # Recycle Bin used on file shares 60 | $RECYCLE.BIN/ 61 | 62 | # Windows Installer files 63 | *.cab 64 | *.msi 65 | *.msix 66 | *.msm 67 | *.msp 68 | 69 | # Windows shortcuts 70 | *.lnk 71 | 72 | # content below from: https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 73 | ## Ignore Visual Studio temporary files, build results, and 74 | ## files generated by popular Visual Studio add-ons. 75 | ## 76 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 77 | 78 | # User-specific files 79 | *.suo 80 | *.user 81 | *.userosscache 82 | *.sln.docstates 83 | 84 | # User-specific files (MonoDevelop/Xamarin Studio) 85 | *.userprefs 86 | 87 | # Build results 88 | [Dd]ebug/ 89 | [Dd]ebugPublic/ 90 | [Rr]elease/ 91 | [Rr]eleases/ 92 | x64/ 93 | x86/ 94 | bld/ 95 | [Bb]in/ 96 | [Oo]bj/ 97 | [Ll]og/ 98 | 99 | # Visual Studio 2015/2017 cache/options directory 100 | .vs/ 101 | # Uncomment if you have tasks that create the project's static files in wwwroot 102 | #wwwroot/ 103 | 104 | # Visual Studio 2017 auto generated files 105 | Generated\ Files/ 106 | 107 | # MSTest test Results 108 | [Tt]est[Rr]esult*/ 109 | [Bb]uild[Ll]og.* 110 | 111 | # NUNIT 112 | *.VisualState.xml 113 | TestResult.xml 114 | 115 | # Build Results of an ATL Project 116 | [Dd]ebugPS/ 117 | [Rr]eleasePS/ 118 | dlldata.c 119 | 120 | # Benchmark Results 121 | BenchmarkDotNet.Artifacts/ 122 | 123 | # .NET Core 124 | project.lock.json 125 | project.fragment.lock.json 126 | artifacts/ 127 | 128 | # StyleCop 129 | StyleCopReport.xml 130 | 131 | # Files built by Visual Studio 132 | *_i.c 133 | *_p.c 134 | *_h.h 135 | *.ilk 136 | *.meta 137 | *.obj 138 | *.iobj 139 | *.pch 140 | *.pdb 141 | *.ipdb 142 | *.pgc 143 | *.pgd 144 | *.rsp 145 | *.sbr 146 | *.tlb 147 | *.tli 148 | *.tlh 149 | *.tmp 150 | *.tmp_proj 151 | *_wpftmp.csproj 152 | *.log 153 | *.vspscc 154 | *.vssscc 155 | .builds 156 | *.pidb 157 | *.svclog 158 | *.scc 159 | 160 | # Chutzpah Test files 161 | _Chutzpah* 162 | 163 | # Visual C++ cache files 164 | ipch/ 165 | *.aps 166 | *.ncb 167 | *.opendb 168 | *.opensdf 169 | *.sdf 170 | *.cachefile 171 | *.VC.db 172 | *.VC.VC.opendb 173 | 174 | # Visual Studio profiler 175 | *.psess 176 | *.vsp 177 | *.vspx 178 | *.sap 179 | 180 | # Visual Studio Trace Files 181 | *.e2e 182 | 183 | # TFS 2012 Local Workspace 184 | $tf/ 185 | 186 | # Guidance Automation Toolkit 187 | *.gpState 188 | 189 | # ReSharper is a .NET coding add-in 190 | _ReSharper*/ 191 | *.[Rr]e[Ss]harper 192 | *.DotSettings.user 193 | 194 | # JustCode is a .NET coding add-in 195 | .JustCode 196 | 197 | # TeamCity is a build add-in 198 | _TeamCity* 199 | 200 | # DotCover is a Code Coverage Tool 201 | *.dotCover 202 | 203 | # AxoCover is a Code Coverage Tool 204 | .axoCover/* 205 | !.axoCover/settings.json 206 | 207 | # Visual Studio code coverage results 208 | *.coverage 209 | *.coveragexml 210 | 211 | # NCrunch 212 | _NCrunch_* 213 | .*crunch*.local.xml 214 | nCrunchTemp_* 215 | 216 | # MightyMoose 217 | *.mm.* 218 | AutoTest.Net/ 219 | 220 | # Web workbench (sass) 221 | .sass-cache/ 222 | 223 | # Installshield output folder 224 | [Ee]xpress/ 225 | 226 | # DocProject is a documentation generator add-in 227 | DocProject/buildhelp/ 228 | DocProject/Help/*.HxT 229 | DocProject/Help/*.HxC 230 | DocProject/Help/*.hhc 231 | DocProject/Help/*.hhk 232 | DocProject/Help/*.hhp 233 | DocProject/Help/Html2 234 | DocProject/Help/html 235 | 236 | # Click-Once directory 237 | publish/ 238 | 239 | # Publish Web Output 240 | *.[Pp]ublish.xml 241 | *.azurePubxml 242 | # Note: Comment the next line if you want to checkin your web deploy settings, 243 | # but database connection strings (with potential passwords) will be unencrypted 244 | *.pubxml 245 | *.publishproj 246 | 247 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 248 | # checkin your Azure Web App publish settings, but sensitive information contained 249 | # in these scripts will be unencrypted 250 | PublishScripts/ 251 | 252 | # NuGet Packages 253 | *.nupkg 254 | # The packages folder can be ignored because of Package Restore 255 | **/[Pp]ackages/* 256 | # except build/, which is used as an MSBuild target. 257 | !**/[Pp]ackages/build/ 258 | # Uncomment if necessary however generally it will be regenerated when needed 259 | #!**/[Pp]ackages/repositories.config 260 | # NuGet v3's project.json files produces more ignorable files 261 | *.nuget.props 262 | *.nuget.targets 263 | 264 | # Microsoft Azure Build Output 265 | csx/ 266 | *.build.csdef 267 | 268 | # Microsoft Azure Emulator 269 | ecf/ 270 | rcf/ 271 | 272 | # Windows Store app package directories and files 273 | AppPackages/ 274 | BundleArtifacts/ 275 | Package.StoreAssociation.xml 276 | _pkginfo.txt 277 | *.appx 278 | 279 | # Visual Studio cache files 280 | # files ending in .cache can be ignored 281 | *.[Cc]ache 282 | # but keep track of directories ending in .cache 283 | !*.[Cc]ache/ 284 | 285 | # Others 286 | ClientBin/ 287 | ~$* 288 | *~ 289 | *.dbmdl 290 | *.dbproj.schemaview 291 | *.jfm 292 | *.pfx 293 | *.publishsettings 294 | orleans.codegen.cs 295 | 296 | # Including strong name files can present a security risk 297 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 298 | #*.snk 299 | 300 | # Since there are multiple workflows, uncomment next line to ignore bower_components 301 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 302 | #bower_components/ 303 | 304 | # RIA/Silverlight projects 305 | Generated_Code/ 306 | 307 | # Backup & report files from converting an old project file 308 | # to a newer Visual Studio version. Backup files are not needed, 309 | # because we have git ;-) 310 | _UpgradeReport_Files/ 311 | Backup*/ 312 | UpgradeLog*.XML 313 | UpgradeLog*.htm 314 | ServiceFabricBackup/ 315 | *.rptproj.bak 316 | 317 | # SQL Server files 318 | *.mdf 319 | *.ldf 320 | *.ndf 321 | 322 | # Business Intelligence projects 323 | *.rdl.data 324 | *.bim.layout 325 | *.bim_*.settings 326 | *.rptproj.rsuser 327 | 328 | # Microsoft Fakes 329 | FakesAssemblies/ 330 | 331 | # GhostDoc plugin setting file 332 | *.GhostDoc.xml 333 | 334 | # Node.js Tools for Visual Studio 335 | .ntvs_analysis.dat 336 | node_modules/ 337 | 338 | # Visual Studio 6 build log 339 | *.plg 340 | 341 | # Visual Studio 6 workspace options file 342 | *.opt 343 | 344 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 345 | *.vbw 346 | 347 | # Visual Studio LightSwitch build output 348 | **/*.HTMLClient/GeneratedArtifacts 349 | **/*.DesktopClient/GeneratedArtifacts 350 | **/*.DesktopClient/ModelManifest.xml 351 | **/*.Server/GeneratedArtifacts 352 | **/*.Server/ModelManifest.xml 353 | _Pvt_Extensions 354 | 355 | # Paket dependency manager 356 | .paket/paket.exe 357 | paket-files/ 358 | 359 | # FAKE - F# Make 360 | .fake/ 361 | 362 | # JetBrains Rider 363 | .idea/ 364 | *.sln.iml 365 | 366 | # CodeRush personal settings 367 | .cr/personal 368 | 369 | # Python Tools for Visual Studio (PTVS) 370 | __pycache__/ 371 | *.pyc 372 | 373 | # Cake - Uncomment if you are using it 374 | # tools/** 375 | # !tools/packages.config 376 | 377 | # Tabs Studio 378 | *.tss 379 | 380 | # Telerik's JustMock configuration file 381 | *.jmconfig 382 | 383 | # BizTalk build output 384 | *.btp.cs 385 | *.btm.cs 386 | *.odx.cs 387 | *.xsd.cs 388 | 389 | # OpenCover UI analysis results 390 | OpenCover/ 391 | 392 | # Azure Stream Analytics local run output 393 | ASALocalRun/ 394 | 395 | # MSBuild Binary and Structured Log 396 | *.binlog 397 | 398 | # NVidia Nsight GPU debugger configuration file 399 | *.nvuser 400 | 401 | # MFractors (Xamarin productivity tool) working folder 402 | .mfractor/ 403 | 404 | # Local History for Visual Studio 405 | .localhistory/ -------------------------------------------------------------------------------- /ClientExample/ClientExample.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /ClientExample/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using MLAPI.ServerList.Client; 5 | 6 | namespace ClientExample 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | // Register 100 servers 13 | for (int i = 0; i < 1000; i++) 14 | { 15 | ServerConnection advertConnection = new ServerConnection(); 16 | 17 | // Connect 18 | advertConnection.Connect("127.0.0.1", 9423).AsyncWaitHandle.WaitOne(); 19 | 20 | // Create server data 21 | Dictionary data = new Dictionary 22 | { 23 | { "Players", (int)i }, 24 | { "Name", "This is the name" } 25 | }; 26 | 27 | // Register server 28 | advertConnection.StartAdvertisment(data); 29 | } 30 | 31 | using (ServerConnection queryConnection = new ServerConnection()) 32 | { 33 | // Connect 34 | queryConnection.Connect("127.0.0.1", 9423); 35 | 36 | // Send query 37 | List models = queryConnection.SendQuery(@" 38 | { 39 | ""$and"": [ 40 | { 41 | ""Players"": { 42 | ""$gte"": 20 43 | } 44 | }, 45 | { 46 | ""Players"": { 47 | ""$lte"": 50 48 | } 49 | }, 50 | { 51 | ""Players"": { 52 | ""$in"": [ 53 | 12, 54 | 13, 55 | 14, 56 | 23, 57 | 43, 58 | 51 59 | ] 60 | } 61 | } 62 | ] 63 | }"); 64 | 65 | Console.WriteLine(string.Format("| {0,5} | {1,5} | {2,5} |", "UUID", "Name", "Players")); 66 | Console.WriteLine(string.Join(Environment.NewLine, models.Select(x => string.Format("| {0,5} | {1,5} | {2,5} |", x.Id, x.ContractData["Name"], x.ContractData["Players"])))); 67 | } 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: TwoTenPvP -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | 2 | MIT License 3 | 4 | Copyright (c) 2020 Albin Corén 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | -------------------------------------------------------------------------------- /MLAPI.ServerList.Client/MLAPI.ServerList.Client.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net35;net45;net471;netstandard2.0 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /MLAPI.ServerList.Client/ServerConnection.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Net; 6 | using System.Net.Sockets; 7 | using System.Text; 8 | using System.Threading; 9 | using MLAPI.ServerList.Shared; 10 | 11 | namespace MLAPI.ServerList.Client 12 | { 13 | public class ServerConnection : IDisposable 14 | { 15 | private readonly Dictionary>> queryCallbacks = new Dictionary>>(); 16 | private Dictionary> validationCallbacks = new Dictionary>(); 17 | private TcpClient client = new TcpClient(); 18 | private bool isAdvertising; 19 | private Guid? registerGuid = null; 20 | private Dictionary advertismentData = null; 21 | 22 | public List SendQuery(string query) 23 | { 24 | using (MemoryStream stream = new MemoryStream()) 25 | { 26 | using (BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8)) 27 | { 28 | stream.Position = 2; 29 | 30 | AutoResetEvent resetEvent = new AutoResetEvent(false); 31 | List serverModels = null; 32 | 33 | Guid guid = Guid.NewGuid(); 34 | 35 | queryCallbacks.Add(guid, (models) => 36 | { 37 | serverModels = models; 38 | resetEvent.Set(); 39 | }); 40 | 41 | // Write packet 42 | writer.Write((byte)MessageType.Query); 43 | writer.Write(guid.ToString()); 44 | writer.Write(query); 45 | 46 | stream.Position = 0; 47 | for (byte i = 0; i < sizeof(ushort); i++) stream.WriteByte(((byte)(stream.Length >> (i * 8)))); 48 | client.Client.Send(stream.GetBuffer(), 0, (int)stream.Length, SocketFlags.None); 49 | 50 | // Wait for response 51 | resetEvent.WaitOne(); 52 | 53 | return serverModels; 54 | } 55 | } 56 | } 57 | 58 | public IAsyncResult Connect(string host, int port) 59 | { 60 | return client.BeginConnect(host, port, (args) => 61 | { 62 | client.EndConnect(args); 63 | 64 | new Thread(() => 65 | { 66 | while (client.Connected) 67 | { 68 | try 69 | { 70 | using (MemoryStream stream = new MemoryStream()) 71 | { 72 | // Buffer to copy between NetworkStream and MemoryStream 73 | byte[] buffer = new byte[1024]; 74 | 75 | // Only do when there is data 76 | while (client.GetStream().DataAvailable) 77 | { 78 | // Read data from the NetworkStream in increments of 1024 79 | int count = client.GetStream().Read(buffer, 0, 1024); 80 | 81 | // Write the data to the MemoryStream 82 | stream.Write(buffer, 0, count); 83 | } 84 | 85 | // Set stream at start 86 | stream.Position = 0; 87 | 88 | using (BinaryReader reader = new BinaryReader(stream, Encoding.UTF8)) 89 | { 90 | byte messageType = reader.ReadByte(); 91 | 92 | if (messageType == (byte)MessageType.QueryResponse) 93 | { 94 | Guid callbackGuid = new Guid(reader.ReadString()); 95 | 96 | int serverCount = reader.ReadInt32(); 97 | 98 | List models = new List(); 99 | 100 | for (int i = 0; i < serverCount; i++) 101 | { 102 | Guid serverGuid = new Guid(reader.ReadString()); 103 | IPAddress address = new IPAddress(reader.ReadBytes(16)); 104 | DateTime lastPing = DateTime.FromBinary(reader.ReadInt64()); 105 | 106 | ServerModel model = new ServerModel() 107 | { 108 | Id = serverGuid, 109 | Address = address, 110 | LastPingTime = lastPing, 111 | ContractData = new Dictionary() 112 | }; 113 | 114 | int dataCount = reader.ReadInt32(); 115 | 116 | for (int x = 0; x < dataCount; x++) 117 | { 118 | string name = reader.ReadString(); 119 | ContractType type = (ContractType)reader.ReadByte(); 120 | 121 | switch (type) 122 | { 123 | case ContractType.Int8: 124 | model.ContractData[name] = reader.ReadSByte(); 125 | break; 126 | case ContractType.Int16: 127 | model.ContractData[name] = reader.ReadInt16(); 128 | break; 129 | case ContractType.Int32: 130 | model.ContractData[name] = reader.ReadInt32(); 131 | break; 132 | case ContractType.Int64: 133 | model.ContractData[name] = reader.ReadInt64(); 134 | break; 135 | case ContractType.UInt8: 136 | model.ContractData[name] = reader.ReadByte(); 137 | break; 138 | case ContractType.UInt16: 139 | model.ContractData[name] = reader.ReadUInt16(); 140 | break; 141 | case ContractType.UInt32: 142 | model.ContractData[name] = reader.ReadUInt32(); 143 | break; 144 | case ContractType.UInt64: 145 | model.ContractData[name] = reader.ReadUInt64(); 146 | break; 147 | case ContractType.String: 148 | model.ContractData[name] = reader.ReadString(); 149 | break; 150 | case ContractType.Buffer: 151 | model.ContractData[name] = reader.ReadBytes(reader.ReadInt32()); 152 | break; 153 | case ContractType.Guid: 154 | model.ContractData[name] = new Guid(reader.ReadString()); 155 | break; 156 | } 157 | } 158 | 159 | models.Add(model); 160 | } 161 | 162 | if (queryCallbacks.TryGetValue(callbackGuid, out Action> callback)) 163 | { 164 | queryCallbacks.Remove(callbackGuid); 165 | callback(models); 166 | } 167 | } 168 | else if (messageType == (byte)MessageType.RegisterAck) 169 | { 170 | registerGuid = new Guid(reader.ReadString()); 171 | bool success = reader.ReadBoolean(); 172 | 173 | if (!success) 174 | { 175 | // TODO: Error 176 | } 177 | } 178 | else if (messageType == (byte)MessageType.ContractResponse) 179 | { 180 | Guid callbackGuid = new Guid(reader.ReadString()); 181 | bool success = reader.ReadBoolean(); 182 | 183 | if (validationCallbacks.TryGetValue(callbackGuid, out Action callback)) 184 | { 185 | validationCallbacks.Remove(callbackGuid); 186 | callback(success); 187 | } 188 | } 189 | } 190 | } 191 | } 192 | catch (Exception) 193 | { 194 | 195 | } 196 | } 197 | }).Start(); 198 | }, null); 199 | } 200 | 201 | public bool ValidateContract(Dictionary data) 202 | { 203 | Type[] acceptedTypes = new Type[] 204 | { 205 | typeof(sbyte), 206 | typeof(short), 207 | typeof(int), 208 | typeof(long), 209 | typeof(byte), 210 | typeof(ushort), 211 | typeof(uint), 212 | typeof(ulong), 213 | typeof(string), 214 | typeof(byte[]), 215 | typeof(Guid) 216 | }; 217 | 218 | Dictionary validationData = data.Where(x => acceptedTypes.Contains(x.Value.GetType())).ToDictionary(x => x.Key, x => x.Value); 219 | 220 | Guid guid = Guid.NewGuid(); 221 | 222 | bool result = false; 223 | AutoResetEvent resetEvent = new AutoResetEvent(false); 224 | 225 | validationCallbacks.Add(guid, (success) => 226 | { 227 | result = success; 228 | resetEvent.Set(); 229 | }); 230 | 231 | using (MemoryStream stream = new MemoryStream()) 232 | { 233 | using (BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8)) 234 | { 235 | stream.Position = 2; 236 | 237 | writer.Write((byte)MessageType.ContractCheck); 238 | writer.Write(guid.ToString()); 239 | writer.Write(validationData.Count); 240 | 241 | foreach (KeyValuePair pair in validationData) 242 | { 243 | writer.Write(pair.Key); 244 | 245 | if (pair.Value is sbyte) 246 | { 247 | writer.Write((byte)ContractType.Int8); 248 | } 249 | else if (pair.Value is short) 250 | { 251 | writer.Write((byte)ContractType.Int16); 252 | } 253 | else if (pair.Value is int) 254 | { 255 | writer.Write((byte)ContractType.Int32); 256 | } 257 | else if (pair.Value is long) 258 | { 259 | writer.Write((byte)ContractType.Int64); 260 | } 261 | else if (pair.Value is byte) 262 | { 263 | writer.Write((byte)ContractType.UInt8); 264 | } 265 | else if (pair.Value is ushort) 266 | { 267 | writer.Write((byte)ContractType.UInt16); 268 | } 269 | else if (pair.Value is uint) 270 | { 271 | writer.Write((byte)ContractType.UInt32); 272 | } 273 | else if (pair.Value is ulong) 274 | { 275 | writer.Write((byte)ContractType.UInt64); 276 | } 277 | else if (pair.Value is byte[] bytes) 278 | { 279 | writer.Write((byte)ContractType.Buffer); 280 | } 281 | else if (pair.Value is Guid) 282 | { 283 | writer.Write((byte)ContractType.Guid); 284 | } 285 | else if (pair.Value is string) 286 | { 287 | writer.Write((byte)ContractType.String); 288 | } 289 | } 290 | 291 | stream.Position = 0; 292 | for (byte i = 0; i < sizeof(ushort); i++) stream.WriteByte(((byte)(stream.Length >> (i * 8)))); 293 | client.Client.Send(stream.GetBuffer(), 0, (int)stream.Length, SocketFlags.None); 294 | } 295 | } 296 | 297 | // Wait for response 298 | resetEvent.WaitOne(); 299 | 300 | return result; 301 | } 302 | 303 | public void StopAdvertising() 304 | { 305 | if (!isAdvertising) 306 | { 307 | // TODO: Throw 308 | return; 309 | } 310 | 311 | isAdvertising = false; 312 | 313 | DateTime startTime = DateTime.Now; 314 | 315 | while (registerGuid == null && (DateTime.Now - startTime).TotalMilliseconds < 10_000) 316 | { 317 | Thread.Sleep(10); 318 | } 319 | 320 | if (registerGuid != null) 321 | { 322 | using (MemoryStream stream = new MemoryStream()) 323 | { 324 | using (BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8)) 325 | { 326 | stream.Position = 2; 327 | 328 | writer.Write((byte)MessageType.RemoveServer); 329 | writer.Write(registerGuid.Value.ToString()); 330 | 331 | stream.Position = 0; 332 | for (byte i = 0; i < sizeof(ushort); i++) stream.WriteByte(((byte)(stream.Length >> (i * 8)))); 333 | client.Client.Send(stream.GetBuffer(), 0, (int)stream.Length, SocketFlags.None); 334 | } 335 | } 336 | } 337 | } 338 | 339 | public void UpdateAdvertismentData(Dictionary data) 340 | { 341 | if (!isAdvertising) 342 | { 343 | // TODO: Throw 344 | return; 345 | } 346 | 347 | DateTime startTime = DateTime.Now; 348 | 349 | while (registerGuid == null && (DateTime.Now - startTime).TotalMilliseconds < 10_000) 350 | { 351 | Thread.Sleep(10); 352 | } 353 | 354 | if (registerGuid != null) 355 | { 356 | Type[] acceptedTypes = new Type[] 357 | { 358 | typeof(sbyte), 359 | typeof(short), 360 | typeof(int), 361 | typeof(long), 362 | typeof(byte), 363 | typeof(ushort), 364 | typeof(uint), 365 | typeof(ulong), 366 | typeof(string), 367 | typeof(byte[]), 368 | typeof(Guid) 369 | }; 370 | 371 | advertismentData = data.Where(x => acceptedTypes.Contains(x.Value.GetType())).ToDictionary(x => x.Key, x => x.Value); 372 | 373 | using (MemoryStream stream = new MemoryStream()) 374 | { 375 | using (BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8)) 376 | { 377 | stream.Position = 2; 378 | 379 | writer.Write((byte)MessageType.UpdateServer); 380 | writer.Write(registerGuid.Value.ToString()); 381 | 382 | writer.Write(advertismentData.Count); 383 | 384 | foreach (KeyValuePair pair in advertismentData) 385 | { 386 | writer.Write(pair.Key.GetStableHash64()); 387 | 388 | if (pair.Value is sbyte) 389 | { 390 | writer.Write((byte)ContractType.Int8); 391 | writer.Write((sbyte)pair.Value); 392 | } 393 | else if (pair.Value is short) 394 | { 395 | writer.Write((byte)ContractType.Int16); 396 | writer.Write((short)pair.Value); 397 | } 398 | else if (pair.Value is int) 399 | { 400 | writer.Write((byte)ContractType.Int32); 401 | writer.Write((int)pair.Value); 402 | } 403 | else if (pair.Value is long) 404 | { 405 | writer.Write((byte)ContractType.Int64); 406 | writer.Write((long)pair.Value); 407 | } 408 | else if (pair.Value is byte) 409 | { 410 | writer.Write((byte)ContractType.UInt8); 411 | writer.Write((byte)pair.Value); 412 | } 413 | else if (pair.Value is ushort) 414 | { 415 | writer.Write((byte)ContractType.UInt16); 416 | writer.Write((ushort)pair.Value); 417 | } 418 | else if (pair.Value is uint) 419 | { 420 | writer.Write((byte)ContractType.UInt32); 421 | writer.Write((uint)pair.Value); 422 | } 423 | else if (pair.Value is ulong) 424 | { 425 | writer.Write((byte)ContractType.UInt64); 426 | writer.Write((ulong)pair.Value); 427 | } 428 | else if (pair.Value is byte[] bytes) 429 | { 430 | writer.Write((byte)ContractType.Buffer); 431 | writer.Write(bytes.Length); 432 | writer.Write(bytes); 433 | } 434 | else if (pair.Value is Guid guid) 435 | { 436 | writer.Write((byte)ContractType.Guid); 437 | writer.Write(guid.ToString()); 438 | } 439 | else if (pair.Value is string) 440 | { 441 | writer.Write((byte)ContractType.String); 442 | writer.Write((string)pair.Value); 443 | } 444 | } 445 | 446 | stream.Position = 0; 447 | for (byte i = 0; i < sizeof(ushort); i++) stream.WriteByte(((byte)(stream.Length >> (i * 8)))); 448 | client.Client.Send(stream.GetBuffer(), 0, (int)stream.Length, SocketFlags.None); 449 | } 450 | } 451 | } 452 | } 453 | 454 | public void StartAdvertisment(Dictionary data, int delay = 10_000) 455 | { 456 | using (MemoryStream stream = new MemoryStream()) 457 | { 458 | using (BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8)) 459 | { 460 | stream.Position = 2; 461 | 462 | writer.Write((byte)MessageType.RegisterServer); 463 | 464 | Type[] acceptedTypes = new Type[] 465 | { 466 | typeof(sbyte), 467 | typeof(short), 468 | typeof(int), 469 | typeof(long), 470 | typeof(byte), 471 | typeof(ushort), 472 | typeof(uint), 473 | typeof(ulong), 474 | typeof(string), 475 | typeof(byte[]), 476 | typeof(Guid) 477 | }; 478 | 479 | advertismentData = data.Where(x => acceptedTypes.Contains(x.Value.GetType())).ToDictionary(x => x.Key, x => x.Value); 480 | 481 | writer.Write(advertismentData.Count); 482 | 483 | foreach (KeyValuePair pair in advertismentData) 484 | { 485 | writer.Write(pair.Key.GetStableHash64()); 486 | 487 | if (pair.Value is sbyte) 488 | { 489 | writer.Write((byte)ContractType.Int8); 490 | writer.Write((sbyte)pair.Value); 491 | } 492 | else if (pair.Value is short) 493 | { 494 | writer.Write((byte)ContractType.Int16); 495 | writer.Write((short)pair.Value); 496 | } 497 | else if (pair.Value is int) 498 | { 499 | writer.Write((byte)ContractType.Int32); 500 | writer.Write((int)pair.Value); 501 | } 502 | else if (pair.Value is long) 503 | { 504 | writer.Write((byte)ContractType.Int64); 505 | writer.Write((long)pair.Value); 506 | } 507 | else if (pair.Value is byte) 508 | { 509 | writer.Write((byte)ContractType.UInt8); 510 | writer.Write((byte)pair.Value); 511 | } 512 | else if (pair.Value is ushort) 513 | { 514 | writer.Write((byte)ContractType.UInt16); 515 | writer.Write((ushort)pair.Value); 516 | } 517 | else if (pair.Value is uint) 518 | { 519 | writer.Write((byte)ContractType.UInt32); 520 | writer.Write((uint)pair.Value); 521 | } 522 | else if (pair.Value is ulong) 523 | { 524 | writer.Write((byte)ContractType.UInt64); 525 | writer.Write((ulong)pair.Value); 526 | } 527 | else if (pair.Value is byte[] bytes) 528 | { 529 | writer.Write((byte)ContractType.Buffer); 530 | writer.Write(bytes.Length); 531 | writer.Write(bytes); 532 | } 533 | else if (pair.Value is Guid guid) 534 | { 535 | writer.Write((byte)ContractType.Guid); 536 | writer.Write(guid.ToString()); 537 | } 538 | else if (pair.Value is string) 539 | { 540 | writer.Write((byte)ContractType.String); 541 | writer.Write((string)pair.Value); 542 | } 543 | } 544 | 545 | stream.Position = 0; 546 | for (byte i = 0; i < sizeof(ushort); i++) stream.WriteByte(((byte)(stream.Length >> (i * 8)))); 547 | client.Client.Send(stream.GetBuffer(), 0, (int)stream.Length, SocketFlags.None); 548 | } 549 | } 550 | 551 | isAdvertising = true; 552 | 553 | new Thread(() => 554 | { 555 | DateTime lastRegisterTime = DateTime.Now; 556 | 557 | while (isAdvertising) 558 | { 559 | Thread.Sleep(delay - (int)(DateTime.Now - lastRegisterTime).TotalMilliseconds); 560 | 561 | if (registerGuid != null && isAdvertising) 562 | { 563 | lastRegisterTime = DateTime.Now; 564 | 565 | using (MemoryStream stream = new MemoryStream()) 566 | { 567 | using (BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8)) 568 | { 569 | stream.Position = 2; 570 | 571 | writer.Write((byte)MessageType.ServerAlive); 572 | writer.Write(registerGuid.Value.ToString()); 573 | 574 | stream.Position = 0; 575 | for (byte i = 0; i < sizeof(ushort); i++) stream.WriteByte(((byte)(stream.Length >> (i * 8)))); 576 | client.Client.Send(stream.GetBuffer(), 0, (int)stream.Length, SocketFlags.None); 577 | } 578 | } 579 | } 580 | } 581 | }).Start(); 582 | } 583 | 584 | public void Dispose() 585 | { 586 | if (isAdvertising) 587 | { 588 | StopAdvertising(); 589 | isAdvertising = false; 590 | } 591 | 592 | client.Close(); 593 | } 594 | } 595 | } 596 | -------------------------------------------------------------------------------- /MLAPI.ServerList.Client/ServerModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Net; 4 | 5 | namespace MLAPI.ServerList.Client 6 | { 7 | public class ServerModel 8 | { 9 | public Guid Id; 10 | public IPAddress Address { get; set; } = new IPAddress(0); 11 | public Dictionary ContractData { get; set; } = new Dictionary(); 12 | public DateTime LastPingTime; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /MLAPI.ServerList.Server/Configuration.cs: -------------------------------------------------------------------------------- 1 | using MLAPI.ServerList.Shared; 2 | 3 | namespace MLAPI.ServerList.Server 4 | { 5 | public class Configuration 6 | { 7 | public ushort Port { get; set; } = 9423; 8 | public string ListenAddress { get; set; } = "0.0.0.0"; 9 | public bool VerbosePrints = true; 10 | public bool UseMongo { get; set; } = false; 11 | public string MongoConnection { get; set; } = "mongodb://127.0.0.1:27017"; 12 | public int CollectionExpiryDelay { get; set; } = 20 * 60 * 1000; 13 | public string MongoDatabase { get; set; } = "listserver"; 14 | public int ServerTimeout { get; set; } = 20_000; 15 | 16 | public ContractDefinition[] ServerContract { get; set; } = new ContractDefinition[] 17 | { 18 | new ContractDefinition() 19 | { 20 | Name = "Name", 21 | Required = false, 22 | Type = ContractType.String 23 | }, 24 | new ContractDefinition() 25 | { 26 | Name = "Players", 27 | Required = true, 28 | Type = ContractType.Int32 29 | } 30 | }; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /MLAPI.ServerList.Server/MLAPI.ServerList.Server.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /MLAPI.ServerList.Server/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Net; 6 | using System.Net.Sockets; 7 | using System.Reflection; 8 | using System.Text; 9 | using System.Threading; 10 | using System.Threading.Tasks; 11 | using MLAPI.ServerList.Shared; 12 | using MongoDB.Driver; 13 | using Newtonsoft.Json; 14 | using Newtonsoft.Json.Linq; 15 | 16 | namespace MLAPI.ServerList.Server 17 | { 18 | public static class Program 19 | { 20 | private static readonly Dictionary contracts = new Dictionary(); 21 | private static MongoClient mongoClient; 22 | internal static Configuration configuration; 23 | 24 | private static List localModels = new List(); 25 | private static Dictionary receiveBuffers = new Dictionary(); 26 | 27 | public static void Main(string[] args) 28 | { 29 | Console.WriteLine("Starting server..."); 30 | 31 | string currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); 32 | string configPath = Path.Combine(currentPath, "config.json"); 33 | 34 | if (File.Exists(configPath)) 35 | { 36 | try 37 | { 38 | // Parse configuration 39 | configuration = JsonConvert.DeserializeObject(File.ReadAllText(configPath)); 40 | } 41 | catch 42 | { 43 | 44 | } 45 | } 46 | 47 | // Create configuration 48 | if (configuration == null) 49 | { 50 | configuration = new Configuration(); 51 | 52 | File.WriteAllText(configPath, JsonConvert.SerializeObject(configuration, Formatting.Indented)); 53 | } 54 | 55 | // Hash contract definitions 56 | for (int i = 0; i < configuration.ServerContract.Length; i++) 57 | { 58 | contracts.Add(configuration.ServerContract[i].Name.GetStableHash64(), configuration.ServerContract[i]); 59 | } 60 | 61 | if (configuration.UseMongo) 62 | { 63 | mongoClient = new MongoClient(configuration.MongoConnection); 64 | 65 | IndexKeysDefinition lastPingIndexDefinition = Builders.IndexKeys.Ascending(x => x.LastPingTime); 66 | 67 | try 68 | { 69 | mongoClient.GetDatabase(configuration.MongoDatabase).GetCollection("servers").Indexes.DropOne("ServerExpirationIndex"); 70 | } 71 | catch (MongoCommandException) 72 | { 73 | // Index probably didnt exist 74 | } 75 | 76 | mongoClient.GetDatabase(configuration.MongoDatabase).GetCollection("servers").Indexes.CreateOne(new CreateIndexModel(lastPingIndexDefinition, new CreateIndexOptions() 77 | { 78 | Name = "ServerExpirationIndex", 79 | ExpireAfter = TimeSpan.FromMilliseconds(configuration.CollectionExpiryDelay) 80 | })); 81 | 82 | 83 | for (int i = 0; i < configuration.ServerContract.Length; i++) 84 | { 85 | if (configuration.ServerContract[i].Type == ContractType.String) 86 | { 87 | IndexKeysDefinition textIndexDefinition = Builders.IndexKeys.Text(x => x.ContractData[configuration.ServerContract[i].Name]); 88 | 89 | mongoClient.GetDatabase(configuration.MongoDatabase).GetCollection("servers").Indexes.CreateOne(new CreateIndexModel(textIndexDefinition)); 90 | } 91 | } 92 | } 93 | else 94 | { 95 | new Thread(() => 96 | { 97 | while (true) 98 | { 99 | localModels.RemoveAll(x => x != null && (DateTime.UtcNow - x.LastPingTime).TotalMilliseconds > configuration.CollectionExpiryDelay); 100 | 101 | Thread.Sleep(5000); 102 | } 103 | }).Start(); 104 | } 105 | 106 | Socket listener = new Socket(IPAddress.Parse(configuration.ListenAddress).AddressFamily, SocketType.Stream, ProtocolType.Tcp); 107 | listener.Bind(new IPEndPoint(IPAddress.Parse(configuration.ListenAddress), configuration.Port)); 108 | listener.Listen(110); 109 | 110 | StartAccept(listener); 111 | 112 | Console.Read(); 113 | } 114 | 115 | private static void StartAccept(Socket listener) 116 | { 117 | try 118 | { 119 | listener.BeginAccept((e) => 120 | { 121 | Socket socket = listener.EndAccept(e); 122 | 123 | receiveBuffers[socket] = new byte[1024 * 8]; 124 | 125 | HandleData(socket, 0, 0, 2); 126 | 127 | StartAccept(listener); 128 | }, null); 129 | } 130 | catch (Exception e) 131 | { 132 | Console.WriteLine(e); 133 | } 134 | } 135 | 136 | // Position is the pos with the size, targetLength is the length INCLUDING length 137 | private static void HandleData(Socket socket, int readOffset, int position, int targetLength) 138 | { 139 | try 140 | { 141 | socket.BeginReceive(receiveBuffers[socket], readOffset, targetLength - position, SocketFlags.None, (e) => 142 | { 143 | int data = socket.EndReceive(e); 144 | 145 | if (data <= 0) 146 | { 147 | socket.Close(); 148 | socket.Dispose(); 149 | return; 150 | } 151 | 152 | position += data; 153 | readOffset += data; 154 | 155 | if (position >= 2) 156 | { 157 | ushort size = (ushort)(((ushort)receiveBuffers[socket][0]) | ((ushort)receiveBuffers[socket][1] << 8)); 158 | targetLength = (int)size + 2; 159 | 160 | if (targetLength > receiveBuffers[socket].Length) 161 | { 162 | // Message too long. Drop it and fix stuff by continuing the buffer 163 | // TODO 164 | HandleData(socket, 2, position, targetLength); 165 | } 166 | else 167 | { 168 | // Message is of an alright size. 169 | if (position < size) 170 | { 171 | // We are not done reading yet. Continue 172 | HandleData(socket, position, position, targetLength); 173 | } 174 | else 175 | { 176 | // We are done reading. Process the message now 177 | Task.Run(() => HandleIncomingMessage(socket, 2, targetLength - 2)).ContinueWith((task) => 178 | { 179 | // Continue after 180 | HandleData(socket, 0, 0, 2); 181 | }); 182 | } 183 | } 184 | } 185 | else 186 | { 187 | // Only one byte, continue 188 | HandleData(socket, readOffset, position, 2 - position); 189 | } 190 | }, null); 191 | } 192 | catch (Exception e) 193 | { 194 | Console.WriteLine(e); 195 | } 196 | } 197 | 198 | private static async Task HandleIncomingMessage(Socket socket, int offset, int size) 199 | { 200 | try 201 | { 202 | if (size <= 0) 203 | { 204 | return; 205 | } 206 | 207 | using (MemoryStream stream = new MemoryStream(receiveBuffers[socket], offset, size)) 208 | { 209 | using (BinaryReader reader = new BinaryReader(stream, Encoding.UTF8, true)) 210 | { 211 | byte messageType = reader.ReadByte(); 212 | 213 | if (messageType == (byte)MessageType.RegisterServer) 214 | { 215 | Console.WriteLine("[Register] Started"); 216 | 217 | // Parse contract 218 | Dictionary contractValues = new Dictionary(); 219 | int valueCount = reader.ReadInt32(); 220 | 221 | for (int i = 0; i < valueCount; i++) 222 | { 223 | ulong nameHash = reader.ReadUInt64(); 224 | 225 | ContractType type = (ContractType)reader.ReadByte(); 226 | 227 | if (contracts.TryGetValue(nameHash, out ContractDefinition definition) && definition.Type == type) 228 | { 229 | object boxedValue = null; 230 | 231 | switch (definition.Type) 232 | { 233 | case ContractType.Int8: 234 | boxedValue = (long)reader.ReadSByte(); 235 | break; 236 | case ContractType.Int16: 237 | boxedValue = (long)reader.ReadInt16(); 238 | break; 239 | case ContractType.Int32: 240 | boxedValue = (long)reader.ReadInt32(); 241 | break; 242 | case ContractType.Int64: 243 | boxedValue = (long)reader.ReadInt32(); 244 | break; 245 | case ContractType.UInt8: 246 | boxedValue = (long)reader.ReadByte(); 247 | break; 248 | case ContractType.UInt16: 249 | boxedValue = (long)reader.ReadUInt16(); 250 | break; 251 | case ContractType.UInt32: 252 | boxedValue = (long)reader.ReadUInt32(); 253 | break; 254 | case ContractType.UInt64: 255 | boxedValue = (long)reader.ReadUInt64(); 256 | break; 257 | case ContractType.String: 258 | boxedValue = reader.ReadString(); 259 | break; 260 | case ContractType.Buffer: 261 | boxedValue = reader.ReadBytes(reader.ReadInt32()); 262 | break; 263 | case ContractType.Guid: 264 | boxedValue = new Guid(reader.ReadString()); 265 | break; 266 | } 267 | 268 | if (boxedValue != null) 269 | { 270 | contractValues.Add(definition.Name, new ContractValue() 271 | { 272 | Definition = definition, 273 | Value = boxedValue 274 | }); 275 | } 276 | } 277 | else 278 | { 279 | switch (type) 280 | { 281 | case ContractType.Int8: 282 | reader.ReadSByte(); 283 | break; 284 | case ContractType.Int16: 285 | reader.ReadInt16(); 286 | break; 287 | case ContractType.Int32: 288 | reader.ReadInt32(); 289 | break; 290 | case ContractType.Int64: 291 | reader.ReadInt32(); 292 | break; 293 | case ContractType.UInt8: 294 | reader.ReadByte(); 295 | break; 296 | case ContractType.UInt16: 297 | reader.ReadUInt16(); 298 | break; 299 | case ContractType.UInt32: 300 | reader.ReadUInt32(); 301 | break; 302 | case ContractType.UInt64: 303 | reader.ReadUInt64(); 304 | break; 305 | case ContractType.String: 306 | reader.ReadString(); 307 | break; 308 | case ContractType.Buffer: 309 | reader.ReadBytes(reader.ReadInt32()); 310 | break; 311 | case ContractType.Guid: 312 | reader.ReadString(); 313 | break; 314 | } 315 | } 316 | } 317 | 318 | // Contract validation, ensure all REQUIRED fields are present 319 | for (int i = 0; i < configuration.ServerContract.Length; i++) 320 | { 321 | if (configuration.ServerContract[i].Required) 322 | { 323 | if (!contractValues.TryGetValue(configuration.ServerContract[i].Name, out ContractValue contractValue) || contractValue.Definition.Type != configuration.ServerContract[i].Type) 324 | { 325 | // Failure, contract did not match 326 | using (MemoryStream outStream = new MemoryStream()) 327 | { 328 | using (BinaryWriter writer = new BinaryWriter(outStream, Encoding.UTF8, true)) 329 | { 330 | writer.Write((byte)MessageType.RegisterAck); 331 | writer.Write(new Guid().ToString()); 332 | writer.Write(false); 333 | } 334 | 335 | socket.BeginSend(outStream.GetBuffer(), 0, (int)outStream.Length, SocketFlags.None, (e) => 336 | { 337 | socket.EndSend(e); 338 | }, null); 339 | } 340 | 341 | Console.WriteLine("[Register] Registrar broke contract. Missing required field \"" + configuration.ServerContract[i].Name + "\" of type " + configuration.ServerContract[i].Type); 342 | return; 343 | } 344 | } 345 | } 346 | 347 | List validatedValues = new List(); 348 | 349 | // Remove all fields not part of contract 350 | for (int i = 0; i < configuration.ServerContract.Length; i++) 351 | { 352 | if (contractValues.TryGetValue(configuration.ServerContract[i].Name, out ContractValue contractValue) && contractValue.Definition.Type == configuration.ServerContract[i].Type) 353 | { 354 | validatedValues.Add(contractValue); 355 | } 356 | } 357 | 358 | // Create model for DB 359 | ServerModel server = new ServerModel() 360 | { 361 | Id = Guid.NewGuid().ToString(), 362 | LastPingTime = DateTime.UtcNow, 363 | Address = ((IPEndPoint)socket.RemoteEndPoint).Address.MapToIPv6(), 364 | ContractData = new Dictionary() 365 | }; 366 | 367 | // Add contract values to model 368 | for (int i = 0; i < validatedValues.Count; i++) 369 | { 370 | server.ContractData.Add(validatedValues[i].Definition.Name, validatedValues[i].Value); 371 | } 372 | 373 | if (configuration.VerbosePrints) 374 | { 375 | Console.WriteLine("[Register] Adding: " + JsonConvert.SerializeObject(server)); 376 | } 377 | else 378 | { 379 | Console.WriteLine("[Register] Adding 1 server"); 380 | } 381 | 382 | if (configuration.UseMongo) 383 | { 384 | // Insert model to DB 385 | await mongoClient.GetDatabase(configuration.MongoDatabase).GetCollection("servers").InsertOneAsync(server); 386 | } 387 | else 388 | { 389 | localModels.Add(server); 390 | } 391 | 392 | using (MemoryStream outStream = new MemoryStream()) 393 | { 394 | using (BinaryWriter writer = new BinaryWriter(outStream, Encoding.UTF8, true)) 395 | { 396 | writer.Write((byte)MessageType.RegisterAck); 397 | writer.Write(server.Id); 398 | writer.Write(true); 399 | } 400 | 401 | socket.BeginSend(outStream.GetBuffer(), 0, (int)outStream.Length, SocketFlags.None, (e) => 402 | { 403 | socket.EndSend(e); 404 | }, null); 405 | } 406 | } 407 | else if (messageType == (byte)MessageType.Query) 408 | { 409 | DateTime startTime = DateTime.Now; 410 | Console.WriteLine("[Query] Started"); 411 | string guid = reader.ReadString(); 412 | string query = reader.ReadString(); 413 | Console.WriteLine("[Query] Parsing"); 414 | JObject parsedQuery = JObject.Parse(query); 415 | 416 | List serverModel = null; 417 | 418 | if (configuration.UseMongo) 419 | { 420 | Console.WriteLine("[Query] Creating mongo filter"); 421 | FilterDefinition filter = Builders.Filter.And(Builders.Filter.Where(x => x.LastPingTime >= DateTime.UtcNow.AddMilliseconds(-configuration.ServerTimeout)), QueryParser.CreateFilter(new List() { parsedQuery })); 422 | 423 | if (configuration.VerbosePrints) 424 | { 425 | Console.WriteLine("[Query] Executing mongo query \"" + mongoClient.GetDatabase(configuration.MongoDatabase).GetCollection("servers").Find(filter) + "\""); 426 | } 427 | else 428 | { 429 | Console.WriteLine("[Query] Executing mongo query"); 430 | } 431 | 432 | serverModel = await (await mongoClient.GetDatabase(configuration.MongoDatabase).GetCollection("servers").FindAsync(filter)).ToListAsync(); 433 | } 434 | else 435 | { 436 | Console.WriteLine("[Query] Querying local"); 437 | serverModel = localModels.AsParallel().Where(x => x.LastPingTime >= DateTime.UtcNow.AddMilliseconds(-configuration.ServerTimeout) && QueryParser.FilterLocalServers(new List() { parsedQuery }, x)).ToList(); 438 | } 439 | 440 | Console.WriteLine("[Query] Found " + (serverModel == null ? 0 : serverModel.Count) + " results. Total query time: " + (DateTime.Now - startTime).TotalMilliseconds + " ms"); 441 | 442 | using (MemoryStream outStream = new MemoryStream()) 443 | { 444 | using (BinaryWriter writer = new BinaryWriter(outStream, Encoding.UTF8, true)) 445 | { 446 | writer.Write((byte)MessageType.QueryResponse); 447 | writer.Write(guid); 448 | writer.Write(serverModel.Count); 449 | 450 | for (int i = 0; i < serverModel.Count; i++) 451 | { 452 | writer.Write(serverModel[i].Id); 453 | writer.Write(serverModel[i].Address.MapToIPv6().GetAddressBytes()); 454 | writer.Write(serverModel[i].LastPingTime.ToBinary()); 455 | writer.Write(serverModel[i].ContractData.Count); 456 | 457 | foreach (KeyValuePair pair in serverModel[i].ContractData) 458 | { 459 | writer.Write(pair.Key); 460 | writer.Write((byte)contracts[pair.Key.GetStableHash64()].Type); 461 | 462 | switch (contracts[pair.Key.GetStableHash64()].Type) 463 | { 464 | case ContractType.Int8: 465 | writer.Write((sbyte)(long)pair.Value); 466 | break; 467 | case ContractType.Int16: 468 | writer.Write((short)(long)pair.Value); 469 | break; 470 | case ContractType.Int32: 471 | writer.Write((int)(long)pair.Value); 472 | break; 473 | case ContractType.Int64: 474 | writer.Write((long)pair.Value); 475 | break; 476 | case ContractType.UInt8: 477 | writer.Write((byte)(long)pair.Value); 478 | break; 479 | case ContractType.UInt16: 480 | writer.Write((ushort)(long)pair.Value); 481 | break; 482 | case ContractType.UInt32: 483 | writer.Write((uint)(long)pair.Value); 484 | break; 485 | case ContractType.UInt64: 486 | writer.Write((ulong)(long)pair.Value); 487 | break; 488 | case ContractType.String: 489 | writer.Write((string)pair.Value); 490 | break; 491 | case ContractType.Buffer: 492 | writer.Write(((byte[])pair.Value).Length); 493 | writer.Write((byte[])pair.Value); 494 | break; 495 | case ContractType.Guid: 496 | writer.Write(((Guid)pair.Value).ToString()); 497 | break; 498 | } 499 | } 500 | } 501 | } 502 | 503 | socket.BeginSend(outStream.GetBuffer(), 0, (int)outStream.Length, SocketFlags.None, (e) => 504 | { 505 | socket.EndSend(e); 506 | }, null); 507 | } 508 | } 509 | else if (messageType == (byte)MessageType.ServerAlive) 510 | { 511 | Console.WriteLine("[Alive] Started"); 512 | Guid guid = new Guid(reader.ReadString()); 513 | 514 | if (configuration.VerbosePrints) 515 | { 516 | Console.WriteLine("[Alive] Parsed from " + guid.ToString()); 517 | } 518 | else 519 | { 520 | Console.WriteLine("[Alive] Parsed"); 521 | } 522 | 523 | if (configuration.UseMongo) 524 | { 525 | // Find and validate address ownership 526 | FilterDefinition filter = Builders.Filter.And(Builders.Filter.Where(x => x.LastPingTime >= DateTime.UtcNow.AddMilliseconds(-configuration.ServerTimeout)), Builders.Filter.Eq(x => x.Address, ((IPEndPoint)socket.RemoteEndPoint).Address.MapToIPv6()), Builders.Filter.Eq(x => x.Id, guid.ToString())); 527 | // Create update 528 | UpdateDefinition update = Builders.Update.Set(x => x.LastPingTime, DateTime.UtcNow); 529 | 530 | // Execute 531 | await mongoClient.GetDatabase(configuration.MongoDatabase).GetCollection("servers").FindOneAndUpdateAsync(filter, update); 532 | } 533 | else 534 | { 535 | ServerModel model = localModels.Find(x => x.Address.Equals(((IPEndPoint)socket.RemoteEndPoint).Address.MapToIPv6()) && x.Id == guid.ToString() && x.LastPingTime >= DateTime.UtcNow.AddMilliseconds(-configuration.ServerTimeout)); 536 | 537 | if (model != null) 538 | { 539 | model.LastPingTime = DateTime.UtcNow; 540 | } 541 | } 542 | } 543 | else if (messageType == (byte)MessageType.RemoveServer) 544 | { 545 | Console.WriteLine("[Remove] Started"); 546 | Guid guid = new Guid(reader.ReadString()); 547 | 548 | if (configuration.VerbosePrints) 549 | { 550 | Console.WriteLine("[Remove] Parsed from " + guid.ToString()); 551 | } 552 | else 553 | { 554 | Console.WriteLine("[Remove] Parsed"); 555 | } 556 | 557 | ServerModel model = null; 558 | 559 | if (configuration.UseMongo) 560 | { 561 | // Find and validate address ownership 562 | FilterDefinition filter = Builders.Filter.And(Builders.Filter.Where(x => x.LastPingTime >= DateTime.UtcNow.AddMilliseconds(-configuration.ServerTimeout)), Builders.Filter.Eq(x => x.Address, ((IPEndPoint)socket.RemoteEndPoint).Address.MapToIPv6()), Builders.Filter.Eq(x => x.Id, guid.ToString())); 563 | 564 | // Execute 565 | model = await mongoClient.GetDatabase(configuration.MongoDatabase).GetCollection("servers").FindOneAndDeleteAsync(filter); 566 | } 567 | else 568 | { 569 | model = localModels.Find(x => x.Id == guid.ToString() && x.Address.Equals(((IPEndPoint)socket.RemoteEndPoint).Address.MapToIPv6())); 570 | 571 | if (model != null) 572 | { 573 | localModels.Remove(model); 574 | } 575 | } 576 | 577 | if (model != null) 578 | { 579 | if (configuration.VerbosePrints) 580 | { 581 | Console.WriteLine("[Remove] Removed: " + JsonConvert.SerializeObject(model)); 582 | } 583 | else 584 | { 585 | Console.WriteLine("[Remove] Removed 1 element"); 586 | } 587 | } 588 | else 589 | { 590 | Console.WriteLine("[Remove] Not found"); 591 | } 592 | } 593 | else if (messageType == (byte)MessageType.UpdateServer) 594 | { 595 | Console.WriteLine("[Update] Started"); 596 | Guid guid = new Guid(reader.ReadString()); 597 | 598 | ServerModel result = null; 599 | 600 | if (configuration.UseMongo) 601 | { 602 | result = await (await mongoClient.GetDatabase(configuration.MongoDatabase).GetCollection("servers").FindAsync(x => x.Id == guid.ToString() && x.Address == ((IPEndPoint)socket.RemoteEndPoint).Address.MapToIPv6() && x.LastPingTime >= DateTime.UtcNow.AddMilliseconds(-configuration.ServerTimeout))).FirstOrDefaultAsync(); 603 | } 604 | else 605 | { 606 | result = localModels.Find(x => x.Id == guid.ToString() && x.Address.Equals(((IPEndPoint)socket.RemoteEndPoint).Address.MapToIPv6()) && x.LastPingTime >= DateTime.UtcNow.AddMilliseconds(-configuration.ServerTimeout)); 607 | } 608 | 609 | if (result != null) 610 | { 611 | // Parse contract 612 | Dictionary contractValues = new Dictionary(); 613 | int valueCount = reader.ReadInt32(); 614 | 615 | for (int i = 0; i < valueCount; i++) 616 | { 617 | ulong nameHash = reader.ReadUInt64(); 618 | 619 | ContractType type = (ContractType)reader.ReadByte(); 620 | 621 | if (contracts.TryGetValue(nameHash, out ContractDefinition definition) && definition.Type == type) 622 | { 623 | object boxedValue = null; 624 | 625 | switch (definition.Type) 626 | { 627 | case ContractType.Int8: 628 | boxedValue = (long)reader.ReadSByte(); 629 | break; 630 | case ContractType.Int16: 631 | boxedValue = (long)reader.ReadInt16(); 632 | break; 633 | case ContractType.Int32: 634 | boxedValue = (long)reader.ReadInt32(); 635 | break; 636 | case ContractType.Int64: 637 | boxedValue = (long)reader.ReadInt64(); 638 | break; 639 | case ContractType.UInt8: 640 | boxedValue = (long)reader.ReadByte(); 641 | break; 642 | case ContractType.UInt16: 643 | boxedValue = (long)reader.ReadUInt16(); 644 | break; 645 | case ContractType.UInt32: 646 | boxedValue = (long)reader.ReadUInt32(); 647 | break; 648 | case ContractType.UInt64: 649 | boxedValue = (long)reader.ReadUInt64(); 650 | break; 651 | case ContractType.String: 652 | boxedValue = reader.ReadString(); 653 | break; 654 | case ContractType.Buffer: 655 | boxedValue = reader.ReadBytes(reader.ReadInt32()); 656 | break; 657 | case ContractType.Guid: 658 | boxedValue = new Guid(reader.ReadString()); 659 | break; 660 | } 661 | 662 | if (boxedValue != null) 663 | { 664 | contractValues.Add(definition.Name, new ContractValue() 665 | { 666 | Definition = definition, 667 | Value = boxedValue 668 | }); 669 | } 670 | } 671 | else 672 | { 673 | switch (type) 674 | { 675 | case ContractType.Int8: 676 | reader.ReadSByte(); 677 | break; 678 | case ContractType.Int16: 679 | reader.ReadInt16(); 680 | break; 681 | case ContractType.Int32: 682 | reader.ReadInt32(); 683 | break; 684 | case ContractType.Int64: 685 | reader.ReadInt64(); 686 | break; 687 | case ContractType.UInt8: 688 | reader.ReadByte(); 689 | break; 690 | case ContractType.UInt16: 691 | reader.ReadUInt16(); 692 | break; 693 | case ContractType.UInt32: 694 | reader.ReadUInt32(); 695 | break; 696 | case ContractType.UInt64: 697 | reader.ReadUInt64(); 698 | break; 699 | case ContractType.String: 700 | reader.ReadString(); 701 | break; 702 | case ContractType.Buffer: 703 | reader.ReadBytes(reader.ReadInt32()); 704 | break; 705 | case ContractType.Guid: 706 | reader.ReadString(); 707 | break; 708 | } 709 | } 710 | } 711 | 712 | // Contract validation, ensure all REQUIRED fields are present 713 | for (int i = 0; i < configuration.ServerContract.Length; i++) 714 | { 715 | if (configuration.ServerContract[i].Required) 716 | { 717 | if (!contractValues.TryGetValue(configuration.ServerContract[i].Name, out ContractValue contractValue) || contractValue.Definition.Type != configuration.ServerContract[i].Type) 718 | { 719 | // Failure, contract did not match 720 | return; 721 | } 722 | } 723 | } 724 | 725 | List validatedValues = new List(); 726 | 727 | // Remove all fields not part of contract 728 | for (int i = 0; i < configuration.ServerContract.Length; i++) 729 | { 730 | if (contractValues.TryGetValue(configuration.ServerContract[i].Name, out ContractValue contractValue) && contractValue.Definition.Type == configuration.ServerContract[i].Type) 731 | { 732 | validatedValues.Add(contractValue); 733 | } 734 | } 735 | 736 | Dictionary validatedLookupValues = new Dictionary(); 737 | 738 | // Add contract values to model 739 | for (int i = 0; i < validatedValues.Count; i++) 740 | { 741 | validatedLookupValues.Add(validatedValues[i].Definition.Name, validatedValues[i].Value); 742 | } 743 | 744 | if (configuration.UseMongo) 745 | { 746 | // Find and validate address ownership 747 | FilterDefinition filter = Builders.Filter.And(Builders.Filter.Where(x => x.LastPingTime >= DateTime.UtcNow.AddMilliseconds(-configuration.ServerTimeout)), Builders.Filter.Eq(x => x.Address, ((IPEndPoint)socket.RemoteEndPoint).Address.MapToIPv6()), Builders.Filter.Eq(x => x.Id, guid.ToString())); 748 | // Create update 749 | UpdateDefinition update = Builders.Update.Set(x => x.LastPingTime, DateTime.UtcNow).Set(x => x.ContractData, validatedLookupValues); 750 | 751 | // Insert model to DB 752 | await mongoClient.GetDatabase(configuration.MongoDatabase).GetCollection("servers").FindOneAndUpdateAsync(filter, update); 753 | } 754 | else 755 | { 756 | ServerModel model = localModels.Find(x => x.Address.Equals(((IPEndPoint)socket.RemoteEndPoint).Address.MapToIPv6()) && x.Id == guid.ToString() && x.LastPingTime >= DateTime.UtcNow.AddMilliseconds(-configuration.ServerTimeout)); 757 | model.LastPingTime = DateTime.UtcNow; 758 | model.ContractData = validatedLookupValues; 759 | } 760 | } 761 | } 762 | else if (messageType == (byte)MessageType.ContractCheck) 763 | { 764 | Console.WriteLine("[ContractCheck] Started"); 765 | 766 | string guid = reader.ReadString(); 767 | int contractCount = reader.ReadInt32(); 768 | 769 | WeakContractDefinition[] remoteContracts = new WeakContractDefinition[contractCount]; 770 | 771 | for (int i = 0; i < contractCount; i++) 772 | { 773 | remoteContracts[i] = new WeakContractDefinition() 774 | { 775 | Name = reader.ReadString(), 776 | Type = (ContractType)reader.ReadByte() 777 | }; 778 | } 779 | 780 | using (MemoryStream outStream = new MemoryStream()) 781 | { 782 | using (BinaryWriter writer = new BinaryWriter(outStream, Encoding.UTF8, true)) 783 | { 784 | writer.Write((byte)MessageType.ContractResponse); 785 | writer.Write(guid); 786 | writer.Write(ContractDefinition.IsCompatible(remoteContracts, contracts.Select(x => x.Value).ToArray())); 787 | } 788 | 789 | socket.BeginSend(outStream.GetBuffer(), 0, (int)outStream.Length, SocketFlags.None, (e) => 790 | { 791 | socket.EndSend(e); 792 | }, null); 793 | } 794 | } 795 | } 796 | } 797 | } 798 | catch (Exception e) 799 | { 800 | Console.WriteLine(e); 801 | } 802 | } 803 | } 804 | } 805 | -------------------------------------------------------------------------------- /MLAPI.ServerList.Server/QueryParser.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text.RegularExpressions; 5 | using MLAPI.ServerList.Shared; 6 | using MongoDB.Driver; 7 | using Newtonsoft.Json.Linq; 8 | 9 | namespace MLAPI.ServerList.Server 10 | { 11 | public static class QueryParser 12 | { 13 | public static bool FilterLocalServers(List tokens, ServerModel serverModel) 14 | { 15 | string name = null; 16 | 17 | foreach (JToken child in tokens) 18 | { 19 | if (child.Type == JTokenType.Property) 20 | { 21 | JProperty value = (JProperty)child; 22 | name = value.Name; 23 | } 24 | 25 | switch (name) 26 | { 27 | case "$not": 28 | { 29 | if (child.Type == JTokenType.Property) 30 | { 31 | List children = child.Values().ToList(); 32 | 33 | return !FilterLocalServers(new List() { children.First() }, serverModel); 34 | } 35 | } 36 | break; 37 | case "$and": 38 | { 39 | if (child.Type == JTokenType.Property) 40 | { 41 | List children = child.Values().ToList(); 42 | 43 | return children.AsParallel().All(x => FilterLocalServers(new List() { x }, serverModel)); 44 | } 45 | } 46 | break; 47 | case "$or": 48 | { 49 | if (child.Type == JTokenType.Property) 50 | { 51 | List children = child.Values().ToList(); 52 | 53 | return children.AsParallel().Any(x => FilterLocalServers(new List() { x }, serverModel)); 54 | } 55 | } 56 | break; 57 | case "$exists": 58 | { 59 | if (child.Type == JTokenType.Property) 60 | { 61 | string propertyName = ((JProperty)child.Parent.Parent).Name; 62 | 63 | return serverModel.ContractData.ContainsKey(propertyName); 64 | } 65 | } 66 | break; 67 | case "$text": 68 | { 69 | if (child.Type == JTokenType.Property) 70 | { 71 | List children = child.Values().ToList(); 72 | 73 | JToken searchToken = children.Where(x => x is JProperty property && property.Name == "$search").FirstOrDefault(); 74 | JToken caseSensitiveToken = children.Where(x => x is JProperty property && property.Name == "$caseSensitive").FirstOrDefault(); 75 | 76 | // TODO: Document this bad boy. This is NOT part of Mongo and is not supported when running with mongo. 77 | JToken fieldSpecificToken = children.Where(x => x is JProperty property && property.Name == "$fieldSpecific").FirstOrDefault(); 78 | 79 | JToken languageToken = children.Where(x => x is JProperty property && property.Name == "$language").FirstOrDefault(); 80 | JToken diacriticSensitiveToken = children.Where(x => x is JProperty property && property.Name == "$diacriticSensitive").FirstOrDefault(); 81 | 82 | if (languageToken != null) 83 | { 84 | Console.WriteLine("[Query] WARNING - $language is not yet supported on $text"); 85 | } 86 | 87 | if (diacriticSensitiveToken != null) 88 | { 89 | Console.WriteLine("[Query] WARNING - $diacriticSensitive is not yet supported on $text"); 90 | } 91 | 92 | if (searchToken == null) 93 | { 94 | Console.WriteLine("[Query] ERROR - $search property is required on $text"); 95 | return false; 96 | } 97 | 98 | string searchString = searchToken.Values().FirstOrDefault(); 99 | 100 | if (searchString == null) 101 | { 102 | Console.WriteLine("[Query] WARNING - $search string on $text was empty"); 103 | return false; 104 | } 105 | 106 | bool caseSensitive = caseSensitiveToken == null ? false : caseSensitiveToken.Values().FirstOrDefault(); 107 | bool fieldSpecific = fieldSpecificToken == null ? false : fieldSpecificToken.Values().FirstOrDefault(); 108 | 109 | string[] strings = searchString.Split(null); 110 | 111 | string[] searchableFields = Program.configuration.ServerContract.Where(x => x.Type == ContractType.String && (!fieldSpecific || x.Name == ((JProperty)child.Parent.Parent).Name)).Select(x => x.Name).ToArray(); 112 | 113 | string[] values = serverModel.ContractData.Where(x => searchableFields.Contains(x.Key)).SelectMany(x => ((string)x.Value).Split(null)).ToArray(); 114 | 115 | return strings.Any(x => values.Any(y => x.Equals(y, caseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase))); 116 | } 117 | } 118 | break; 119 | case "$in": 120 | { 121 | if (child.Type == JTokenType.Property) 122 | { 123 | string propertyName = ((JProperty)child.Parent.Parent).Name; 124 | 125 | JToken firstToken = child.Values().Values().FirstOrDefault(); 126 | 127 | if (firstToken != null && serverModel.ContractData.ContainsKey(propertyName)) 128 | { 129 | switch (firstToken.Type) 130 | { 131 | case JTokenType.Integer: 132 | return serverModel.ContractData[propertyName] is long && child.Values().Values().Contains((long)serverModel.ContractData[propertyName]); 133 | case JTokenType.Float: 134 | return serverModel.ContractData[propertyName] is float && child.Values().Values().Contains((float)serverModel.ContractData[propertyName]); 135 | case JTokenType.String: 136 | return serverModel.ContractData[propertyName] is string && child.Values().Values().Contains((string)serverModel.ContractData[propertyName]); 137 | case JTokenType.Boolean: 138 | return serverModel.ContractData[propertyName] is bool && child.Values().Values().Contains((bool)serverModel.ContractData[propertyName]); 139 | case JTokenType.Date: 140 | return serverModel.ContractData[propertyName] is DateTime && child.Values().Values().Contains((DateTime)serverModel.ContractData[propertyName]); 141 | case JTokenType.Bytes: 142 | return serverModel.ContractData[propertyName] is byte[] && child.Values().Values().Contains((byte[])serverModel.ContractData[propertyName]); 143 | case JTokenType.Guid: 144 | return serverModel.ContractData[propertyName] is Guid && child.Values().Values().Contains((Guid)serverModel.ContractData[propertyName]); 145 | case JTokenType.Uri: 146 | return serverModel.ContractData[propertyName] is Uri && child.Values().Values().Contains((Uri)serverModel.ContractData[propertyName]); 147 | case JTokenType.TimeSpan: 148 | return serverModel.ContractData[propertyName] is TimeSpan && child.Values().Values().Contains((TimeSpan)serverModel.ContractData[propertyName]); 149 | } 150 | } 151 | 152 | // Fallback, no values provided 153 | return false; 154 | } 155 | } 156 | break; 157 | case "$nin": 158 | { 159 | if (child.Type == JTokenType.Property) 160 | { 161 | string propertyName = ((JProperty)child.Parent.Parent).Name; 162 | 163 | JToken firstToken = child.Values().Values().FirstOrDefault(); 164 | 165 | if (firstToken != null && serverModel.ContractData.ContainsKey(propertyName)) 166 | { 167 | switch (firstToken.Type) 168 | { 169 | case JTokenType.Integer: 170 | return serverModel.ContractData[propertyName] is long && !child.Values().Values().Contains((long)serverModel.ContractData[propertyName]); 171 | case JTokenType.Float: 172 | return serverModel.ContractData[propertyName] is float && !child.Values().Values().Contains((float)serverModel.ContractData[propertyName]); 173 | case JTokenType.String: 174 | return serverModel.ContractData[propertyName] is string && !child.Values().Values().Contains((string)serverModel.ContractData[propertyName]); 175 | case JTokenType.Boolean: 176 | return serverModel.ContractData[propertyName] is bool && !child.Values().Values().Contains((bool)serverModel.ContractData[propertyName]); 177 | case JTokenType.Date: 178 | return serverModel.ContractData[propertyName] is DateTime && !child.Values().Values().Contains((DateTime)serverModel.ContractData[propertyName]); 179 | case JTokenType.Bytes: 180 | return serverModel.ContractData[propertyName] is byte[] && !child.Values().Values().Contains((byte[])serverModel.ContractData[propertyName]); 181 | case JTokenType.Guid: 182 | return serverModel.ContractData[propertyName] is Guid && !child.Values().Values().Contains((Guid)serverModel.ContractData[propertyName]); 183 | case JTokenType.Uri: 184 | return serverModel.ContractData[propertyName] is Uri && !child.Values().Values().Contains((Uri)serverModel.ContractData[propertyName]); 185 | case JTokenType.TimeSpan: 186 | return serverModel.ContractData[propertyName] is TimeSpan && !child.Values().Values().Contains((TimeSpan)serverModel.ContractData[propertyName]); 187 | } 188 | } 189 | 190 | // Fallback, no values provided 191 | return true; 192 | } 193 | } 194 | break; 195 | case "$eq": 196 | { 197 | if (child.Type == JTokenType.Property) 198 | { 199 | string propertyName = ((JProperty)child.Parent.Parent).Name; 200 | 201 | if (serverModel.ContractData.ContainsKey(propertyName)) 202 | { 203 | switch (child.Values().First().Type) 204 | { 205 | case JTokenType.Integer: 206 | return serverModel.ContractData[propertyName] is long && (long)serverModel.ContractData[propertyName] == child.Values().Values().First(); 207 | case JTokenType.Float: 208 | return serverModel.ContractData[propertyName] is float && Math.Abs((float)serverModel.ContractData[propertyName] - child.Values().Values().First()) < 0.0001; 209 | case JTokenType.String: 210 | return serverModel.ContractData[propertyName] is string && (string)serverModel.ContractData[propertyName] == child.Values().Values().First(); 211 | case JTokenType.Boolean: 212 | return serverModel.ContractData[propertyName] is bool && (bool)serverModel.ContractData[propertyName] == child.Values().Values().First(); 213 | case JTokenType.Date: 214 | return serverModel.ContractData[propertyName] is DateTime && (DateTime)serverModel.ContractData[propertyName] == child.Values().Values().First(); 215 | case JTokenType.Bytes: 216 | return serverModel.ContractData[propertyName] is byte[] && ((byte[])serverModel.ContractData[propertyName]).SequenceEqual(child.Values().Values().First()); 217 | case JTokenType.Guid: 218 | return serverModel.ContractData[propertyName] is Guid && (Guid)serverModel.ContractData[propertyName] == child.Values().Values().First(); 219 | case JTokenType.Uri: 220 | return serverModel.ContractData[propertyName] is Uri && (Uri)serverModel.ContractData[propertyName] == child.Values().Values().First(); 221 | case JTokenType.TimeSpan: 222 | return serverModel.ContractData[propertyName] is TimeSpan && (TimeSpan)serverModel.ContractData[propertyName] == child.Values().Values().First(); 223 | case JTokenType.Null: 224 | return serverModel.ContractData[propertyName] == null; 225 | default: 226 | { 227 | Console.WriteLine("[Query] ERROR - Cannot operate $eq with value of type " + child.Values().First().Type); 228 | return false; 229 | } 230 | } 231 | } 232 | else 233 | { 234 | // Value does not exist 235 | return false; 236 | } 237 | } 238 | } 239 | break; 240 | case "$ne": 241 | { 242 | if (child.Type == JTokenType.Property) 243 | { 244 | string propertyName = ((JProperty)child.Parent.Parent).Name; 245 | 246 | if (serverModel.ContractData.ContainsKey(propertyName)) 247 | { 248 | switch (child.Values().First().Type) 249 | { 250 | case JTokenType.Integer: 251 | return !(serverModel.ContractData[propertyName] is long) || (long)serverModel.ContractData[propertyName] != child.Values().Values().First(); 252 | case JTokenType.Float: 253 | return !(serverModel.ContractData[propertyName] is float) || Math.Abs((float)serverModel.ContractData[propertyName] - child.Values().Values().First()) >= 0.0001; 254 | case JTokenType.String: 255 | return !(serverModel.ContractData[propertyName] is string) || (string)serverModel.ContractData[propertyName] != child.Values().Values().First(); 256 | case JTokenType.Boolean: 257 | return !(serverModel.ContractData[propertyName] is bool) || (bool)serverModel.ContractData[propertyName] != child.Values().Values().First(); 258 | case JTokenType.Date: 259 | return !(serverModel.ContractData[propertyName] is DateTime) || (DateTime)serverModel.ContractData[propertyName] != child.Values().Values().First(); 260 | case JTokenType.Bytes: 261 | return !(serverModel.ContractData[propertyName] is byte[]) || !((byte[])serverModel.ContractData[propertyName]).SequenceEqual(child.Values().Values().First()); 262 | case JTokenType.Guid: 263 | return !(serverModel.ContractData[propertyName] is Guid) || (Guid)serverModel.ContractData[propertyName] != child.Values().Values().First(); 264 | case JTokenType.Uri: 265 | return !(serverModel.ContractData[propertyName] is Uri) || (Uri)serverModel.ContractData[propertyName] != child.Values().Values().First(); 266 | case JTokenType.TimeSpan: 267 | return !(serverModel.ContractData[propertyName] is TimeSpan) || (TimeSpan)serverModel.ContractData[propertyName] != child.Values().Values().First(); 268 | case JTokenType.Null: 269 | return serverModel.ContractData[propertyName] != null; 270 | default: 271 | { 272 | Console.WriteLine("[Query] ERROR - Cannot operate $ne with value of type " + child.Values().First().Type); 273 | return false; 274 | } 275 | } 276 | } 277 | else 278 | { 279 | // Value does not exist 280 | return false; 281 | } 282 | } 283 | } 284 | break; 285 | case "$regex": 286 | { 287 | if (child.Type == JTokenType.Property) 288 | { 289 | JToken optionsToken = child.Values().Where(x => x is JProperty property && property.Name == "$options").FirstOrDefault(); 290 | 291 | if (optionsToken != null) 292 | { 293 | Console.WriteLine("[Query] WARNING - $options is not yet supported on $regex"); 294 | } 295 | 296 | string propertyName = ((JProperty)child.Parent.Parent).Name; 297 | 298 | if (serverModel.ContractData.ContainsKey(propertyName) && serverModel.ContractData[propertyName] is string) 299 | { 300 | switch (child.Values().First().Type) 301 | { 302 | case JTokenType.String: 303 | { 304 | return Regex.IsMatch((string)serverModel.ContractData[propertyName], child.Values().Values().First()); 305 | } 306 | default: 307 | { 308 | Console.WriteLine("[Query] ERROR - Cannot operate $regex with non string values"); 309 | return false; 310 | } 311 | } 312 | } 313 | else 314 | { 315 | // Value does not exist OR is the wrong type 316 | return false; 317 | } 318 | } 319 | } 320 | break; 321 | case "$gt": 322 | { 323 | if (child.Type == JTokenType.Property) 324 | { 325 | string propertyName = ((JProperty)child.Parent.Parent).Name; 326 | 327 | if (serverModel.ContractData.ContainsKey(propertyName)) 328 | { 329 | switch (child.Values().First().Type) 330 | { 331 | case JTokenType.Integer: 332 | return serverModel.ContractData[propertyName] is long && (long)serverModel.ContractData[propertyName] > child.Values().Values().First(); 333 | case JTokenType.Float: 334 | return serverModel.ContractData[propertyName] is float && (float)serverModel.ContractData[propertyName] > child.Values().Values().First(); 335 | case JTokenType.Date: 336 | return serverModel.ContractData[propertyName] is DateTime && (DateTime)serverModel.ContractData[propertyName] > child.Values().Values().First(); 337 | case JTokenType.TimeSpan: 338 | return serverModel.ContractData[propertyName] is TimeSpan && (TimeSpan)serverModel.ContractData[propertyName] > child.Values().Values().First(); 339 | default: 340 | { 341 | Console.WriteLine("[Query] ERROR - Cannot operate $gt with value of type " + child.Values().First().Type); 342 | return false; 343 | } 344 | } 345 | } 346 | else 347 | { 348 | // Value does not exist 349 | return false; 350 | } 351 | } 352 | } 353 | break; 354 | case "$gte": 355 | { 356 | if (child.Type == JTokenType.Property) 357 | { 358 | string propertyName = ((JProperty)child.Parent.Parent).Name; 359 | 360 | if (serverModel.ContractData.ContainsKey(propertyName)) 361 | { 362 | switch (child.Values().First().Type) 363 | { 364 | case JTokenType.Integer: 365 | return serverModel.ContractData[propertyName] is long && (long)serverModel.ContractData[propertyName] >= child.Values().Values().First(); 366 | case JTokenType.Float: 367 | return serverModel.ContractData[propertyName] is float && (float)serverModel.ContractData[propertyName] >= child.Values().Values().First(); 368 | case JTokenType.Date: 369 | return serverModel.ContractData[propertyName] is DateTime && (DateTime)serverModel.ContractData[propertyName] >= child.Values().Values().First(); 370 | case JTokenType.TimeSpan: 371 | return serverModel.ContractData[propertyName] is TimeSpan && (TimeSpan)serverModel.ContractData[propertyName] >= child.Values().Values().First(); 372 | default: 373 | { 374 | Console.WriteLine("[Query] ERROR - Cannot operate $gte with value of type " + child.Values().First().Type); 375 | return false; 376 | } 377 | } 378 | } 379 | else 380 | { 381 | // Value does not exist 382 | return false; 383 | } 384 | } 385 | } 386 | break; 387 | case "$lt": 388 | { 389 | if (child.Type == JTokenType.Property) 390 | { 391 | string propertyName = ((JProperty)child.Parent.Parent).Name; 392 | 393 | if (serverModel.ContractData.ContainsKey(propertyName)) 394 | { 395 | switch (child.Values().First().Type) 396 | { 397 | case JTokenType.Integer: 398 | return serverModel.ContractData[propertyName] is long && (long)serverModel.ContractData[propertyName] < child.Values().Values().First(); 399 | case JTokenType.Float: 400 | return serverModel.ContractData[propertyName] is float && (float)serverModel.ContractData[propertyName] < child.Values().Values().First(); 401 | case JTokenType.Date: 402 | return serverModel.ContractData[propertyName] is DateTime && (DateTime)serverModel.ContractData[propertyName] < child.Values().Values().First(); 403 | case JTokenType.TimeSpan: 404 | return serverModel.ContractData[propertyName] is TimeSpan && (TimeSpan)serverModel.ContractData[propertyName] < child.Values().Values().First(); 405 | default: 406 | { 407 | Console.WriteLine("[Query] ERROR - Cannot operate $lt with value of type " + child.Values().First().Type); 408 | return false; 409 | } 410 | } 411 | } 412 | else 413 | { 414 | // Value does not exist 415 | return false; 416 | } 417 | } 418 | } 419 | break; 420 | case "$lte": 421 | { 422 | if (child.Type == JTokenType.Property) 423 | { 424 | string propertyName = ((JProperty)child.Parent.Parent).Name; 425 | 426 | if (serverModel.ContractData.ContainsKey(propertyName)) 427 | { 428 | switch (child.Values().First().Type) 429 | { 430 | case JTokenType.Integer: 431 | return serverModel.ContractData[propertyName] is long && (long)serverModel.ContractData[propertyName] <= child.Values().Values().First(); 432 | case JTokenType.Float: 433 | return serverModel.ContractData[propertyName] is float && (float)serverModel.ContractData[propertyName] <= child.Values().Values().First(); 434 | case JTokenType.Date: 435 | return serverModel.ContractData[propertyName] is DateTime && (DateTime)serverModel.ContractData[propertyName] <= child.Values().Values().First(); 436 | case JTokenType.TimeSpan: 437 | return serverModel.ContractData[propertyName] is TimeSpan && (TimeSpan)serverModel.ContractData[propertyName] <= child.Values().Values().First(); 438 | default: 439 | { 440 | Console.WriteLine("[Query] ERROR - Cannot operate $lte with value of type " + child.Values().First().Type); 441 | return false; 442 | } 443 | } 444 | } 445 | else 446 | { 447 | // Value does not exist 448 | return false; 449 | } 450 | } 451 | } 452 | break; 453 | default: 454 | { 455 | if (child.Type == JTokenType.Property || child.Type == JTokenType.Object) 456 | { 457 | return FilterLocalServers(child.Children().ToList(), serverModel); 458 | } 459 | } 460 | break; 461 | } 462 | } 463 | 464 | return true; 465 | } 466 | 467 | public static FilterDefinition CreateFilter(List tokens) 468 | { 469 | string name = null; 470 | 471 | foreach (var child in tokens) 472 | { 473 | if (child.Type == JTokenType.Property) 474 | { 475 | JProperty value = (JProperty)child; 476 | name = value.Name; 477 | } 478 | 479 | switch (name) 480 | { 481 | case "$not": 482 | { 483 | if (child.Type == JTokenType.Property) 484 | { 485 | return Builders.Filter.Not(CreateFilter(new List() { child.Values().First() })); 486 | } 487 | } 488 | break; 489 | case "$and": 490 | { 491 | if (child.Type == JTokenType.Property) 492 | { 493 | List children = child.Values().ToList(); 494 | List> childFilters = new List>(); 495 | 496 | for (int i = 0; i < children.Count; i++) 497 | { 498 | childFilters.Add(CreateFilter(new List() { children[i] })); 499 | } 500 | 501 | return Builders.Filter.And(childFilters); 502 | } 503 | } 504 | break; 505 | case "$or": 506 | { 507 | if (child.Type == JTokenType.Property) 508 | { 509 | List children = child.Values().ToList(); 510 | List> childFilters = new List>(); 511 | 512 | for (int i = 0; i < children.Count; i++) 513 | { 514 | childFilters.Add(CreateFilter(new List() { children[i] })); 515 | } 516 | 517 | return Builders.Filter.Or(childFilters); 518 | } 519 | } 520 | break; 521 | case "$exists": 522 | { 523 | if (child.Type == JTokenType.Property) 524 | { 525 | return Builders.Filter.Exists("ContractData." + ((JProperty)child.Parent.Parent).Name); 526 | } 527 | } 528 | break; 529 | case "$text": 530 | { 531 | if (child.Type == JTokenType.Property) 532 | { 533 | List children = child.Values().ToList(); 534 | 535 | JToken searchToken = children.Where(x => x is JProperty property && property.Name == "$search").FirstOrDefault(); 536 | JToken caseSensitiveToken = children.Where(x => x is JProperty property && property.Name == "$caseSensitive").FirstOrDefault(); 537 | 538 | // TODO: Document this bad boy. This is NOT part of Mongo and is not supported when running with mongo. 539 | JToken fieldSpecificToken = children.Where(x => x is JProperty property && property.Name == "$fieldSpecific").FirstOrDefault(); 540 | 541 | JToken languageToken = children.Where(x => x is JProperty property && property.Name == "$language").FirstOrDefault(); 542 | JToken diacriticSensitiveToken = children.Where(x => x is JProperty property && property.Name == "$diacriticSensitive").FirstOrDefault(); 543 | 544 | if (languageToken != null) 545 | { 546 | Console.WriteLine("[Query] WARNING - $language is not yet supported on $text"); 547 | } 548 | 549 | if (diacriticSensitiveToken != null) 550 | { 551 | Console.WriteLine("[Query] WARNING - $diacriticSensitive is not yet supported on $text"); 552 | } 553 | 554 | if (fieldSpecificToken != null) 555 | { 556 | Console.WriteLine("[Query] WARNING - $fieldSpecific is not supported when running with mongo"); 557 | } 558 | 559 | if (searchToken == null) 560 | { 561 | Console.WriteLine("[Query] ERROR - $search property is required on $text"); 562 | return Builders.Filter.Where(x => false); 563 | } 564 | 565 | string searchString = searchToken.Values().FirstOrDefault(); 566 | 567 | if (searchString == null) 568 | { 569 | Console.WriteLine("[Query] WARNING - $search string on $text was empty"); 570 | return Builders.Filter.Where(x => false); 571 | } 572 | 573 | bool caseSensitive = caseSensitiveToken.Values().FirstOrDefault(); 574 | 575 | return Builders.Filter.Text(searchString, new TextSearchOptions() 576 | { 577 | CaseSensitive = caseSensitive 578 | }); 579 | } 580 | } 581 | break; 582 | case "$in": 583 | { 584 | if (child.Type == JTokenType.Property) 585 | { 586 | JToken firstToken = child.Values().Values().FirstOrDefault(); 587 | 588 | if (firstToken != null) 589 | { 590 | switch (firstToken.Type) 591 | { 592 | case JTokenType.Integer: 593 | return Builders.Filter.In("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values()); 594 | case JTokenType.Float: 595 | return Builders.Filter.In("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values()); 596 | case JTokenType.String: 597 | return Builders.Filter.In("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values()); 598 | case JTokenType.Boolean: 599 | return Builders.Filter.In("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values()); 600 | case JTokenType.Date: 601 | return Builders.Filter.In("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values()); 602 | case JTokenType.Bytes: 603 | return Builders.Filter.In("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values()); 604 | case JTokenType.Guid: 605 | return Builders.Filter.In("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values()); 606 | case JTokenType.Uri: 607 | return Builders.Filter.In("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values()); 608 | case JTokenType.TimeSpan: 609 | return Builders.Filter.In("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values()); 610 | } 611 | } 612 | 613 | // Fallback, no values provided 614 | return Builders.Filter.Where(x => false); 615 | } 616 | } 617 | break; 618 | case "$nin": 619 | { 620 | if (child.Type == JTokenType.Property) 621 | { 622 | JToken firstToken = child.Values().Values().FirstOrDefault(); 623 | 624 | if (firstToken != null) 625 | { 626 | switch (firstToken.Type) 627 | { 628 | case JTokenType.Integer: 629 | return Builders.Filter.Nin("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values()); 630 | case JTokenType.Float: 631 | return Builders.Filter.Nin("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values()); 632 | case JTokenType.String: 633 | return Builders.Filter.Nin("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values()); 634 | case JTokenType.Boolean: 635 | return Builders.Filter.Nin("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values()); 636 | case JTokenType.Date: 637 | return Builders.Filter.Nin("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values()); 638 | case JTokenType.Bytes: 639 | return Builders.Filter.Nin("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values()); 640 | case JTokenType.Guid: 641 | return Builders.Filter.Nin("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values()); 642 | case JTokenType.Uri: 643 | return Builders.Filter.Nin("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values()); 644 | case JTokenType.TimeSpan: 645 | return Builders.Filter.Nin("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values()); 646 | } 647 | } 648 | 649 | // Fallback, no values provided. Thus its NOT inside 650 | return Builders.Filter.Where(x => true); 651 | } 652 | } 653 | break; 654 | case "$eq": 655 | { 656 | if (child.Type == JTokenType.Property) 657 | { 658 | switch (child.Values().First().Type) 659 | { 660 | case JTokenType.Integer: 661 | return Builders.Filter.Eq("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 662 | case JTokenType.Float: 663 | return Builders.Filter.Eq("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 664 | case JTokenType.String: 665 | return Builders.Filter.Eq("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 666 | case JTokenType.Boolean: 667 | return Builders.Filter.Eq("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 668 | case JTokenType.Date: 669 | return Builders.Filter.Eq("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 670 | case JTokenType.Bytes: 671 | return Builders.Filter.Eq("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 672 | case JTokenType.Guid: 673 | return Builders.Filter.Eq("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 674 | case JTokenType.Uri: 675 | return Builders.Filter.Eq("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 676 | case JTokenType.TimeSpan: 677 | return Builders.Filter.Eq("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 678 | default: 679 | { 680 | Console.WriteLine("[Query] ERROR - Cannot operate $eq with value of type " + child.Values().First().Type); 681 | return Builders.Filter.Where(x => false); 682 | } 683 | } 684 | } 685 | } 686 | break; 687 | case "$ne": 688 | { 689 | if (child.Type == JTokenType.Property) 690 | { 691 | switch (child.Values().First().Type) 692 | { 693 | case JTokenType.Integer: 694 | return Builders.Filter.Ne("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 695 | case JTokenType.Float: 696 | return Builders.Filter.Ne("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 697 | case JTokenType.String: 698 | return Builders.Filter.Ne("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 699 | case JTokenType.Boolean: 700 | return Builders.Filter.Ne("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 701 | case JTokenType.Date: 702 | return Builders.Filter.Ne("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 703 | case JTokenType.Bytes: 704 | return Builders.Filter.Ne("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 705 | case JTokenType.Guid: 706 | return Builders.Filter.Ne("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 707 | case JTokenType.Uri: 708 | return Builders.Filter.Ne("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 709 | case JTokenType.TimeSpan: 710 | return Builders.Filter.Ne("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 711 | default: 712 | { 713 | Console.WriteLine("[Query] ERROR - Cannot operate $eq with value of type " + child.Values().First().Type); 714 | return Builders.Filter.Where(x => false); 715 | } 716 | } 717 | } 718 | } 719 | break; 720 | case "$regex": 721 | { 722 | if (child.Type == JTokenType.Property) 723 | { 724 | JToken optionsToken = child.Values().Where(x => x is JProperty property && property.Name == "$options").FirstOrDefault(); 725 | 726 | if (optionsToken != null) 727 | { 728 | Console.WriteLine("[Query] WARNING - $options is not yet supported on $regex"); 729 | } 730 | 731 | switch (child.Values().First().Type) 732 | { 733 | case JTokenType.String: 734 | return Builders.Filter.Regex("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 735 | default: 736 | { 737 | Console.WriteLine("[Query] ERROR - Cannot operate $regex with non string values"); 738 | return Builders.Filter.Where(x => false); 739 | } 740 | } 741 | } 742 | } 743 | break; 744 | case "$gt": 745 | { 746 | if (child.Type == JTokenType.Property) 747 | { 748 | switch (child.Values().First().Type) 749 | { 750 | case JTokenType.Integer: 751 | return Builders.Filter.Gt("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 752 | case JTokenType.Float: 753 | return Builders.Filter.Gt("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 754 | case JTokenType.String: 755 | return Builders.Filter.Gt("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 756 | case JTokenType.Boolean: 757 | return Builders.Filter.Gt("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 758 | case JTokenType.Date: 759 | return Builders.Filter.Gt("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 760 | case JTokenType.Bytes: 761 | return Builders.Filter.Gt("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 762 | case JTokenType.Guid: 763 | return Builders.Filter.Gt("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 764 | case JTokenType.Uri: 765 | return Builders.Filter.Gt("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 766 | case JTokenType.TimeSpan: 767 | return Builders.Filter.Gt("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 768 | default: 769 | { 770 | Console.WriteLine("[Query] ERROR - Cannot operate $gt with value of type " + child.Values().First().Type); 771 | return Builders.Filter.Where(x => false); 772 | } 773 | } 774 | } 775 | } 776 | break; 777 | case "$gte": 778 | { 779 | if (child.Type == JTokenType.Property) 780 | { 781 | switch (child.Values().First().Type) 782 | { 783 | case JTokenType.Integer: 784 | return Builders.Filter.Gte("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 785 | case JTokenType.Float: 786 | return Builders.Filter.Gte("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 787 | case JTokenType.String: 788 | return Builders.Filter.Gte("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 789 | case JTokenType.Boolean: 790 | return Builders.Filter.Gte("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 791 | case JTokenType.Date: 792 | return Builders.Filter.Gte("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 793 | case JTokenType.Bytes: 794 | return Builders.Filter.Gte("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 795 | case JTokenType.Guid: 796 | return Builders.Filter.Gte("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 797 | case JTokenType.Uri: 798 | return Builders.Filter.Gte("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 799 | case JTokenType.TimeSpan: 800 | return Builders.Filter.Gte("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 801 | default: 802 | { 803 | Console.WriteLine("[Query] ERROR - Cannot operate $gte with value of type " + child.Values().First().Type); 804 | return Builders.Filter.Where(x => false); 805 | } 806 | } 807 | } 808 | } 809 | break; 810 | case "$lt": 811 | { 812 | if (child.Type == JTokenType.Property) 813 | { 814 | switch (child.Values().First().Type) 815 | { 816 | case JTokenType.Integer: 817 | return Builders.Filter.Lt("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 818 | case JTokenType.Float: 819 | return Builders.Filter.Lt("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 820 | case JTokenType.String: 821 | return Builders.Filter.Lt("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 822 | case JTokenType.Boolean: 823 | return Builders.Filter.Lt("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 824 | case JTokenType.Date: 825 | return Builders.Filter.Lt("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 826 | case JTokenType.Bytes: 827 | return Builders.Filter.Lt("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 828 | case JTokenType.Guid: 829 | return Builders.Filter.Lt("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 830 | case JTokenType.Uri: 831 | return Builders.Filter.Lt("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 832 | case JTokenType.TimeSpan: 833 | return Builders.Filter.Lt("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 834 | default: 835 | { 836 | Console.WriteLine("[Query] ERROR - Cannot operate $lt with value of type " + child.Values().First().Type); 837 | return Builders.Filter.Where(x => false); 838 | } 839 | } 840 | } 841 | } 842 | break; 843 | case "$lte": 844 | { 845 | if (child.Type == JTokenType.Property) 846 | { 847 | switch (child.Values().First().Type) 848 | { 849 | case JTokenType.Integer: 850 | return Builders.Filter.Lte("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 851 | case JTokenType.Float: 852 | return Builders.Filter.Lte("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 853 | case JTokenType.String: 854 | return Builders.Filter.Lte("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 855 | case JTokenType.Boolean: 856 | return Builders.Filter.Lte("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 857 | case JTokenType.Date: 858 | return Builders.Filter.Lte("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 859 | case JTokenType.Bytes: 860 | return Builders.Filter.Lte("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 861 | case JTokenType.Guid: 862 | return Builders.Filter.Lte("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 863 | case JTokenType.Uri: 864 | return Builders.Filter.Lte("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 865 | case JTokenType.TimeSpan: 866 | return Builders.Filter.Lte("ContractData." + ((JProperty)child.Parent.Parent).Name, child.Values().Values().First()); 867 | default: 868 | { 869 | Console.WriteLine("[Query] ERROR - Cannot operate $lte with value of type " + child.Values().First().Type); 870 | return Builders.Filter.Where(x => false); 871 | } 872 | } 873 | } 874 | } 875 | break; 876 | default: 877 | { 878 | if (child.Type == JTokenType.Property || child.Type == JTokenType.Object) 879 | { 880 | return CreateFilter(child.Children().ToList()); 881 | } 882 | } 883 | break; 884 | } 885 | } 886 | 887 | return Builders.Filter.Empty; 888 | } 889 | } 890 | } 891 | -------------------------------------------------------------------------------- /MLAPI.ServerList.Server/ServerContract.cs: -------------------------------------------------------------------------------- 1 | using MLAPI.ServerList.Shared; 2 | using Newtonsoft.Json; 3 | using Newtonsoft.Json.Converters; 4 | 5 | namespace MLAPI.ServerList.Server 6 | { 7 | public struct WeakContractDefinition 8 | { 9 | public string Name { get; set; } 10 | public ContractType Type { get; set; } 11 | } 12 | 13 | public struct ContractDefinition 14 | { 15 | public string Name { get; set; } 16 | [JsonConverter(typeof(StringEnumConverter))] 17 | public ContractType Type { get; set; } 18 | public bool Required { get; set; } 19 | 20 | // Checks if two contract definitions are compatible with each other 21 | public static bool IsCompatible(ContractDefinition[] v1, ContractDefinition[] v2) 22 | { 23 | // Contract conflict validation 24 | for (int i = 0; i < v1.Length; i++) 25 | { 26 | bool found = false; 27 | 28 | for (int j = 0; j < v2.Length; j++) 29 | { 30 | if (v1[i].Name == v2[j].Name) 31 | { 32 | if (v1[i].Type != v2[j].Type) 33 | { 34 | return false; 35 | } 36 | 37 | found = true; 38 | } 39 | } 40 | 41 | if (v1[i].Required && !found) 42 | { 43 | // If required, fail if we dont find. 44 | return false; 45 | } 46 | } 47 | 48 | 49 | // Contract conflict validation 50 | for (int i = 0; i < v2.Length; i++) 51 | { 52 | bool found = false; 53 | 54 | for (int j = 0; j < v1.Length; j++) 55 | { 56 | if (v2[i].Name == v1[j].Name) 57 | { 58 | if (v2[i].Type != v1[j].Type) 59 | { 60 | return false; 61 | } 62 | 63 | found = true; 64 | } 65 | } 66 | 67 | if (v2[i].Required && !found) 68 | { 69 | // If required, fail if we dont find. 70 | return false; 71 | } 72 | } 73 | 74 | return true; 75 | } 76 | 77 | public static bool IsCompatible(WeakContractDefinition[] v1, ContractDefinition[] v2) 78 | { 79 | // Contract conflict validation 80 | for (int i = 0; i < v1.Length; i++) 81 | { 82 | for (int j = 0; j < v2.Length; j++) 83 | { 84 | if (v1[i].Name == v2[j].Name) 85 | { 86 | if (v1[i].Type != v2[j].Type) 87 | { 88 | return false; 89 | } 90 | } 91 | } 92 | } 93 | 94 | 95 | // Contract conflict validation 96 | for (int i = 0; i < v2.Length; i++) 97 | { 98 | bool found = false; 99 | 100 | for (int j = 0; j < v1.Length; j++) 101 | { 102 | if (v2[i].Name == v1[j].Name) 103 | { 104 | if (v2[i].Type != v1[j].Type) 105 | { 106 | return false; 107 | } 108 | 109 | found = true; 110 | } 111 | } 112 | 113 | if (v2[i].Required && !found) 114 | { 115 | // If required, fail if we dont find. 116 | return false; 117 | } 118 | } 119 | 120 | return true; 121 | } 122 | } 123 | 124 | public struct ContractValue 125 | { 126 | public ContractDefinition Definition; 127 | public object Value; 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /MLAPI.ServerList.Server/ServerModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Net; 4 | using MongoDB.Bson.Serialization.Attributes; 5 | using Newtonsoft.Json; 6 | 7 | namespace MLAPI.ServerList.Server 8 | { 9 | public class ServerModel 10 | { 11 | [BsonId] 12 | public string Id; 13 | [JsonIgnore] 14 | public IPAddress Address { get; set; } = new IPAddress(0); 15 | public Dictionary ContractData { get; set; } = new Dictionary(); 16 | public DateTime LastPingTime; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /MLAPI.ServerList.Shared/ContractType.cs: -------------------------------------------------------------------------------- 1 | namespace MLAPI.ServerList.Shared 2 | { 3 | public enum ContractType 4 | { 5 | Int8, 6 | Int16, 7 | Int32, 8 | Int64, 9 | UInt8, 10 | UInt16, 11 | UInt32, 12 | UInt64, 13 | String, 14 | Buffer, 15 | Guid 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /MLAPI.ServerList.Shared/HashCode.cs: -------------------------------------------------------------------------------- 1 | namespace MLAPI.ServerList.Shared 2 | { 3 | /// 4 | /// Provides extension methods for getting hashes 5 | /// 6 | public static class HashCode 7 | { 8 | private const uint FNV_offset_basis32 = 2166136261; 9 | private const uint FNV_prime32 = 16777619; 10 | 11 | private const ulong FNV_offset_basis64 = 14695981039346656037; 12 | private const ulong FNV_prime64 = 1099511628211; 13 | 14 | /// 15 | /// non cryptographic stable hash code, 16 | /// it will always return the same hash for the same 17 | /// string. 18 | /// 19 | /// This is simply an implementation of FNV-1 32 bit xor folded to 16 bit 20 | /// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function 21 | /// 22 | /// The stable hash32. 23 | /// Text. 24 | public static ushort GetStableHash16(this string txt) 25 | { 26 | uint hash32 = txt.GetStableHash32(); 27 | 28 | return (ushort)((hash32 >> 16) ^ hash32); 29 | } 30 | 31 | 32 | /// 33 | /// non cryptographic stable hash code, 34 | /// it will always return the same hash for the same 35 | /// string. 36 | /// 37 | /// This is simply an implementation of FNV-1 32 bit 38 | /// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function 39 | /// 40 | /// The stable hash32. 41 | /// Text. 42 | public static uint GetStableHash32(this string txt) 43 | { 44 | unchecked 45 | { 46 | uint hash = FNV_offset_basis32; 47 | for (int i = 0; i < txt.Length; i++) 48 | { 49 | uint ch = txt[i]; 50 | hash = hash * FNV_prime32; 51 | hash = hash ^ ch; 52 | } 53 | return hash; 54 | } 55 | } 56 | 57 | /// 58 | /// non cryptographic stable hash code, 59 | /// it will always return the same hash for the same 60 | /// string. 61 | /// 62 | /// This is simply an implementation of FNV-1 64 bit 63 | /// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function 64 | /// 65 | /// The stable hash32. 66 | /// Text. 67 | public static ulong GetStableHash64(this string txt) 68 | { 69 | unchecked 70 | { 71 | ulong hash = FNV_offset_basis64; 72 | for (int i = 0; i < txt.Length; i++) 73 | { 74 | ulong ch = txt[i]; 75 | hash = hash * FNV_prime64; 76 | hash = hash ^ ch; 77 | } 78 | return hash; 79 | } 80 | } 81 | 82 | /// 83 | /// non cryptographic stable hash code, 84 | /// it will always return the same hash for the same 85 | /// string. 86 | /// 87 | /// This is simply an implementation of FNV-1 32 bit xor folded to 16 bit 88 | /// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function 89 | /// 90 | /// The stable hash32. 91 | /// Text. 92 | public static ushort GetStableHash16(this byte[] bytes) 93 | { 94 | uint hash32 = bytes.GetStableHash32(); 95 | 96 | return (ushort)((hash32 >> 16) ^ hash32); 97 | } 98 | 99 | /// 100 | /// non cryptographic stable hash code, 101 | /// it will always return the same hash for the same 102 | /// string. 103 | /// 104 | /// This is simply an implementation of FNV-1 32 bit 105 | /// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function 106 | /// 107 | /// The stable hash32. 108 | /// Text. 109 | public static uint GetStableHash32(this byte[] bytes) 110 | { 111 | unchecked 112 | { 113 | uint hash = FNV_offset_basis32; 114 | for (int i = 0; i < bytes.Length; i++) 115 | { 116 | uint bt = bytes[i]; 117 | hash = hash * FNV_prime32; 118 | hash = hash ^ bt; 119 | } 120 | return hash; 121 | } 122 | } 123 | 124 | /// 125 | /// non cryptographic stable hash code, 126 | /// it will always return the same hash for the same 127 | /// string. 128 | /// 129 | /// This is simply an implementation of FNV-1 64 bit 130 | /// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function 131 | /// 132 | /// The stable hash32. 133 | /// Text. 134 | public static ulong GetStableHash64(this byte[] bytes) 135 | { 136 | unchecked 137 | { 138 | ulong hash = FNV_offset_basis64; 139 | for (int i = 0; i < bytes.Length; i++) 140 | { 141 | ulong bt = bytes[i]; 142 | hash = hash * FNV_prime64; 143 | hash = hash ^ bt; 144 | } 145 | return hash; 146 | } 147 | } 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /MLAPI.ServerList.Shared/MLAPI.ServerList.Shared.projitems: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath) 5 | true 6 | {E4A4F493-8E04-4CBD-B35A-EA6BA2770045} 7 | 8 | 9 | MLAPI.ServerList.Shared 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /MLAPI.ServerList.Shared/MLAPI.ServerList.Shared.shproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | {E4A4F493-8E04-4CBD-B35A-EA6BA2770045} 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /MLAPI.ServerList.Shared/MessageType.cs: -------------------------------------------------------------------------------- 1 | namespace MLAPI.ServerList.Shared 2 | { 3 | public enum MessageType 4 | { 5 | RegisterServer, 6 | RemoveServer, 7 | UpdateServer, 8 | ServerAlive, 9 | RegisterAck, 10 | Query, 11 | QueryResponse, 12 | ContractCheck, 13 | ContractResponse 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /MLAPI.ServerList.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLAPI.ServerList.Server", "MLAPI.ServerList.Server\MLAPI.ServerList.Server.csproj", "{A9B57DAF-7FBF-46CF-9BD9-36E925B6EFAA}" 5 | EndProject 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLAPI.ServerList.Client", "MLAPI.ServerList.Client\MLAPI.ServerList.Client.csproj", "{49D8D72C-8E8C-43A7-A366-E1831EE24625}" 7 | EndProject 8 | Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "MLAPI.ServerList.Shared", "MLAPI.ServerList.Shared\MLAPI.ServerList.Shared.shproj", "{E4A4F493-8E04-4CBD-B35A-EA6BA2770045}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClientExample", "ClientExample\ClientExample.csproj", "{3EEE2C5A-7B56-4A4E-914C-BFC963E15A4F}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|Any CPU = Debug|Any CPU 15 | Release|Any CPU = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {A9B57DAF-7FBF-46CF-9BD9-36E925B6EFAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {A9B57DAF-7FBF-46CF-9BD9-36E925B6EFAA}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {A9B57DAF-7FBF-46CF-9BD9-36E925B6EFAA}.Release|Any CPU.ActiveCfg = Release|Any CPU 21 | {A9B57DAF-7FBF-46CF-9BD9-36E925B6EFAA}.Release|Any CPU.Build.0 = Release|Any CPU 22 | {49D8D72C-8E8C-43A7-A366-E1831EE24625}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {49D8D72C-8E8C-43A7-A366-E1831EE24625}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {49D8D72C-8E8C-43A7-A366-E1831EE24625}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {49D8D72C-8E8C-43A7-A366-E1831EE24625}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {3EEE2C5A-7B56-4A4E-914C-BFC963E15A4F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {3EEE2C5A-7B56-4A4E-914C-BFC963E15A4F}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {3EEE2C5A-7B56-4A4E-914C-BFC963E15A4F}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {3EEE2C5A-7B56-4A4E-914C-BFC963E15A4F}.Release|Any CPU.Build.0 = Release|Any CPU 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MLAPI.ServerList 2 | The MLAPI.ServerList is a server list designed for video games. It's built of one server application that can shard horizontally and a client library that can register servers and make queries. It's built to be high performance, feature rich and general purpose. 3 | 4 | ## Features 5 | * Cross platform 6 | * Search for servers with the MongoDB query language (sub and super set of the official language) 7 | * Shards horizontally with MongoDB 8 | * Can run without sharding in memory 9 | * Custom data is contracted 10 | * Custom data contracts can be changed and be backwards compatible 11 | 12 | ## TODO 13 | * File database to reduce memory usage when not using shards 14 | * TLS 15 | 16 | ## Usage 17 | The ServerList has two parts, a server and a client library. 18 | 19 | ### Server 20 | The server is a .NET Core application. When started, it generates a configuration file called config.json. The default configuration looks like this: 21 | 22 | ```json 23 | { 24 | "VerbosePrints": true, 25 | "Port": 9423, 26 | "ListenAddress": "0.0.0.0", 27 | "UseMongo": false, 28 | "MongoConnection": "mongodb://127.0.0.1:27017", 29 | "CollectionExpiryDelay": 1200000, 30 | "MongoDatabase": "listserver", 31 | "ServerTimeout": 20000, 32 | "ServerContract": [ 33 | { 34 | "Name": "Name", 35 | "Type": "String", 36 | "Required": false 37 | }, 38 | { 39 | "Name": "Players", 40 | "Type": "Int32", 41 | "Required": true 42 | } 43 | ] 44 | } 45 | ``` 46 | 47 | The options are: 48 | 49 | ##### VerbosePrints 50 | Whether or not prints should include information that **might** be sensitive such as the unique Id of a server. 51 | 52 | ##### Port 53 | The TCP port the server should run on. 54 | 55 | ##### ListenAddress 56 | The local address to listen on. Defaults to 0.0.0.0 which means all interfaces. 57 | 58 | ##### UseMongo 59 | Whether or not to use MongoDB as the backend. This allows you to shard horizontally. 60 | 61 | ##### MongoConnection 62 | The MongoDB connection string. Only required if UseMongo is true. 63 | 64 | ##### MongoDatabase 65 | The database to use in Mongo to store the servers. Only required if UseMongo is true. 66 | 67 | ##### CollectionExpiryDelay 68 | The amount of time a server is in memory before being cleared out. 69 | 70 | ##### ServerTimeout 71 | The amount of time a server has to send a heartbeat before it is concidered dead. 72 | 73 | ##### ServerContract 74 | This is the contract that defines what fields are possible, required for a server to define and also for a client to query on. See the contracts section below. 75 | 76 | ### Client 77 | The client library has two parts, one part is the part which a game server runs, and the other one is what a game client would run. An example can be seen below: 78 | 79 | ```csharp 80 | // Creates a connection object 81 | ServerConnection advertConnection = new ServerConnection(); 82 | 83 | // Connect to the ListServer 84 | advertConnection.Connect("127.0.0.1", 9423); 85 | 86 | // Create some data about our server 87 | Dictionary data = new Dictionary 88 | { 89 | { "Players", 25 }, 90 | { "Name", "This is the name" } 91 | }; 92 | 93 | // Announce our server to the ListServer 94 | advertConnection.StartAdvertisment(data); 95 | ``` 96 | 97 | Additionaly, the server can stop advertising or update its advertisment data. This is useful for having a "Players" count that is changed every time a player connects or leaves. 98 | 99 | ```csharp 100 | // Create some new data about our server 101 | Dictionary data = new Dictionary 102 | { 103 | { "Players", 30 }, 104 | { "Name", "This is the name" } 105 | }; 106 | 107 | // Updates the data on the server 108 | advertConnection.UpdateAdvertismentData(data); 109 | ``` 110 | 111 | ```csharp 112 | // Stops advertising to the server 113 | advertConnection.StopAdvertising(); 114 | ``` 115 | 116 | For game clients, they can query the ListServer like this: 117 | ```csharp 118 | // Disposing a ServerConnection will close the connection and clean itself up. 119 | using (ServerConnection queryConnection = new ServerConnection()) 120 | { 121 | // Connect 122 | queryConnection.Connect("127.0.0.1", 9423); 123 | 124 | // Send query to find all servers with players >= 20 AND <= 50. This requires a Players field to be present. 125 | List models = queryConnection.SendQuery(@" 126 | { 127 | ""$and"": [ 128 | { 129 | ""Players"": { 130 | ""$gte"": 20 131 | } 132 | }, 133 | { 134 | ""Players"": { 135 | ""$lte"": 50 136 | } 137 | } 138 | ] 139 | }"); 140 | 141 | // Prints header 142 | Console.WriteLine(string.Format("| {0,5} | {1,5} | {2,5} |", "UUID", "Name", "Players")); 143 | // Prints the servers 144 | Console.WriteLine(string.Join(Environment.NewLine, models.Select(x => string.Format("| {0,5} | {1,5} | {2,5} |", x.Id, x.ContractData["Name"], x.ContractData["Players"])))); 145 | } 146 | ``` 147 | 148 | ## Query 149 | To do a query, you need the client library. After that you can use the MongoDB query language to send queries. The supported operations are: 150 | ``` 151 | $not - Inverses the nested query 152 | $and - Combines multiple queries and ensures all are true 153 | $or - Combines multiple queries and ensures at least one is true 154 | $exists - Ensures a field exists 155 | $text - Does a text search 156 | $regex - Does a regex text search 157 | $in - Ensures a field is included in a specific query list 158 | $nin - Ensures a field is not included in a specific query list 159 | $eq - Ensures a field is equal to a specific value 160 | $ne - Ensures a field is not equal to a specific value 161 | $gt - Ensures a field is greater than a specific value 162 | $gte - Ensures a field is greater than or equal to a specific value 163 | $lt - Ensures a field is less than a specific value 164 | $lte - Ensures a field is less than or equal to a specific value 165 | ``` 166 | 167 | An example query can look like this: 168 | 169 | ```json 170 | { 171 | "$and": [ 172 | { 173 | "Players": { 174 | "$gte": 20 175 | } 176 | }, 177 | { 178 | "$text": { 179 | "$search": "Hello world", 180 | "$caseSensitive": true 181 | } 182 | } 183 | ] 184 | } 185 | ``` 186 | 187 | For more info, you can read up on the MongoDB query language. 188 | 189 | This will find all the servers that has players greater than or equal to ``20`` AND has a text field that matches ``Hello world`` as a text query. Queries can be applied to ANY custom fields such as ELO or whatever field you would like. 190 | 191 | ## Contracts 192 | ServerLists can have custom data, such as the amount of players, server name or any other field you would like. This is enforced with a contract. A contract is made of multiple contract definitions, each definition being a contract for its own field. Each definition has the following properties: 193 | 194 | #### Name 195 | This is the name of the contract. Such as "Players". 196 | 197 | #### Required 198 | Sets whether or not the field is required for all servers. Servers not setting this field, with the correct type will not be added. 199 | 200 | #### Type 201 | This is the field type, it can be one of the following: 202 | 203 | ``` 204 | Int8 - C# sbyte 205 | Int16 - C# short 206 | Int32 - C# int 207 | Int64 - C# long 208 | UInt8 - C# byte 209 | UInt16 - C# ushort 210 | UInt32 - C# uint 211 | UInt64 - C# ulong 212 | String - C# string 213 | Buffer - C# byte[] 214 | Guid - C# Guid 215 | ``` --------------------------------------------------------------------------------