Merge branch 'server' into client
This commit is contained in:
@@ -1,10 +1,65 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using Newtonsoft;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace Server
|
namespace Server
|
||||||
{
|
{
|
||||||
class Client
|
class Client
|
||||||
{
|
{
|
||||||
|
private Communication communication;
|
||||||
|
private TcpClient tcpClient;
|
||||||
|
private NetworkStream stream;
|
||||||
|
private byte[] buffer = new byte[1024];
|
||||||
|
private byte[] totalBuffer = new byte[1024];
|
||||||
|
|
||||||
|
public string Username { get; set; }
|
||||||
|
|
||||||
|
public Client(Communication communication, TcpClient tcpClient)
|
||||||
|
{
|
||||||
|
this.communication = communication;
|
||||||
|
this.tcpClient = tcpClient;
|
||||||
|
this.stream = this.tcpClient.GetStream();
|
||||||
|
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnRead(IAsyncResult ar)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
int receivedBytes = stream.EndRead(ar);
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (IOException)
|
||||||
|
{
|
||||||
|
communication.Disconnect(this);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int counter = 0;
|
||||||
|
|
||||||
|
while (buffer.Length > counter)
|
||||||
|
{
|
||||||
|
byte[] lenghtBytes = new byte[4];
|
||||||
|
Array.Copy(buffer, counter, lenghtBytes, 0, 4);
|
||||||
|
int length = Convert.ToInt32(lenghtBytes);
|
||||||
|
byte[] packet = new byte[length];
|
||||||
|
Array.Copy(buffer, counter + 4, packet, 0, length-4);
|
||||||
|
HandleData(Encoding.Default.GetString(packet));
|
||||||
|
counter += length;
|
||||||
|
}
|
||||||
|
|
||||||
|
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void HandleData(string packet)
|
||||||
|
{
|
||||||
|
Console.WriteLine(packet);
|
||||||
|
JsonConvert.DeserializeObject(packet);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace Server
|
|
||||||
{
|
|
||||||
class Comms
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
39
Server/Communication.cs
Normal file
39
Server/Communication.cs
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO.Pipes;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Server
|
||||||
|
{
|
||||||
|
class Communication
|
||||||
|
{
|
||||||
|
private TcpListener listener;
|
||||||
|
private List<Client> clients;
|
||||||
|
|
||||||
|
public Communication(TcpListener listener)
|
||||||
|
{
|
||||||
|
this.listener = listener;
|
||||||
|
this.clients = new List<Client>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Start()
|
||||||
|
{
|
||||||
|
listener.Start();
|
||||||
|
listener.BeginAcceptTcpClient(new AsyncCallback(OnConnect), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnConnect(IAsyncResult ar)
|
||||||
|
{
|
||||||
|
var tcpClient = listener.EndAcceptTcpClient(ar);
|
||||||
|
Console.WriteLine($"Client connected from {tcpClient.Client.RemoteEndPoint}");
|
||||||
|
clients.Add(new Client(this, tcpClient));
|
||||||
|
listener.BeginAcceptTcpClient(new AsyncCallback(OnConnect), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void Disconnect(Client client)
|
||||||
|
{
|
||||||
|
clients.Remove(client);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
|
||||||
namespace Server
|
namespace Server
|
||||||
{
|
{
|
||||||
@@ -6,7 +8,13 @@ namespace Server
|
|||||||
{
|
{
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Hello World!");
|
Communication communication = new Communication(new TcpListener(IPAddress.Any, 5555));
|
||||||
|
communication.Start();
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,10 @@
|
|||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\Message\Message.csproj" />
|
<ProjectReference Include="..\Message\Message.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user