-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from MattEstHaut/feature/server
Ajoute les fonctionnalités essentielles du serveur
- Loading branch information
Showing
3 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System.Net.Sockets; | ||
using Shared.Comm; | ||
using Shared.Resp; | ||
|
||
public class ConnectionHandler | ||
{ | ||
private readonly CommandManager _manager; | ||
|
||
public ConnectionHandler(CommandManager manager) | ||
{ | ||
_manager = manager; | ||
} | ||
|
||
public void Handle(TcpClient socket) | ||
{ | ||
using var client = new Client(socket); | ||
|
||
try | ||
{ | ||
while (socket.Connected) | ||
{ | ||
var request = client.Read(); | ||
|
||
if (request is not ItemArray array) | ||
throw new Exception("Expected array"); | ||
|
||
var args = array.Items.ConvertAll(i => i.ToString() ?? "").ToArray(); | ||
|
||
if (args.Length == 0) | ||
throw new Exception("Expected at least 1 argument"); | ||
|
||
var command = args[0]; | ||
var response = _manager.Execute(command, args[1..]); | ||
client.Send(response); | ||
} | ||
} | ||
finally { } | ||
} | ||
|
||
public async Task HandleAsync(TcpClient socket) | ||
{ | ||
await Task.Run(() => Handle(socket)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
Console.WriteLine("Hello, World!"); | ||
int port = 6379; | ||
|
||
if (args.Length > 0) port = int.Parse(args[0]); | ||
|
||
var database = new Database(15000); | ||
var server = new Server(port, database); | ||
server.Run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Net; | ||
using System.Net.Sockets; | ||
|
||
public class Server | ||
{ | ||
private readonly TcpListener _listener; | ||
private readonly ConnectionHandler _handler; | ||
private readonly CommandManager _manager; | ||
|
||
public Server(int port, Database db) | ||
{ | ||
_listener = new TcpListener(IPAddress.Any, port); | ||
_manager = new CommandManager(db); | ||
_handler = new ConnectionHandler(_manager); | ||
} | ||
|
||
public void Run() | ||
{ | ||
_listener.Start(); | ||
_ = _manager.StartAsync(); | ||
|
||
while (_listener.Server.IsBound) | ||
{ | ||
var socket = _listener.AcceptTcpClient(); | ||
_ = _handler.HandleAsync(socket); | ||
} | ||
} | ||
|
||
public void Stop() | ||
{ | ||
_listener.Stop(); | ||
_manager.Stop(); | ||
} | ||
|
||
public void RunAsync() => Task.Run(() => Run()); | ||
} |