Changed network code
This commit is contained in:
@@ -48,6 +48,16 @@ namespace YJMPD_UWP
|
||||
}
|
||||
}
|
||||
|
||||
private static ApiHandler apihandler = new ApiHandler();
|
||||
|
||||
public static ApiHandler Api
|
||||
{
|
||||
get
|
||||
{
|
||||
return apihandler;
|
||||
}
|
||||
}
|
||||
|
||||
private static GameHandler gamehandler = new GameHandler();
|
||||
|
||||
public static GameHandler Game
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using YJMPD_UWP.Helpers;
|
||||
|
||||
namespace YJMPD_UWP.Model
|
||||
{
|
||||
public class ApiHandler
|
||||
{
|
||||
public delegate void OnGameFoundHandler(object sender, EventArgs e);
|
||||
public event OnGameFoundHandler OnGameFound;
|
||||
|
||||
public enum Command
|
||||
{
|
||||
Hi,
|
||||
Name,
|
||||
Picture,
|
||||
GameFound,
|
||||
PlayerJoined
|
||||
}
|
||||
|
||||
public ApiHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void HandleMessage(JObject o)
|
||||
{
|
||||
Command c = (Command)Enum.Parse(typeof(Command), o["command"].ToString());
|
||||
|
||||
switch(c)
|
||||
{
|
||||
case Command.GameFound:
|
||||
GameFound();
|
||||
break;
|
||||
case Command.PlayerJoined:
|
||||
PlayerJoined(o["msg"].ToString());
|
||||
break;
|
||||
default:
|
||||
//Do nothing
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void PlayerJoined(string username)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
//Event will be handled by the game manager
|
||||
App.Game.AddPlayer(username);
|
||||
}
|
||||
|
||||
private void GameFound()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
if (OnGameFound == null) return;
|
||||
|
||||
OnGameFound(this, new EventArgs());
|
||||
}
|
||||
|
||||
public JObject Message(Command c, string msg)
|
||||
{
|
||||
return JObject.FromObject(new
|
||||
{
|
||||
command = c.ToString(),
|
||||
msg = msg
|
||||
});
|
||||
}
|
||||
|
||||
//API stuff
|
||||
public async Task<bool> SearchGame()
|
||||
{
|
||||
JObject obj = JObject.FromObject(new
|
||||
{
|
||||
command = Command.Name.ToString(),
|
||||
name = Settings.Username
|
||||
});
|
||||
Debug.WriteLine(obj.ToString(Formatting.None));
|
||||
await App.Network.Write(obj.ToString(Formatting.None));
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using Windows.Foundation;
|
||||
using Windows.UI.Xaml.Media.Imaging;
|
||||
using YJMPD_UWP.Helpers;
|
||||
using YJMPD_UWP.Helpers.EventArgs;
|
||||
@@ -77,7 +79,7 @@ namespace YJMPD_UWP.Model
|
||||
public async Task<bool> Search()
|
||||
{
|
||||
UpdateGameStatus(GameStatus.SEARCHING);
|
||||
return await App.Network.SearchGame(this, Settings.Username);
|
||||
return await App.Api.SearchGame();
|
||||
}
|
||||
|
||||
//Starting and Stopping
|
||||
@@ -94,22 +96,8 @@ namespace YJMPD_UWP.Model
|
||||
|
||||
private async Task<bool> StartGame()
|
||||
{
|
||||
|
||||
//Do stuff
|
||||
/*
|
||||
UpdateGameStatus(GameStatus.SEARCHING);
|
||||
Search();
|
||||
|
||||
IAsyncAction asyncAction = Windows.System.Threading.ThreadPool.RunAsync((workItem) =>
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
App.Network.WaitingForPlayers(this);
|
||||
|
||||
Debug.WriteLine("Searching");
|
||||
Task.Delay(TimeSpan.FromMilliseconds(50));
|
||||
}
|
||||
});*/
|
||||
|
||||
|
||||
UpdateGameStatus(GameStatus.WAITING);
|
||||
|
||||
@@ -9,28 +9,23 @@ using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace YJMPD_UWP.Model
|
||||
{
|
||||
public class NetworkHandler
|
||||
{
|
||||
|
||||
public enum Commands
|
||||
{
|
||||
Hi,
|
||||
Name,
|
||||
Picture
|
||||
}
|
||||
|
||||
public delegate void OnStatusUpdatedHandler(object sender, NetworkStatusUpdatedEventArgs e);
|
||||
public event OnStatusUpdatedHandler OnStatusUpdate;
|
||||
|
||||
public enum NetworkStatus { DISCONNECTED, CONNECTING, CONNECTED }
|
||||
public NetworkStatus Status { get; private set; }
|
||||
|
||||
private IAsyncAction BackgroundReader;
|
||||
|
||||
private StreamSocket client;
|
||||
DataWriter dout;
|
||||
StreamReader din;
|
||||
private DataWriter dout;
|
||||
private StreamReader din;
|
||||
|
||||
private void UpdateNetworkStatus(NetworkStatus status)
|
||||
{
|
||||
@@ -44,69 +39,15 @@ namespace YJMPD_UWP.Model
|
||||
public NetworkHandler()
|
||||
{
|
||||
Status = NetworkStatus.DISCONNECTED;
|
||||
OpenConnection();
|
||||
Connect();
|
||||
}
|
||||
|
||||
|
||||
//API stuff
|
||||
public async Task<bool> SearchGame(GameHandler g, string playername)
|
||||
private async Task<string> Read()
|
||||
{
|
||||
JObject obj = JObject.FromObject(new
|
||||
{
|
||||
command = Commands.Name.ToString(),
|
||||
name = playername
|
||||
});
|
||||
Debug.WriteLine(obj.ToString(Formatting.None));
|
||||
Write(obj.ToString(Formatting.None));
|
||||
|
||||
var response = await Read();
|
||||
Debug.WriteLine(response);
|
||||
JObject o = JObject.Parse(response);
|
||||
if(o["msg"].ToString() == "ok")
|
||||
{
|
||||
g.AddPlayer(playername);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public async Task<bool> WaitingForPlayers(GameHandler g)
|
||||
{
|
||||
bool added = false;
|
||||
|
||||
var response = await Read();
|
||||
Debug.WriteLine(response);
|
||||
JObject o = JObject.Parse(response);
|
||||
try {
|
||||
g.AddPlayer(o["player"].ToString());
|
||||
added = true;
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
Debug.WriteLine(e);
|
||||
}
|
||||
return added;
|
||||
}
|
||||
|
||||
|
||||
//Writing and Reading
|
||||
public async Task<string> Read()
|
||||
{
|
||||
return await ReadData();
|
||||
return await din.ReadLineAsync();
|
||||
}
|
||||
|
||||
public async Task<bool> Write(string data)
|
||||
{
|
||||
return await WriteData(data);
|
||||
}
|
||||
|
||||
private async Task<string> ReadData()
|
||||
{
|
||||
return din.ReadLine();
|
||||
}
|
||||
|
||||
private async Task<bool> WriteData(string data)
|
||||
{
|
||||
dout.WriteString(data + Environment.NewLine);
|
||||
await dout.StoreAsync();
|
||||
@@ -117,22 +58,10 @@ namespace YJMPD_UWP.Model
|
||||
|
||||
//Connecting and Disconnecting
|
||||
|
||||
public async Task<bool> Connect()
|
||||
{
|
||||
return await OpenConnection();
|
||||
}
|
||||
|
||||
public async Task<bool> Disconnect()
|
||||
{
|
||||
return await CloseConnection();
|
||||
}
|
||||
|
||||
private async Task<bool> OpenConnection()
|
||||
private async Task<bool> Connect()
|
||||
{
|
||||
UpdateNetworkStatus(NetworkStatus.CONNECTING);
|
||||
|
||||
UpdateNetworkStatus(NetworkStatus.CONNECTED);
|
||||
|
||||
client = new StreamSocket();
|
||||
|
||||
StreamSocketControl controller = client.Control;
|
||||
@@ -150,16 +79,29 @@ namespace YJMPD_UWP.Model
|
||||
dout.UnicodeEncoding = UnicodeEncoding.Utf8;
|
||||
dout.ByteOrder = ByteOrder.LittleEndian;
|
||||
|
||||
|
||||
UpdateNetworkStatus(NetworkStatus.CONNECTED);
|
||||
|
||||
BackgroundReader = Windows.System.Threading.ThreadPool.RunAsync(async (workItem) =>
|
||||
{
|
||||
while (workItem.Status == AsyncStatus.Started)
|
||||
{
|
||||
Debug.WriteLine("Awaiting incoming data...");
|
||||
string data = await App.Network.Read();
|
||||
App.Network.HandleMessage(data);
|
||||
Task.Delay(TimeSpan.FromMilliseconds(50));
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private async Task<bool> CloseConnection()
|
||||
private async Task<bool> Disconnect()
|
||||
{
|
||||
UpdateNetworkStatus(NetworkStatus.DISCONNECTED);
|
||||
|
||||
if (BackgroundReader != null)
|
||||
BackgroundReader.Cancel();
|
||||
|
||||
din.Dispose();
|
||||
dout.Dispose();
|
||||
|
||||
@@ -167,5 +109,15 @@ namespace YJMPD_UWP.Model
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void HandleMessage(string data)
|
||||
{
|
||||
Debug.WriteLine(data);
|
||||
JObject o = JObject.Parse(data);
|
||||
if (o["msg"].ToString() == "ok")
|
||||
{
|
||||
App.Api.HandleMessage(o);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user