diff --git a/zsirozas/Common.cs b/zsirozas/Common.cs index 1174eeea8d4a8865730c01e9c38fa66ec8b91e51..6082fafae676a8fdd4865a03330a48f454ed4a94 100644 --- a/zsirozas/Common.cs +++ b/zsirozas/Common.cs @@ -18,6 +18,9 @@ namespace zsirozas string CreateUser(string nickname, ConnectionType type, object backTalk); void RemoveUser(string userID); string NewGame(string roomID); + string[] ListRooms(); + //RoomInfo(string roomID); + //UserInfo UserInfo(string userID); void ParseNextMessage(Stream s, object param); } @@ -34,6 +37,7 @@ namespace zsirozas /// </summary> /// <param name="s">a message that will be written to the network stream</param> void SendMessage(string s); + bool ValidateConnection(object connInfo); } //2 féle csatlakozási módot kínálok diff --git a/zsirozas/GameServer.cs b/zsirozas/GameServer.cs index 2d3a55780afd24f03a8b9debc8c83195e277698a..b06f3437fd89c7737339f49c933ae18880cb8c29 100644 --- a/zsirozas/GameServer.cs +++ b/zsirozas/GameServer.cs @@ -3,6 +3,7 @@ using System.IO; using System.Net; using System.Collections.Generic; using System.Net.Sockets; +using System.Web.Script.Serialization; namespace zsirozas { @@ -19,19 +20,7 @@ namespace zsirozas UsersByID = new Dictionary<string, User>(); RoomsByID = new Dictionary<string, Room>(); GamesByID = new Dictionary<string, Game>(); - } - public List<string> Users; - public Dictionary<string, User> UsersByID; - - public List<string> Games; - public Dictionary<string, Game> GamesByID; - public List<string> Rooms; - public Dictionary<string, Room> RoomsByID; - - public int ListenPort = 6500; - public void Loop() - { if (bigRoom) { string Room0 = "RoomZero"; @@ -45,15 +34,41 @@ namespace zsirozas } ); } - var requestQueue = new TcpListener(IPAddress.Any, ListenPort); - requestQueue.Start(); - while (true) - { - var clientRequest = requestQueue.AcceptTcpClient(); - ParseNextMessage(clientRequest.GetStream(), ((IPEndPoint)clientRequest.Client.RemoteEndPoint).Address); - clientRequest.Close(); - } } + public List<string> Users; + public Dictionary<string, User> UsersByID; + + public List<string> Games; + public Dictionary<string, Game> GamesByID; + + public List<string> Rooms; + public Dictionary<string, Room> RoomsByID; + + //public int ListenPort = 6500; + //public void Loop() + //{ + // if (bigRoom) + // { + // string Room0 = "RoomZero"; + // Rooms.Add(Room0); + // RoomsByID.Add( + // Room0, + // new Room() + // { + // roomID = Room0, + // userList = new List<string>() + // } + // ); + // } + // var requestQueue = new TcpListener(IPAddress.Any, ListenPort); + // requestQueue.Start(); + // while (true) + // { + // var clientRequest = requestQueue.AcceptTcpClient(); + // ParseNextMessage(clientRequest.GetStream(), ((IPEndPoint)clientRequest.Client.RemoteEndPoint).Address); + // clientRequest.Close(); + // } + //} public void ParseNextMessage(Stream s, object param) { @@ -126,7 +141,9 @@ namespace zsirozas sw.WriteLine("OK"); break; case ServerAction.ListRooms: - throw new NotImplementedException(); //Not in this version. + sw.WriteLine("OK"); + sw.WriteLine(string.Join("\n", ListRooms())); + break; case ServerAction.Login: line = sr.ReadLine(); try @@ -191,8 +208,14 @@ namespace zsirozas { Console.WriteLine("Server shut down succesfully."); //TODO: leállási feladatok - wrapper.Exit(); - //System.Environment.Exit(0); + if (wrapper != null) + { + wrapper.Exit(); + } + else + { + System.Environment.Exit(0); + } } @@ -210,7 +233,7 @@ namespace zsirozas RoomsByID.Add(r.roomID, r); Rooms.Add(r.roomID); UsersByID[userID].roomID = r.roomID; - Console.WriteLine("[LOG] Room "+r.roomID+" has been created."); + Console.WriteLine("[LOG] Room " + r.roomID + " has been created."); return r.roomID; } @@ -240,7 +263,7 @@ namespace zsirozas //U.msgConn.Connect(IP, port); //U.messageStreamW = new StreamWriter(U.msgConn.GetStream()); - Console.WriteLine("[LOG] " + U.userID + " logged in via " + type + " ( " + backTalk.ToString()+" )"); + Console.WriteLine("[LOG] " + U.userID + " logged in via " + type + " ( " + backTalk.ToString() + " )"); if (bigRoom) { @@ -268,7 +291,7 @@ namespace zsirozas { RoomsByID.Remove(UsersByID[userID].roomID); Rooms.Remove(UsersByID[userID].roomID); - Console.WriteLine("[LOG] Empty room "+UsersByID[userID].roomID+" has been destroyed"); + Console.WriteLine("[LOG] Empty room " + UsersByID[userID].roomID + " has been destroyed"); } UsersByID[userID].roomID = null; } @@ -297,6 +320,11 @@ namespace zsirozas Console.WriteLine("[LOG] New game " + gameID); return gameID; } + + public string[] ListRooms() + { + return Rooms.ToArray(); + } } enum ServerAction @@ -329,6 +357,29 @@ namespace zsirozas //de diagnosztikai célból azért számon tarjuk public ConnectionType connectionType { get; protected set; } } + struct UserInfo + { + public string nickname; + public string userID; + public string roomID; + public bool occupied; + public UserInfo(User user) + { + this.nickname = user.nickname; + this.userID = user.userID; + this.roomID = user.roomID; + this.occupied = user.occupied; + } + public override string ToString() + { + return new JavaScriptSerializer().Serialize(this); + } + public static UserInfo Parse(string s) + { + return new JavaScriptSerializer().Deserialize<UserInfo>(s); + } + } + struct Room { diff --git a/zsirozas/RawTcpTransport.cs b/zsirozas/RawTcpTransport.cs index 2d5dc60b56bd85be3b5bfaa5f885d7d7e3344ad9..1396c4329308299b9cb6d868cdcda1ad7ac6c36d 100644 --- a/zsirozas/RawTcpTransport.cs +++ b/zsirozas/RawTcpTransport.cs @@ -48,6 +48,17 @@ namespace zsirozas } throw new NotImplementedException(); } + + //TODO: eztet robusztusabbra + //TODO: akarom-e strem alapúnál használni bármire? + public bool ValidateConnection(object connInfo) + { + if ((connInfo is IPAddress) &&(IPAddress)connInfo == ((IPEndPoint)msgConn.Client.RemoteEndPoint).Address) + { + return true; + } + return false; + } } } \ No newline at end of file