diff --git a/zsirozas/Common.cs b/zsirozas/Common.cs index d3fa3100da795a7f94a245e420339dc8507e5503..b317a29d1a28555d2b47857fe6bb12458e29294f 100644 --- a/zsirozas/Common.cs +++ b/zsirozas/Common.cs @@ -68,7 +68,7 @@ namespace zsirozas switch (type) { case ConnectionType.SignalR: - //client = new AppClientWithSignalR((string)backTalk); + client = new AppClientWithSignalR((string)backTalk); break; case ConnectionType.RawTCP: client = new AppClientWithTcp((IPEndPoint)backTalk); diff --git a/zsirozas/ServerWrapper.cs b/zsirozas/ServerWrapper.cs index e962fa5a3ab0654ae4096805508ce6dec051295f..303831909f34b9f719da925be4970bff2bdeb3d5 100644 --- a/zsirozas/ServerWrapper.cs +++ b/zsirozas/ServerWrapper.cs @@ -2,6 +2,8 @@ using System.Net; using System.Net.Sockets; using System.Threading; +using Microsoft.AspNet.SignalR; +using Owin; namespace zsirozas { @@ -87,24 +89,23 @@ namespace zsirozas public int tcp_listening_port = 6500; private TcpListener requestQueue; - ////............................................................................................... - //public void stopSignalR() - //{ - // if (webapp != null) - // { - // webapp.Dispose(); - // webapp = null; - // } - //} - //public void startSignalR() - //{ + //............................................................................................... + public void stopSignalR() + { + if (webapp != null) + { + webapp.Dispose(); + webapp = null; + } + } + public void startSignalR() + { - // webapp = WebApp.Start(signalr_server_url, Configuration); - // SignalRServerHub.appServer = serverInstance; - // GlobalHost.DependencyResolver.Register(typeof(SignalRServerHub), () => new SignalRServerHub(serverInstance)); - //} - //private IDisposable webapp; - //private string signalr_server_url = "http://localhost:9080"; - //private void Configuration(IAppBuilder app) { app.MapSignalR(); } + webapp = WebApp.Start(signalr_server_url, Configuration); + GlobalHost.DependencyResolver.Register(typeof(SignalRServerHub), () => new SignalRServerHub(serverInstance)); + } + private IDisposable webapp; + private string signalr_server_url = "http://localhost:9080"; + private void Configuration(IAppBuilder app) { app.MapSignalR(); } } } \ No newline at end of file diff --git a/zsirozas/SignalR-Transport.cs b/zsirozas/SignalR-Transport.cs index a97584d15950e8b4364d8ac633d17d814a88d34f..b8ce9f2cb174ad8695726293308893d45ad3c852 100644 --- a/zsirozas/SignalR-Transport.cs +++ b/zsirozas/SignalR-Transport.cs @@ -1,109 +1,390 @@ using System; +using System.IO; using System.Threading.Tasks; using Microsoft.AspNet.SignalR; //és a klienshez using Microsoft.AspNet.SignalR.Client; using Microsoft.AspNet.SignalR.Client.Transports; +using WrappedException = Microsoft.AspNet.SignalR.HubException; + namespace zsirozas { //............................................................ class SignalRRemoteServer : IServerApiProvider { + IAppClient client; + public SignalRRemoteServer(string server_url, IAppClient client) + { + this.client = client; + hub = new HubConnection(server_url).CreateHubProxy(nameof(SignalRServerHub)); + //proxy calls from srver + hub.On<ServerEvent, string>(nameof(client.SimpleNotify), client.SimpleNotify); + hub.On<GameEvent, string>(nameof(client.InGameNotify), client.InGameNotify); + } + + public InvalidActionException UnwrapException(WrappedException ex) + { + return new InvalidActionException(ex.Message); + } + + // + // Server API + // + IHubProxy hub; public string CreateUser(string name, ConnectionType type, object backTalk) { - return hub.Invoke<string>(nameof(CreateUser), name, type, backTalk).Result; + try + { + return hub.Invoke<string>(nameof(CreateUser), name, type, backTalk).Result; + } + catch (WrappedException ex) + { + throw UnwrapException(ex); + } } public string CreateRoom(string userID) { - return hub.Invoke<string>(nameof(CreateRoom), userID).Result; + try + { + return hub.Invoke<string>(nameof(CreateRoom), userID).Result; + } + catch (WrappedException ex) + { + throw UnwrapException(ex); + } } public void LeaveRoom(string userID) { - hub.Invoke(nameof(LeaveRoom), userID).Wait(); + try + { + hub.Invoke(nameof(LeaveRoom), userID).Wait(); + } + catch (WrappedException ex) + { + throw UnwrapException(ex); + } } public void JoinRoom(string userID, string roomID) { - hub.Invoke(nameof(JoinRoom), roomID).Wait(); + try + { + hub.Invoke(nameof(JoinRoom), roomID).Wait(); + } + catch (WrappedException ex) + { + throw UnwrapException(ex); + } } public void RemoveUser(string userID) { - hub.Invoke(nameof(RemoveUser), userID).Wait(); + try + { + hub.Invoke(nameof(RemoveUser), userID).Wait(); + } + catch (WrappedException ex) + { + throw UnwrapException(ex); + } } public string NewGame(string roomID) { - return hub.Invoke<string>(nameof(NewGame), roomID).Result; + try + { + return hub.Invoke<string>(nameof(NewGame), roomID).Result; + } + catch (WrappedException ex) + { + throw UnwrapException(ex); + } + } + + + public void RegisterWrapper(ServerWrapper _wrapper) + { + return; } - IAppClient client; - public SignalRRemoteServer(string server_url) + public string[] ListRooms() { - hub = new HubConnection(server_url).CreateHubProxy(nameof(SignalRServerHub)); + try + { + return hub.Invoke<string[]>(nameof(ListRooms)).Result; + } + catch (WrappedException ex) + { + throw UnwrapException(ex); + } + } + + public string[] ListUsers() + { + try + { + return hub.Invoke<string[]>(nameof(ListUsers)).Result; + } + catch (WrappedException ex) + { + throw UnwrapException(ex); + } + } + + public ServerInfo ServerInfo() + { + try + { + return hub.Invoke<ServerInfo>(nameof(ServerInfo)).Result; + } + catch (WrappedException ex) + { + throw UnwrapException(ex); + } + } + + public RoomInfo RoomInfo(string roomID) + { + try + { + return hub.Invoke<RoomInfo>(nameof(RoomInfo), roomID).Result; + } + catch (WrappedException ex) + { + throw UnwrapException(ex); + } + } - hub.On<ServerEvent, object>(nameof(client.SimpleNotify), client.SimpleNotify); - hub.On<GameEvent, object>(nameof(client.InGameNotify), client.InGameNotify); + public UserInfo UserInfo(string userID) + { + try + { + return hub.Invoke<UserInfo>(nameof(UserInfo), userID).Result; + } + catch (WrappedException ex) + { + throw UnwrapException(ex); + } + } + + public void ParseNextMessage(Stream s, object param) + { + throw new NotImplementedException(); + } + + public object GameAction(string gameID, string userID, PlayerAction action, int cardID) + { + try + { + return hub.Invoke<object>(nameof(GameAction), userID, action, cardID).Result; + } + catch (WrappedException ex) + { + throw UnwrapException(ex); + } } } + //................................................................................ class SignalRServerHub : Hub<IAppClient>, IServerApiProvider { - //a hubok tranziesnek, úgyhogy ezt csak így tudom értelmesen - //TODO: van-e ennek "szebb" módja? - public static IServerApiProvider appServer = new ActualServer(); + //let dependency injection handle it + public IServerApiProvider appServer; + public SignalRServerHub(IServerApiProvider serverInstance) { appServer = serverInstance; } + + public void RegisterWrapper(ServerWrapper _wrapper) + { + throw new WrappedException("Why would you try to do something like this?"); + } + + + /// <summary> + /// Creates a Microsoft.AspNet.SignalR.HubException with the same message. + /// Use this to get messages across since SignalR omits them by default. + /// </summary> + public WrappedException WrapException(Exception ex) + { + return new Microsoft.AspNet.SignalR.HubException(ex.Message); + } + public string CreateUser(string name, ConnectionType type, object backTalk) { switch (type) { case ConnectionType.SignalR: if (backTalk != null) goto default; - backTalk = Context.ConnectionId; break; case ConnectionType.RawTCP: - throw new Exception("What? Why?"); + throw new WrappedException("What? Why?"); default: - throw new NotSupportedException("You can't do that"); + throw new WrappedException("You can't do that"); + } + try + { + return appServer.CreateUser(name, type, backTalk); + } + catch (InvalidActionException ex) + { + throw WrapException(ex); } - return appServer.CreateUser(name, type, backTalk); } public string CreateRoom(string userID) { - return appServer.CreateRoom(userID); + try + { + return appServer.CreateRoom(userID); + } + catch (InvalidActionException ex) + { + throw WrapException(ex); + } } public void LeaveRoom(string userID) - { - appServer.LeaveRoom(userID); + { + try + { + appServer.LeaveRoom(userID); + } + catch (InvalidActionException ex) + { + throw WrapException(ex); + } } public void JoinRoom(string userID, string roomID) { - appServer.JoinRoom(userID, roomID); + try + { + appServer.JoinRoom(userID, roomID); + } + catch (InvalidActionException ex) + { + throw WrapException(ex); + } } public void RemoveUser(string userID) { - appServer.RemoveUser(userID); + try + { + appServer.RemoveUser(userID); + } + catch (InvalidActionException ex) + { + throw WrapException(ex); + } } public string NewGame(string roomID) { - return appServer.NewGame(roomID); + try + { + return appServer.NewGame(roomID); + } + catch (InvalidActionException ex) + { + throw WrapException(ex); + } + } + + public string[] ListRooms() + { + try + { + return appServer.ListRooms(); + } + catch (InvalidActionException ex) + { + throw WrapException(ex); + } + } + + public string[] ListUsers() + { + try + { + return appServer.ListUsers(); + } + catch (InvalidActionException ex) + { + throw WrapException(ex); + } + } + + public ServerInfo ServerInfo() + { + try + { + return appServer.ServerInfo(); + } + catch (InvalidActionException ex) + { + throw WrapException(ex); + } + } + + public RoomInfo RoomInfo(string roomID) + { + try + { + return appServer.RoomInfo(roomID); + } + catch (InvalidActionException ex) + { + throw WrapException(ex); + } + } + + public UserInfo UserInfo(string userID) + { + try + { + return appServer.UserInfo(userID); + } + catch (InvalidActionException ex) + { + throw WrapException(ex); + } + } + + public void ParseNextMessage(Stream s, object param) + { + try + { + appServer.ParseNextMessage(s, param); + } + catch (InvalidActionException ex) + { + throw WrapException(ex); + } + } + + public object GameAction(string gameID, string userID, PlayerAction action, int cardID) + { + try + { + return appServer.GameAction(gameID, userID, action, cardID); + } + catch (InvalidActionException ex) + { + throw WrapException(ex); + } } + } class AppClientWithSignalR : IAppClient { @@ -114,17 +395,34 @@ namespace zsirozas this.signalRConnID = backTalk; } - public void InGameNotify(GameEvent @event, object param0) + public void InGameNotify(GameEvent @event, string param0) { var context = GlobalHost.ConnectionManager.GetHubContext<SignalRServerHub>(); (context as IAppClient).InGameNotify(@event, param0); } - public void SimpleNotify(ServerEvent @event, object param0) + + public void ParseNextMessage(StreamReader sr) + { + //todo implemtálni valahogy?? + throw new NotImplementedException(); + } + + public void SendMessage(string s) + { + throw new NotImplementedException(); + } + + public void SimpleNotify(ServerEvent @event, string param0) { var context = GlobalHost.ConnectionManager.GetHubContext<SignalRServerHub>(); (context as IAppClient).SimpleNotify(@event, param0); } + + public bool ValidateConnection(object connInfo) + { + return this.signalRConnID == (connInfo as string); + } } } \ No newline at end of file