Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pipe cliente v01 #2

Merged
merged 4 commits into from
Jun 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified TresEnRayaApp/.vs/TresEnRayaApp/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
Binary file modified TresEnRayaApp/.vs/TresEnRayaApp/v17/.futdcache.v1
Binary file not shown.
Binary file modified TresEnRayaApp/.vs/TresEnRayaApp/v17/.suo
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified TresEnRayaApp/.vs/TresEnRayaApp/v17/TestStore/0/testlog.manifest
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ public void AddClientesInHandlerSession()
{
var collectionSession = new System.Collections.Concurrent.ConcurrentQueue<Session>();
var handlerSessions = new HandlerSessionListener(ref collectionSession);
Assert.AreEqual(0, handlerSessions.Sessions.Count);
Assert.AreEqual(0, collectionSession.Count);

handlerSessions.AddClient(new System.Net.Sockets.TcpClient());
handlerSessions.AddClient(new System.Net.Sockets.TcpClient());

Assert.AreEqual(1, handlerSessions.Sessions.Count);
Assert.IsTrue(handlerSessions.Sessions.ToArray()[0].CompleteClients());
Assert.AreEqual(1, collectionSession.Count);
Assert.IsTrue(collectionSession.ToArray()[0].CompleteClients());

handlerSessions.AddClient(new System.Net.Sockets.TcpClient());
handlerSessions.AddClient(new System.Net.Sockets.TcpClient());

Assert.AreEqual(2, handlerSessions.Sessions.Count);
Assert.IsTrue(handlerSessions.Sessions.ToArray()[1].CompleteClients());
Assert.AreEqual(2, collectionSession.Count);
Assert.IsTrue(collectionSession.ToArray()[1].CompleteClients());
}
}
}
67 changes: 62 additions & 5 deletions TresEnRayaApp/ServerQueu.Test/Services/ListenerQueuServerTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using NUnit.Framework;
using TresEnRayaApp;
Expand All @@ -7,32 +10,86 @@ namespace ServerQueu.Test
{
public class ListenerQueuServerTest
{
int Port = 9999;
int Port = 11129;
string Ip="127.0.0.1";

[Test]
public void Basic()
{
var collection = new System.Collections.Concurrent.ConcurrentQueue<Session>();
var handler=new HandlerSessionListener(ref collection);
var listenerQueuServer = new ListenerQueuServer(Ip,Port,2,handler);

var listenerQueuServer = new ListenerQueuServer(Ip,Port,2);
Assert.IsNull(listenerQueuServer.ThreadListener);

listenerQueuServer.RunThreads();
if (listenerQueuServer.ThreadListener!=null)
{
Assert.AreEqual(ThreadState.Running, listenerQueuServer.ThreadListener.ThreadState);
listenerQueuServer.Close();
}
else
{
Assert.Fail();
}
}

public void BasicFuntion()
[Test]
public void BasicFuntionWithPipe()
{
var listenerQueuServer = new ListenerQueuServer(Ip, Port, 2);
var collection= new System.Collections.Concurrent.ConcurrentQueue<Session>();
var handler=new HandlerSessionListener(ref collection);

var listenerQueuServer = new ListenerQueuServer(Ip, Port, 3,handler);
listenerQueuServer.RunThreads();

var client1 = new TcpClient(Ip, Port);
var client2 = new TcpClient(Ip, Port);

Thread.Sleep(1000);

Assert.AreEqual(1,collection.Count);

if (collection.TryDequeue(out var session))
{
var collectionToRead = new ConcurrentQueue<string>();
var collectionToWrite = new ConcurrentQueue<string>();
var PipeClien = new PipeClients(ref session,ref collectionToRead,ref collectionToWrite);

string mensaje = "HolaMundo";
collectionToWrite.Enqueue(mensaje);
PipeClien.SendMessages();

var streamClien1=client1.GetStream();
var streamClien2=client2.GetStream();

byte[] bufferClien1 = new byte[mensaje.Length];
byte[] bufferClien2 = new byte[mensaje.Length];

streamClien1.Read(bufferClien1, 0, bufferClien1.Length);
streamClien2.Read(bufferClien2,0,bufferClien2.Length);

streamClien1.Write(bufferClien1, 0, bufferClien1.Length);
streamClien2.Write(bufferClien2, 0, bufferClien2.Length);
PipeClien.GetMessages();

Assert.AreEqual(2, collectionToRead.Count);
collectionToRead.TryDequeue(out var mensaje1);
collectionToRead.TryDequeue(out var mensaje2);
Assert.Multiple(() =>
{
Assert.AreEqual(mensaje, Encoding.UTF8.GetString(bufferClien1));
Assert.AreEqual(mensaje, Encoding.UTF8.GetString(bufferClien2));
Assert.AreEqual(mensaje, mensaje1);
Assert.AreEqual(mensaje, mensaje2);
});
listenerQueuServer.Close();
}
else
{
Assert.Fail();
}


}
}
}
21 changes: 15 additions & 6 deletions TresEnRayaApp/ServerQueu.Test/Services/ManagerQueuTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,26 @@ namespace ServerQueu.Test
{
public class ManagerQueuTest
{

private string IP=default!;
private int Port;

[SetUp]
public void SetUp()
{
IP="127.0.0.1";
Port = 8000;
}
[Test]
public void BasicFuntion()
{
var collectionSession = new ConcurrentQueue<Session>();
collectionSession.Enqueue(new Session());
HandlerSessionListener handler=new HandlerSessionListener(ref collectionSession);

var client1 = new TcpClient();
var client2 = new TcpClient();
var session=new Session();
session.AddClient(client1);
session.AddClient(client2);
collectionSession.Enqueue(session);
ListenerQueuServer listenerQueuServer = new ListenerQueuServer(IP,Port,2,handler);
listenerQueuServer.RunThreads();


var managerQueu = new ManagerQueu(ref collectionSession);
Assert.IsTrue(managerQueu.Sessions.Count == 1);
Expand Down
12 changes: 12 additions & 0 deletions TresEnRayaApp/ServerQueu/Factories/FactoryToGames.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ServerQueu.Factories
{
class FactoryToGames
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace TresEnRayaApp
{
public class HandlerSessionListener
{
public readonly ConcurrentQueue<Session> Sessions;
private readonly ConcurrentQueue<Session> Sessions;

private Session? BufferSession=null;

Expand Down
80 changes: 80 additions & 0 deletions TresEnRayaApp/ServerQueu/PipeClients.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using TresEnRayaApp;

namespace ServerQueu
{
public class PipeClients
{
private readonly Session Session;
private readonly ConcurrentQueue<string> LectureToProcess;
private readonly ConcurrentQueue<string> ResponseToClients;
public PipeClients(ref Session session,ref ConcurrentQueue<string> LectureToProcess,ref ConcurrentQueue<string> ResponseToClients)
{
Session = session;
this.LectureToProcess = LectureToProcess;
this.ResponseToClients = ResponseToClients;
}

public void SendMessages()
{
if (ResponseToClients.TryDequeue(out var reponseToSend))
{
if (Session.Client1 != null)
{
byte[] bufferToSend = Encoding.UTF8.GetBytes(reponseToSend);
var stream = Session.Client1.GetStream();
stream.Write(bufferToSend, 0, bufferToSend.Length);

}
if (Session.Client2 != null)
{
byte[] bufferToSend = Encoding.UTF8.GetBytes(reponseToSend);
var stream = Session.Client2.GetStream();
stream.Write(bufferToSend, 0, bufferToSend.Length);

}
}


}
public void GetMessages()
{
if (Session.Client1 != null)
{
int sizeData = Session.Client1.Available;
if (sizeData > 0)
{
byte[] buffer=new byte[sizeData];
var stream = Session.Client1.GetStream();
stream.Read(buffer, 0, buffer.Length);

string mensaje=Encoding.UTF8.GetString(buffer,0,buffer.Length);

LectureToProcess.Enqueue(mensaje);
}
}

if (Session.Client2!=null)
{
int sizeData=Session.Client2.Available;
if (sizeData>0)
{
byte[] buffer=new byte[sizeData];
var stream = Session.Client2.GetStream();
stream.Read(buffer, 0, buffer.Length);

string mensaje = Encoding.UTF8.GetString(buffer, 0, buffer.Length);

LectureToProcess.Enqueue(mensaje);
}
}
}

}
}
4 changes: 0 additions & 4 deletions TresEnRayaApp/ServerQueu/ServerQueu.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,4 @@
<PackageReference Include="System.Collections.Concurrent" Version="4.3.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Factories\" />
</ItemGroup>

</Project>
15 changes: 11 additions & 4 deletions TresEnRayaApp/ServerQueu/Services/GameTask.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -9,11 +10,17 @@ namespace ServerQueu
{
public class GameTask
{
private readonly Session Session;
public GameTask(Session session)
{
public readonly Session Session;
public readonly PipeClients Pipe;
public readonly ConcurrentQueue<string> ToRead=new ConcurrentQueue<string>();
public readonly ConcurrentQueue<string> ToWrite=new ConcurrentQueue<string>();

//Tres en raya para controllar
public GameTask(ref Session session)
{
Session = session;

Pipe= new PipeClients(ref Session,ref ToRead,ref ToWrite);

}

public bool RunTask()
Expand Down
22 changes: 14 additions & 8 deletions TresEnRayaApp/ServerQueu/Services/ListenerQueuServer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
Expand All @@ -12,6 +13,7 @@ public class ListenerQueuServer
private readonly string Ip;
private readonly int Port;
private readonly int Backlog;
private bool Finish;

private TcpListener? _tcpSocketServer = null;
public TcpListener? TcpSocketServer { get { return _tcpSocketServer; } }
Expand All @@ -24,42 +26,46 @@ public Thread? ThreadListener

private HandlerSessionListener? HandlerSessionListener=null;




public ListenerQueuServer(string ip,int port,int backlog)
public ListenerQueuServer(string ip,int port,int backlog, HandlerSessionListener handlerSessionListener)
{
this.Ip = ip;
this.Port = port;
this.Backlog = backlog;
HandlerSessionListener=handlerSessionListener;
}

public void RunThreads()
{
EnabledTcpSocketServer();
EnabledTcpSocketServerToStart();

_threadListener= new Thread(() =>
{
if (_tcpSocketServer != null&&HandlerSessionListener!=null)
{
while (_tcpSocketServer.Server.IsBound)
while (_tcpSocketServer.Server.IsBound&& !Finish)
{
TcpClient tcpClient= _tcpSocketServer.AcceptTcpClient();
HandlerSessionListener.AddClient(tcpClient);
}
_tcpSocketServer.Stop();
}

});

_threadListener.Start();
}
public void Close()
{
Finish=true;
}


private void EnabledTcpSocketServer()
private void EnabledTcpSocketServerToStart()
{
_tcpSocketServer=new TcpListener(System.Net.IPAddress.Parse(Ip),Port);
_tcpSocketServer.Start(Backlog);
var sessionCollection = new System.Collections.Concurrent.ConcurrentQueue<Session>();
HandlerSessionListener = new HandlerSessionListener(ref sessionCollection);
Finish=false;
}

}
Expand Down
2 changes: 1 addition & 1 deletion TresEnRayaApp/ServerQueu/Services/ManagerQueu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public bool RunFirstElement()
//Pendiente de hacerlo como un servicio
if (Sessions.TryDequeue(out var newSession))
{
GameTask gameTask = new GameTask(newSession);
GameTask gameTask = new GameTask(ref newSession);
return gameTask.RunTask();
}

Expand Down
22 changes: 22 additions & 0 deletions TresEnRayaApp/ServerQueu/TresEnRaya.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ServerQueu
{
public struct TresEnRaya
{

public int[,] Board { get; } = new int[3,3];
public bool FinishGame { get; set; }= false;

public TresEnRaya()
{


}

}
}
Binary file modified TresEnRayaApp/ServerQueu/bin/Debug/net6.0/ServerQueu.dll
Binary file not shown.
Binary file modified TresEnRayaApp/ServerQueu/bin/Debug/net6.0/ServerQueu.pdb
Binary file not shown.
Loading