Skip to content

Commit 287ec85

Browse files
committed
added logger & controller select on server side
1 parent bb87e13 commit 287ec85

File tree

3 files changed

+114
-24
lines changed

3 files changed

+114
-24
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea/
2-
.obj/
2+
obj/
3+
bin/

Core/NetJoy/Server/NetJoyServer.cs

+67-23
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
using System;
2+
using System.Linq;
23
using System.Net;
34
using System.Net.Sockets;
45
using System.Threading;
56
using System.Threading.Tasks;
67
using NetJoy.Core.Config;
78
using NetJoy.Core.NetJoy.Packets;
9+
using NetJoy.Core.Utils;
810
using Newtonsoft.Json;
911
using SharpDX.DirectInput;
1012

1113
using Encoding = System.Text.Encoding;
1214

1315
namespace NetJoy.Core.NetJoy.Server
1416
{
15-
public class NetJoyServer
17+
public sealed class NetJoyServer
1618
{
1719
private Joystick _joystick; //joystick we're polling for input
1820
private Socket _clientSocket = null; //the connected client socket
@@ -45,6 +47,7 @@ await Task.Run(() =>
4547
var serverListener = StartServerListener();
4648
var controllerListener = StartControllerListener();
4749

50+
4851
//wait for all tasks to complete
4952
Task.WhenAll(serverListener, controllerListener);
5053
}).ConfigureAwait(false);
@@ -64,7 +67,7 @@ await Task.Run(() =>
6467
var localEndPoint = new IPEndPoint(IPAddress.Any, _configuration.server.port);
6568

6669
//log that we started the server
67-
Console.WriteLine($"Started Server @{ip}:{_configuration.server.port}");
70+
Logger.Debug($"Started Server @{ip}:{_configuration.server.port}");
6871

6972
// Create a TCP/IP socket.
7073
var listener = new Socket(ip.AddressFamily,
@@ -81,7 +84,7 @@ await Task.Run(() =>
8184
_allDone.Reset();
8285

8386
// Start an asynchronous socket to listen for connections.
84-
Console.WriteLine("Waiting for a connection...");
87+
Logger.Debug("Waiting for a connection...");
8588
listener.BeginAccept(
8689
AcceptCallback,
8790
listener );
@@ -178,27 +181,14 @@ private void Send(string message)
178181
private void GetJoystick()
179182
{
180183
var directInput = new DirectInput();
181-
var joystickGuid = Guid.Empty;
182-
183-
//try to get a gamepad
184-
foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AllDevices))
185-
{
186-
joystickGuid = deviceInstance.InstanceGuid;
187-
}
188-
189-
// If Gamepad not found, look for a Joystick
190-
if (joystickGuid == Guid.Empty)
191-
{
192-
foreach (var deviceInstance in directInput.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AllDevices))
193-
{
194-
joystickGuid = deviceInstance.InstanceGuid;
195-
}
196-
}
197-
184+
185+
//get the joystick guid & select a stick
186+
var joystickGuid = SelectStick(directInput);
187+
198188
// If Joystick not found, throws an error
199189
if (joystickGuid == Guid.Empty)
200190
{
201-
Console.WriteLine("No joystick/Gamepad found.");
191+
Logger.LogError("No joystick/Gamepad found.");
202192
Console.ReadKey();
203193
Environment.Exit(1);
204194
}
@@ -207,15 +197,15 @@ private void GetJoystick()
207197
_joystick= new Joystick(directInput, joystickGuid);
208198

209199
//log that we found a joystick
210-
Console.WriteLine($"Found Joystick with GUID: {joystickGuid}");
200+
Logger.Debug($"Found Joystick with GUID: {joystickGuid}");
211201

212202
//query all available effects
213203
var allEffects = _joystick.GetEffects();
214204

215205
//log all of the available effects
216206
foreach (var effect in allEffects)
217207
{
218-
Console.WriteLine($"Found Effect: {effect}");
208+
Logger.Log($"Found Effect: {effect}");
219209
}
220210

221211
//set the buffer size for the joystick
@@ -224,5 +214,59 @@ private void GetJoystick()
224214
//acquire the joystick
225215
_joystick.Acquire();
226216
}
217+
218+
/// <summary>
219+
/// Draw menu for selecting a stick
220+
/// </summary>
221+
/// <param name="directInput"> stick to select</param>
222+
/// <returns></returns>
223+
private Guid SelectStick(DirectInput directInput)
224+
{
225+
do
226+
{
227+
//the joysticks to select from
228+
var joysticks = directInput.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AllDevices);
229+
230+
//add all detected gamepads to the joystick list
231+
foreach (var pad in directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AllDevices))
232+
{
233+
joysticks.Add(pad);
234+
}
235+
236+
//Log the joysticks
237+
for (var index = 0; index < joysticks.Count; index++)
238+
{
239+
var stick = joysticks[index];
240+
241+
Logger.Log($"{index + 1}) {stick.ProductName}");
242+
}
243+
244+
//Log message depending on joysticks
245+
Logger.Log(!joysticks.Any()
246+
? "Plug in a joystick & press enter to rescan!"
247+
: "Enter the ID of the stick you wish to use: ");
248+
249+
//check if the input from the console is valid
250+
var validInput = int.TryParse(Console.ReadLine(), out var choice);
251+
252+
//if the input was invalid continue
253+
if (validInput && choice >= 1 && choice <= joysticks.Count)
254+
{
255+
//clear the console
256+
Console.Clear();
257+
258+
//select the stick and log it
259+
var stick = joysticks[choice - 1];
260+
Logger.Debug($"Selected Joystick: {stick.ProductName}");
261+
return stick.InstanceGuid;
262+
}
263+
264+
//clear the logger
265+
Logger.Clear();
266+
267+
//log an error
268+
Logger.LogError("Invalid Input, Please select from the sticks below!");
269+
} while (true);
270+
}
227271
}
228272
}

Core/Utils/Logger.cs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
3+
namespace NetJoy.Core.Utils
4+
{
5+
public static class Logger
6+
{
7+
/// <summary>
8+
/// Log an error message
9+
/// </summary>
10+
/// <param name="message">to log</param>
11+
public static void LogError(string message)
12+
{
13+
Console.ForegroundColor = ConsoleColor.Red;
14+
Console.WriteLine("[ERROR]: " + message);
15+
}
16+
17+
/// <summary>
18+
/// Clear the console
19+
/// </summary>
20+
public static void Clear()
21+
{
22+
Console.Clear();
23+
}
24+
25+
/// <summary>
26+
/// Log the message with a white foreground
27+
/// </summary>
28+
/// <param name="message">as a string to log</param>
29+
public static void Log(string message)
30+
{
31+
Console.ForegroundColor = ConsoleColor.White;
32+
Console.WriteLine(message);
33+
}
34+
35+
/// <summary>
36+
/// Log a debug message to the console
37+
/// </summary>
38+
/// <param name="message"></param>
39+
public static void Debug(string message)
40+
{
41+
Console.ForegroundColor = ConsoleColor.DarkCyan;
42+
Console.WriteLine($"[DEBUG] {message}");
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)