CloseConnection
This commit is contained in:
parent
757c56c1ea
commit
55151bae11
2 changed files with 44 additions and 12 deletions
|
@ -1,4 +1,5 @@
|
|||
using Microsoft.Xna.Framework.Graphics.PackedVector;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Graphics.PackedVector;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
@ -24,12 +25,13 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
|||
public delegate void OnDataSend(string data);
|
||||
public event OnDataSend GetDataSend; // event
|
||||
Dictionary<Socket, Thread> managerThread;
|
||||
|
||||
|
||||
|
||||
public void Init() //create Socket
|
||||
{
|
||||
endPoint = new IPEndPoint(ip, port);
|
||||
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
managerThread = new Dictionary<Socket, Thread>();
|
||||
}
|
||||
public void SendData() //отправляет клиенту Data
|
||||
{
|
||||
|
@ -45,9 +47,25 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
|||
updates.Add(data);
|
||||
}
|
||||
|
||||
|
||||
public void CloseConnection()
|
||||
{
|
||||
foreach (var item in clients)
|
||||
{
|
||||
item.Shutdown(SocketShutdown.Both);
|
||||
item.Close();
|
||||
}
|
||||
foreach (var item in managerThread)
|
||||
{
|
||||
foreach (var socket in clients)
|
||||
{
|
||||
managerThread[socket].Interrupt();
|
||||
}
|
||||
}
|
||||
managerThread.Clear();
|
||||
clients.Clear();
|
||||
}
|
||||
//Поток 2
|
||||
|
||||
|
||||
public void StartWaitingForPlayers(object players)//Слушает игроков, которые хотят подключиться
|
||||
{
|
||||
int playNumber = (int)players;
|
||||
|
@ -56,19 +74,27 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
|||
for (int i = 0; i < playNumber; i++)
|
||||
{
|
||||
Socket client = socket.Accept();
|
||||
Thread thread = new Thread(StartListening);
|
||||
thread.Start(client);
|
||||
managerThread.Add(client, thread);
|
||||
clients.Add(client); //добавляем клиентов в лист
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void StartListening(object socket)//начать слушать клиентов в самой игре активируют Ивент
|
||||
{
|
||||
// obj to Socket
|
||||
Socket client = (Socket)socket;
|
||||
var buff = new byte[1024];
|
||||
var answ = client.Receive(buff);
|
||||
string response = Encoding.UTF8.GetString(buff, 0, answ);
|
||||
GetDataSend(response);
|
||||
while (client.Connected)
|
||||
{
|
||||
var buff = new byte[1024];
|
||||
var answ = client.Receive(buff);
|
||||
string response = Encoding.UTF8.GetString(buff, 0, answ);
|
||||
GetDataSend(response);
|
||||
}
|
||||
Thread.Sleep(-1);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ using System.Text.Json;
|
|||
using System.Threading.Tasks;
|
||||
using ZoFo.GameCore.GameManagers.NetworkManager;
|
||||
using ZoFo.GameCore.GameManagers.NetworkManager.Updates;
|
||||
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient;
|
||||
using ZoFo.GameCore.GameObjects;
|
||||
|
||||
namespace ZoFo.GameCore
|
||||
|
@ -26,9 +27,14 @@ namespace ZoFo.GameCore
|
|||
List<IUpdateData> updateDatas = JsonSerializer.Deserialize<List<IUpdateData>>(data);
|
||||
//ТУТ Switch case будет честное слово
|
||||
}
|
||||
public void CreateRoom() { }
|
||||
public void StartGame() { }
|
||||
public void EndGame() { }
|
||||
public void CreateRoom(int players) {
|
||||
networkManager.StartWaitingForPlayers(players);
|
||||
}
|
||||
// public void StartGame() { } принудительный запуск
|
||||
public void EndGame() {
|
||||
UpdateGameEnded gameEnded = new UpdateGameEnded();
|
||||
networkManager.AddData(gameEnded);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue