static void Main(string[] args) { SQLDataManager.InitSQL(); MainServer server = new MainServer(9000); ConsoleKeyInfo cki; do { cki = Console.ReadKey(); } while (cki.Key != ConsoleKey.Escape); }
User SingUp(NetworkStream?stream, Dictionary <string, object>?myObject) { var data = new Dictionary <string, object>(); data["type"] = (int)Types.TYPE_Get_User; User user = SQLDataManager.CreateNewUser((string)myObject["name"], (string)myObject["email"], (string)myObject["pass"]); data["name"] = (string)myObject["name"]; data["uid"] = user.uId; data["req"] = 1; data["error"] = "null"; Console.WriteLine($"BD New user: {(string)myObject["name"]} :: {user.uId}"); sendDictionaryByJson(stream, data); return(user); }
User LogIn(NetworkStream?stream, Dictionary <string, object>?myObject) { var data = new Dictionary <string, object>(); data["type"] = (int)Types.TYPE_Get_User; User user = SQLDataManager.GetUserData((string)myObject["email"], (string)myObject["pass"]); if (user != null) { data["name"] = user.name; data["uid"] = user.uId; data["req"] = 1; data["error"] = "null"; Console.WriteLine($"BD login user: {user.name} :: {user.uId}"); sendDictionaryByJson(stream, data); } return(user); }
void setUserStatys(string uid, int statys) { SQLDataManager.UdpateStatus(uid, statys); }
private void Channel(object obj) { TcpClient Client = (TcpClient)obj; User user = null; clients.Add(Client); NetworkStream stream = Client.GetStream(); Client.SendBufferSize = DEFPACSIZE; Client.ReceiveBufferSize = DEFPACSIZE; Console.WriteLine($"\n\nnew client: {Client.Client.RemoteEndPoint.ToString()} | Count: {clients.Count}"); try { float timer = IsTimer; while (true) { Byte[] readingData = new Byte[DEFPACSIZE]; int PackSize = 0; if (stream.DataAvailable) { timer = IsTimer; do { PackSize = stream.Read(readingData, 0, readingData.Length); }while (stream.DataAvailable); } if (PackSize > 0) { //Dictionary<string, object> myObject = ByteToDictionary(readingData, PackSize); Dictionary <string, object> myObject = StringJsonToDictionary(readingData, PackSize); Console.WriteLine($"Type: {myObject["type"]}"); switch ((Types)Convert.ToInt32(myObject["type"])) { case Types.test: { if (!SQLDataManager.ValidationByMAC((string)myObject["mac"])) { closeConnect(Client); } } break; case Types.TYPE_LogIn: { user = LogIn(stream, myObject); if (user is null) { closeConnect(Client); return; } } break; case Types.TYPE_SingUp: { user = SingUp(stream, myObject); } break; case Types.TYPE_LogOut: { setUserStatys(user.uId, 0); user = null; closeConnect(Client); } return; case Types.TYPE_CreateRoomS: { CreateNewRoom(stream, IPAddress.Any); } break; case Types.TYPE_NonPack: { string t = myObject["data"].ToString(); Console.WriteLine("Non Dictionary Data: " + t.Substring(0, 11)); byte[] data = Encoding.ASCII.GetBytes("HTTP/1.1 404\n\r\n\r" + File.ReadAllText("Server/WebUI/NotFound.html") .Replace("{users}", "" + (clients.Count - 1)) .Replace("{rooms}", "" + rooms.Count) .Replace("{color}", "red") .Replace("{status}", "User with this MACAddress can't play now") ); if (t.Substring(0, 20).Contains("?mac=")) { string mac = t.Substring(t.IndexOf("=") + 1, t.IndexOf('H') - 11); bool val = SQLDataManager.ValidationByMAC(mac); if (val) { data = Encoding.ASCII.GetBytes("HTTP/1.1 404\n\r\n\r" + File.ReadAllText("Server/WebUI/NotFound.html") .Replace("{users}", "" + (clients.Count - 1)) .Replace("{rooms}", "" + rooms.Count) .Replace("{color}", "green") .Replace("{status}", "User with this MACAddress can play now") ); } Console.WriteLine($"Now check MAC: {mac} answer = {val}"); } stream.Write(data, 0, data.Length); closeConnect(Client); } return; case Types.ADMIN: { switch (myObject["cmd"]) { case "getRooms": { var d = new Dictionary <string, object>(); rooms.ForEach(room => { d.Add("" + room.PORT, room.Rules.MatchState); }); sendDictionaryByJson(stream, d); } break; } } break; } } Thread.Sleep(300); /*timer -= 0.3f; * if (timer <= 0) * { * Console.WriteLine($"Timeout: {IsTimer} sec lost"); * closeConnect(Client); * return; * }*/ } } catch (Exception e) { Console.WriteLine($"Error client: {e.Message}\n{e.StackTrace}"); closeConnect(Client); } }