Merge branch 'NetworkManagerDev' into Development
This commit is contained in:
commit
86c73093c5
4 changed files with 107 additions and 28 deletions
|
@ -1,9 +1,25 @@
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text.Json;
|
||||||
|
using ZoFo.GameCore.GameManagers.NetworkManager;
|
||||||
|
using ZoFo.GameCore.GameManagers.NetworkManager.Updates;
|
||||||
|
|
||||||
namespace ZoFo.GameCore
|
namespace ZoFo.GameCore
|
||||||
{
|
{
|
||||||
public class Client
|
public class Client
|
||||||
{
|
{
|
||||||
public void OnDataSend(string Data){ }
|
ClientNetworkManager networkManager;
|
||||||
|
public Client()
|
||||||
|
{
|
||||||
|
networkManager = new ClientNetworkManager();
|
||||||
|
networkManager.GetDataSent += OnDataSend;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnDataSend(string data)
|
||||||
|
{
|
||||||
|
List<IUpdateData> updateDatas = JsonSerializer.Deserialize<List<IUpdateData>>(data);
|
||||||
|
// Тут будет switch
|
||||||
|
}
|
||||||
|
|
||||||
public void GameEndedUnexpectedly(){ }
|
public void GameEndedUnexpectedly(){ }
|
||||||
|
|
||||||
|
|
|
@ -15,8 +15,8 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
||||||
private int port = 7632;
|
private int port = 7632;
|
||||||
private EndPoint endPoint;
|
private EndPoint endPoint;
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
delegate void OnDataSent(string Data);
|
public delegate void OnDataSent(string Data);
|
||||||
event OnDataSent GetDataSent; // event
|
public event OnDataSent GetDataSent; // event
|
||||||
public void Init() //create endPoint, socket
|
public void Init() //create endPoint, socket
|
||||||
{
|
{
|
||||||
endPoint = new IPEndPoint(iPAddress, port);
|
endPoint = new IPEndPoint(iPAddress, port);
|
||||||
|
@ -28,6 +28,12 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void StopConnection()
|
||||||
|
{
|
||||||
|
socket.Shutdown(SocketShutdown.Both);
|
||||||
|
socket.Close();
|
||||||
|
}
|
||||||
|
|
||||||
public void JoinRoom() // multyplayer
|
public void JoinRoom() // multyplayer
|
||||||
{
|
{
|
||||||
SendData();
|
SendData();
|
||||||
|
@ -45,11 +51,13 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
||||||
{
|
{
|
||||||
socket.Connect(endPoint);
|
socket.Connect(endPoint);
|
||||||
|
|
||||||
|
while(socket.Connected)
|
||||||
|
{
|
||||||
byte[] bytes = new byte[2048];
|
byte[] bytes = new byte[2048];
|
||||||
|
|
||||||
var countAnsw = socket.Receive(bytes);
|
var countAnsw = socket.Receive(bytes);
|
||||||
|
string update = Encoding.UTF8.GetString(bytes, 0, countAnsw); // обновление отосланные сервером
|
||||||
string updates = Encoding.UTF8.GetString(bytes, 0, countAnsw); // обновления отосланные сервером
|
GetDataSent(update);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
@ -21,44 +22,79 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
private List<Socket> clients;
|
private List<Socket> clients;
|
||||||
private List<IUpdateData> updates;
|
private List<IUpdateData> updates;
|
||||||
delegate void OnDataSend(string data); //
|
public delegate void OnDataSend(string data);
|
||||||
|
public event OnDataSend GetDataSend; // event
|
||||||
|
Dictionary<Socket, Thread> managerThread;
|
||||||
|
|
||||||
|
|
||||||
public void Init() //create Socket
|
public void Init() //create Socket
|
||||||
{
|
{
|
||||||
endPoint = new IPEndPoint(ip, port);
|
endPoint = new IPEndPoint(ip, port);
|
||||||
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||||
|
managerThread = new Dictionary<Socket, Thread>();
|
||||||
}
|
}
|
||||||
|
public void SendData() //отправляет клиенту Data
|
||||||
public void SendData()
|
|
||||||
{
|
{
|
||||||
|
string data = JsonSerializer.Serialize(updates);
|
||||||
|
var databytes = Encoding.UTF8.GetBytes(data);
|
||||||
|
foreach (var item in clients)
|
||||||
|
{
|
||||||
|
item.SendAsync(databytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void AddData(IUpdateData data)//добавляет в лист updates новую data
|
||||||
|
{
|
||||||
|
updates.Add(data);
|
||||||
}
|
}
|
||||||
public void AddData(IUpdateData 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
|
//Поток 2
|
||||||
public void StartWaitingForPlayers()//Слушает игроков, которые хотят подключиться
|
|
||||||
|
public void StartWaitingForPlayers(object players)//Слушает игроков, которые хотят подключиться
|
||||||
{
|
{
|
||||||
|
int playNumber = (int)players;
|
||||||
socket.Bind(endPoint);
|
socket.Bind(endPoint);
|
||||||
socket.Listen(10);
|
socket.Listen(playNumber);
|
||||||
for (int i = 0; i < 10; i++)
|
for (int i = 0; i < playNumber; i++)
|
||||||
{
|
{
|
||||||
Socket client = socket.Accept();
|
Socket client = socket.Accept();
|
||||||
|
Thread thread = new Thread(StartListening);
|
||||||
|
thread.Start(client);
|
||||||
|
managerThread.Add(client, thread);
|
||||||
clients.Add(client); //добавляем клиентов в лист
|
clients.Add(client); //добавляем клиентов в лист
|
||||||
}
|
}
|
||||||
|
|
||||||
StartListening();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StartListening()//начать слушать клиентов в самой игре активируют Ивент
|
private void StartListening(object socket)//начать слушать клиентов в самой игре активируют Ивент
|
||||||
|
{
|
||||||
|
// obj to Socket
|
||||||
|
Socket client = (Socket)socket;
|
||||||
|
while (client.Connected)
|
||||||
{
|
{
|
||||||
var buff = new byte[1024];
|
var buff = new byte[1024];
|
||||||
foreach (var client in clients)
|
|
||||||
{
|
|
||||||
var answ = client.Receive(buff);
|
var answ = client.Receive(buff);
|
||||||
string response = Encoding.UTF8.GetString(buff, 0, answ);
|
string response = Encoding.UTF8.GetString(buff, 0, answ);
|
||||||
// List<IUpdateData> updateDatas =
|
GetDataSend(response);
|
||||||
}
|
}
|
||||||
|
Thread.Sleep(-1);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,11 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
using System.Threading.Tasks;
|
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;
|
using ZoFo.GameCore.GameObjects;
|
||||||
|
|
||||||
namespace ZoFo.GameCore
|
namespace ZoFo.GameCore
|
||||||
|
@ -10,12 +14,27 @@ namespace ZoFo.GameCore
|
||||||
public class Server
|
public class Server
|
||||||
{
|
{
|
||||||
private List<GameObject> gameObjects;
|
private List<GameObject> gameObjects;
|
||||||
|
private ServerNetworkManager networkManager;
|
||||||
// private List<> entity; //entity
|
// private List<> entity; //entity
|
||||||
|
public Server()
|
||||||
|
{
|
||||||
|
networkManager = new ServerNetworkManager();
|
||||||
|
networkManager.GetDataSend += OnDataSend;
|
||||||
|
}
|
||||||
|
|
||||||
public void OnDataSend(string data) { }
|
public void OnDataSend(string data)
|
||||||
public void CreateRoom() { }
|
{
|
||||||
public void StartGame() { }
|
List<IUpdateData> updateDatas = JsonSerializer.Deserialize<List<IUpdateData>>(data);
|
||||||
public void EndGame() { }
|
//ТУТ Switch case будет честное слово
|
||||||
|
}
|
||||||
|
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