public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { if (!string.IsNullOrEmpty(ch.UserName)) { User u = Database.getInstance().getUser(ch.UserName); if (u != null) { if (Server.userList.ContainsKey(ch.UserName)) { Server.userList[ch.UserName].Level = u.Level; Server.userList[ch.UserName].TotalScore = u.TotalScore; Server.userList[ch.UserName].Playcount = u.Playcount; Server.userList[ch.UserName].Accuracy = u.Accuracy; } else { throw new Exception(); } } else { throw new Exception(); } } return null; }
public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { string msg = br.ReadString(); Console.WriteLine(msg); Logger.log(msg); return null; }
public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { List<object> l = new List<object>(); l.Add(br.ReadInt32()); l.Add(br.ReadString()); return new RecievePacket(l); }
public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { List<object> l = new List<object>(); //name of person to cancel, easier this way l.Add(br.ReadString()); return new RecievePacket(l); }
public static void sendSpectateFinish(ClientHandler h, int score, int combo, int per, int gr, int ok, int miss, int flags, double acc) { try { if (h != null && h.Bw != null) { h.Bw.Write((short)SendHeaders.SPECTATE_FINISH); h.Bw.Write(score); h.Bw.Write(combo); h.Bw.Write(per); h.Bw.Write(gr); h.Bw.Write(ok); h.Bw.Write(miss); h.Bw.Write(flags); h.Bw.Write(acc); } else { h.abort(); } } catch (Exception e) { h.abort(e); } }
public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { double ver = br.ReadDouble(); Console.WriteLine("Client with ver " + ver + " requesting version check"); bw.Write((short)SendHeaders.VERSION_CHECK); bw.Write(Server.getVersion()); //may need to change in the future when there is alot of traffic (so not reading every time) return null; }
public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { List<object> l = new List<object>(); //offset, score, combo, health l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); l.Add(br.ReadDouble()); return new RecievePacket(l); }
public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { List<object> l = new List<object>(); //score, maxcombo, perfect, great, ok, miss, modflags l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); l.Add(br.ReadDouble()); return new RecievePacket(l); }
public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { string user = br.ReadString(); string chartMd5 = br.ReadString(); string song = br.ReadString(); int type = br.ReadInt16(); List<object> l = new List<object>(); l.Add(user.ToLower()); l.Add(chartMd5); l.Add(song); l.Add(type); //mod flags, scroll, speed l.Add(br.ReadInt32()); l.Add(br.ReadDouble()); l.Add(br.ReadDouble()); return new RecievePacket(l); }
public static void sendSpectateFail(ClientHandler h) { try { if (h != null && h.Bw != null) { h.Bw.Write((short)SendHeaders.SPECTATE_FAIL); } else { h.abort(); } } catch (Exception e) { h.abort(e); } }
public static void endSpectateRecord(ClientHandler h) { try { if (h != null & h.Bw != null) { h.Bw.Write((short)SendHeaders.SPECTATE_END); } else { h.abort(); } } catch (Exception e) { h.abort(e); } }
public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { string user = br.ReadString(); string pass = br.ReadString(); //hashed bw.Write((short)SendHeaders.LOGIN_AUTH); //login_auth Database db = Database.getInstance(); Session s = db.login(user, pass); if (s != null) { if (Server.userList.ContainsKey(user)) { Server.userList[user].Handler.abort(); } bw.Write((byte)constants.LOGIN_SUCCESS); string avi = s.getAvatarUrl(); bw.Write(avi); bw.Write(pass); User u = db.getUser(user); if (!string.IsNullOrEmpty(ch.UserName)) { Server.userList.Remove(ch.UserName); } Server.userList.Add(user.ToLower(), u); List<object> l = new List<object>(); l.Add(u); bw.Write(user); return new RecievePacket(l); } else { bw.Write((byte)constants.LOGIN_FAILED); } bw.Write(user); return null; }
public static void listen() { Console.WriteLine("Server running version " + getVersion() + ". Starting server on port " + port); TcpListener listener = new TcpListener(port); registerHandlers(); Console.WriteLine("registered {0} handlers", handlers.Count); listener.Start(); new Thread(new ThreadStart(input)).Start(); while (run) //multicliented through use of multiple threads, but should still be somewhat efficient as read() is a blocking method so most threads will be blocked { TcpClient c = listener.AcceptTcpClient(); Console.WriteLine("Client connected at {0}", (c.Client.RemoteEndPoint as IPEndPoint).Address.ToString()); ClientHandler ch = new ClientHandler(c); Thread t = new Thread(new ThreadStart(ch.Run)); ch.myThread = t; t.Start(); } }
public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { string user = br.ReadString(); string pass = br.ReadString(); //hashed bw.Write((short)SendHeaders.LOGIN_AUTH); //login_auth Database db = Database.getInstance(); Session s = db.login(user, pass); if (s != null) { if (Server.userList.ContainsKey(user)) { Server.userList[user].Handler.abort(); } bw.Write((byte)constants.LOGIN_SUCCESS); string avi = s.getAvatarUrl(); bw.Write(avi); bw.Write(pass); User u = db.getUser(user); if (!string.IsNullOrEmpty(ch.UserName)) { Server.userList.Remove(ch.UserName); } Server.userList.Add(user.ToLower(), u); List <object> l = new List <object>(); l.Add(u); bw.Write(user); return(new RecievePacket(l)); } else { bw.Write((byte)constants.LOGIN_FAILED); } bw.Write(user); return(null); }
public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { string msg = br.ReadString(); Console.WriteLine(msg); Logger.log(msg); return(null); }
public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { if (!string.IsNullOrEmpty(ch.UserName)) { User u = Database.getInstance().getUser(ch.UserName); if (u != null) { if (Server.userList.ContainsKey(ch.UserName)) { Server.userList[ch.UserName].Level = u.Level; Server.userList[ch.UserName].TotalScore = u.TotalScore; Server.userList[ch.UserName].Playcount = u.Playcount; Server.userList[ch.UserName].Accuracy = u.Accuracy; } else { throw new Exception(); } } else { throw new Exception(); } } return(null); }
public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { string[] users = br.ReadString().ToLower().Split(';'); foreach (string s in users) { //Console.WriteLine(s); if (!string.IsNullOrEmpty(s)) { if (Server.userList.ContainsKey(s)) { // User u = Database.getInstance().getUser(s); //refresh user info if (Server.userList[s] != null) { PacketWriter.sendUserInfo(ch, Server.userList[s], s); } else { Console.WriteLine("user was null"); } } else { Console.WriteLine("server had no key " + s + " in userlist"); PacketWriter.sendUserInfo(ch, null, s); } } } return null; }
public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { List<object> l = new List<object>(); //offset, difference, lane, type of hit l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); return new RecievePacket(l); }
public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { string user = br.ReadString(); string chartMd5 = br.ReadString(); string song = br.ReadString(); int type = br.ReadInt16(); List <object> l = new List <object>(); l.Add(user.ToLower()); l.Add(chartMd5); l.Add(song); l.Add(type); //mod flags, scroll, speed l.Add(br.ReadInt32()); l.Add(br.ReadDouble()); l.Add(br.ReadDouble()); return(new RecievePacket(l)); }
public void spectate(ClientHandler h) { if (!specs.ContainsKey(h.userName)) { specs.Add(h.userName, h); specChart.Add(h.userName, false); if (specs.Count == 1) { PacketWriter.sendSpectateRecord(this); } sendSpecs(); } }
public static void sendSpectateStart(ClientHandler h, string user, string md5, int mods, double scroll) { try { if (h != null && h.Bw != null) { h.Bw.Write((short)SendHeaders.SPECTATE_START); h.Bw.Write(user); h.Bw.Write(md5); h.Bw.Write(mods); h.Bw.Write(scroll); } else { h.abort(); } } catch (Exception e) { h.abort(e); } }
public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { List <object> l = new List <object>(); //name of person to cancel, easier this way l.Add(br.ReadString()); return(new RecievePacket(l)); }
public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { List <object> l = new List <object>(); //score, maxcombo, perfect, great, ok, miss, modflags l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); l.Add(br.ReadDouble()); return(new RecievePacket(l)); }
public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { return(null); }
public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { double ver = br.ReadDouble(); Console.WriteLine("Client with ver " + ver + " requesting version check"); bw.Write((short)SendHeaders.VERSION_CHECK); bw.Write(Server.getVersion()); //may need to change in the future when there is alot of traffic (so not reading every time) return(null); }
public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { List <object> l = new List <object>(); l.Add(br.ReadInt32()); l.Add(br.ReadString()); return(new RecievePacket(l)); }
public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { return null; }
public static void sendSpectateUsersMe(ClientHandler h, string list) { try { if (h != null && h.Bw != null) { h.Bw.Write((short)SendHeaders.SPECTATE_USERS_ME); h.Bw.Write(list); } else { h.abort(); } } catch (Exception e) { h.abort(e); } }
public void unspectate(ClientHandler h) { if (specs.ContainsKey(h.userName)) { specs.Remove(h.userName); if (specs.Count == 0) { PacketWriter.endSpectateRecord(this); } else { sendSpecs(); } } }
public static void sendUserInfo(ClientHandler ch, User u, string usr) { try { BinaryWriter Bw = ch.Bw; if (ch != null && Bw != null) { Bw.Write((short)SendHeaders.USER_REQUEST_INFO); //Console.WriteLine(u.Name); //online bool, name,realname,,avatar,playcount,totalscore,mode,currentsong,currentchart, accuracy, level if (u == null) { Bw.Write(false); Bw.Write(usr); } else { Bw.Write(true); Bw.Write(u.Name); Bw.Write(u.RealName); Bw.Write(u.Avatar); Bw.Write(u.Playcount); Bw.Write(u.TotalScore); Bw.Write((int)u.Mode); Bw.Write(u.CurrentSong); Bw.Write(u.CurrentChart); Bw.Write(u.Accuracy); Bw.Write(u.Level); } } else { ch.abort(); } } catch (Exception e) { ch.abort(e); } }
public static void sendSpectateHeartbeat(ClientHandler h, int off, int score, int combo, int hp, double acc) { try { if (h != null && h.Bw != null) { h.Bw.Write((short)SendHeaders.SPECTATE_HEARTBEAT); h.Bw.Write(off); h.Bw.Write(score); h.Bw.Write(combo); h.Bw.Write(hp); h.Bw.Write(acc); } else { h.abort(); } } catch (Exception e) { h.abort(e); } }
public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { string[] users = br.ReadString().ToLower().Split(';'); foreach (string s in users) { //Console.WriteLine(s); if (!string.IsNullOrEmpty(s)) { if (Server.userList.ContainsKey(s)) { // User u = Database.getInstance().getUser(s); //refresh user info if (Server.userList[s] != null) { PacketWriter.sendUserInfo(ch, Server.userList[s], s); } else { Console.WriteLine("user was null"); } } else { Console.WriteLine("server had no key " + s + " in userlist"); PacketWriter.sendUserInfo(ch, null, s); } } } return(null); }
public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { List <object> l = new List <object>(); //offset, difference, lane, type of hit l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); return(new RecievePacket(l)); }
public static void sendSpectateHit(ClientHandler h, int off, int diff, int lane, int type) { try { if (h != null && h.Bw != null) { h.Bw.Write((short)SendHeaders.SPECTATE_HIT); h.Bw.Write(off); h.Bw.Write(diff); h.Bw.Write(lane); h.Bw.Write(type); } else { h.abort(); } } catch (Exception e) { h.abort(e); } }
public RecievePacket handleData(BinaryReader br, BinaryWriter bw, TcpClient client, ClientHandler ch) { List <object> l = new List <object>(); //offset, score, combo, health l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); l.Add(br.ReadInt32()); l.Add(br.ReadDouble()); return(new RecievePacket(l)); }
public static void sendSpectateRelease(ClientHandler h, int off, int lane) { try { if (h != null && h.Bw != null) { h.Bw.Write((short)SendHeaders.SPECTATE_RELEASE); h.Bw.Write(off); h.Bw.Write(lane); } else { h.abort(); } } catch (Exception e) { h.abort(e); } }
public void handlePacket(short header, RecievePacket packet) { if (header == (short)RecieveHeaders.SONG_START) { User u = Server.userList[userName]; if (u.Name.Equals((string)packet.info[0])) { u.CurrentChart = (string)packet.info[1]; u.CurrentSong = (string)packet.info[2]; u.Mode = (User.PlayMode)packet.info[3]; u.ModFlags = (int)packet.info[4]; u.Scroll = (double)packet.info[5]; u.Speed = (double)packet.info[6]; } if (specs.Count > 0 && !u.CurrentChart.Equals("") && (u.Mode == User.PlayMode.INGAME || u.Mode == User.PlayMode.MULTI)) { foreach (KeyValuePair <string, ClientHandler> h in specs) { PacketWriter.sendSpectateStart(h.Value, userName, Server.userList[userName].CurrentChart, Server.userList[userName].ModFlags, Server.userList[userName].Scroll); specChart[h.Key] = false; } } } else if (header == (short)RecieveHeaders.LOGIN && packet != null) { User temp = (User)packet.info[0]; userName = temp.Name.ToLower(); //tolower unnecessary, but more explicit that userName is lowercase temp.Handler = this; myThread.Name = userName; } //should have some sort of packet converter that converts packet to some subclass with typed fields parsed from the list that's properly named to increase readability else if (header == (short)RecieveHeaders.SPECTATE_HOOK) { if (Server.userList.ContainsKey((string)packet.info[0])) { try { if (speccing != null) { speccing.unspectate(this); } } catch { } Server.userList[(string)packet.info[0]].Handler.spectate(this); if (!Server.userList[(string)packet.info[0]].CurrentChart.Equals("")) { PacketWriter.sendSpectateStart(this, (string)packet.info[0], Server.userList[(string)packet.info[0]].CurrentChart, Server.userList[(string)packet.info[0]].ModFlags, Server.userList[(string)packet.info[0]].Scroll); } speccing = Server.userList[(string)packet.info[0]].Handler; } } else if (header == (short)RecieveHeaders.SPECTATE_HIT) { foreach (KeyValuePair <string, ClientHandler> h in specs) { PacketWriter.sendHit(h.Value, (int)packet.info[0], (int)packet.info[1], (int)packet.info[2], (int)packet.info[3]); } } else if (header == (short)RecieveHeaders.SPECTATE_RELEASE) { foreach (KeyValuePair <string, ClientHandler> h in specs) { PacketWriter.sendSpectateRelease(h.Value, (int)packet.info[0], (int)packet.info[1]); } } else if (header == (short)RecieveHeaders.SPECTATE_CANCEL) { if (Server.userList.ContainsKey((string)packet.info[0])) { Server.userList[(string)packet.info[0]].Handler.unspectate(this); speccing = null; } } else if (header == (short)RecieveHeaders.SPECTATE_PRESS) { foreach (KeyValuePair <string, ClientHandler> h in specs) { PacketWriter.sendSpectatePress(h.Value, (int)packet.info[0], (int)packet.info[1]); } } else if (header == (short)RecieveHeaders.SPECTATE_HEARTBEAT) { foreach (KeyValuePair <string, ClientHandler> h in specs) { PacketWriter.sendSpectateHeartbeat(h.Value, (int)packet.info[0], (int)packet.info[1], (int)packet.info[2], (int)packet.info[3], (double)packet.info[4]); } } else if (header == (short)RecieveHeaders.SPECTATE_FINISH) { foreach (KeyValuePair <string, ClientHandler> h in specs) { PacketWriter.sendSpectateFinish(h.Value, (int)packet.info[0], (int)packet.info[1], (int)packet.info[2], (int)packet.info[3], (int)packet.info[4], (int)packet.info[5], (int)packet.info[6], (double)packet.info[7]); } } else if (header == (short)RecieveHeaders.SPECTATE_FAIL) { foreach (KeyValuePair <string, ClientHandler> h in specs) { PacketWriter.sendSpectateFail(h.Value); } } else if (header == (short)RecieveHeaders.SPECTATE_GOT_CHART) { if (speccing != null && speccing.specs.Keys.Contains(userName)) { speccing.specChart[userName] = true; } } }
public void handlePacket(short header, RecievePacket packet) { if (header == (short)RecieveHeaders.SONG_START) { User u = Server.userList[userName]; if (u.Name.Equals((string)packet.info[0])) { u.CurrentChart = (string)packet.info[1]; u.CurrentSong = (string)packet.info[2]; u.Mode = (User.PlayMode)packet.info[3]; u.ModFlags = (int)packet.info[4]; u.Scroll = (double)packet.info[5]; u.Speed = (double)packet.info[6]; } if (specs.Count > 0 && !u.CurrentChart.Equals("") && (u.Mode == User.PlayMode.INGAME || u.Mode == User.PlayMode.MULTI)) { foreach (KeyValuePair<string, ClientHandler> h in specs) { PacketWriter.sendSpectateStart(h.Value, userName, Server.userList[userName].CurrentChart, Server.userList[userName].ModFlags, Server.userList[userName].Scroll); specChart[h.Key] = false; } } } else if (header == (short)RecieveHeaders.LOGIN && packet != null) { User temp = (User)packet.info[0]; userName = temp.Name.ToLower(); //tolower unnecessary, but more explicit that userName is lowercase temp.Handler = this; myThread.Name = userName; } //should have some sort of packet converter that converts packet to some subclass with typed fields parsed from the list that's properly named to increase readability else if (header == (short)RecieveHeaders.SPECTATE_HOOK) { if (Server.userList.ContainsKey((string)packet.info[0])) { try { if (speccing != null) { speccing.unspectate(this); } } catch { } Server.userList[(string)packet.info[0]].Handler.spectate(this); if (!Server.userList[(string)packet.info[0]].CurrentChart.Equals("")) { PacketWriter.sendSpectateStart(this, (string)packet.info[0], Server.userList[(string)packet.info[0]].CurrentChart, Server.userList[(string)packet.info[0]].ModFlags, Server.userList[(string)packet.info[0]].Scroll); } speccing = Server.userList[(string)packet.info[0]].Handler; } } else if (header == (short)RecieveHeaders.SPECTATE_HIT) { foreach (KeyValuePair<string, ClientHandler> h in specs) { PacketWriter.sendHit(h.Value, (int)packet.info[0], (int)packet.info[1], (int)packet.info[2], (int)packet.info[3]); } } else if (header == (short)RecieveHeaders.SPECTATE_RELEASE) { foreach (KeyValuePair<string, ClientHandler> h in specs) { PacketWriter.sendSpectateRelease(h.Value, (int)packet.info[0], (int)packet.info[1]); } } else if (header == (short)RecieveHeaders.SPECTATE_CANCEL) { if (Server.userList.ContainsKey((string)packet.info[0])) { Server.userList[(string)packet.info[0]].Handler.unspectate(this); speccing = null; } } else if (header == (short)RecieveHeaders.SPECTATE_PRESS) { foreach (KeyValuePair<string, ClientHandler> h in specs) { PacketWriter.sendSpectatePress(h.Value, (int)packet.info[0], (int)packet.info[1]); } } else if (header == (short)RecieveHeaders.SPECTATE_HEARTBEAT) { foreach (KeyValuePair<string, ClientHandler> h in specs) { PacketWriter.sendSpectateHeartbeat(h.Value, (int)packet.info[0], (int)packet.info[1], (int)packet.info[2], (int)packet.info[3], (double)packet.info[4]); } } else if (header == (short)RecieveHeaders.SPECTATE_FINISH) { foreach (KeyValuePair<string, ClientHandler> h in specs) { PacketWriter.sendSpectateFinish(h.Value, (int)packet.info[0], (int)packet.info[1], (int)packet.info[2], (int)packet.info[3], (int)packet.info[4], (int)packet.info[5], (int)packet.info[6], (double)packet.info[7]); } } else if (header == (short)RecieveHeaders.SPECTATE_FAIL) { foreach (KeyValuePair<string, ClientHandler> h in specs) { PacketWriter.sendSpectateFail(h.Value); } } else if (header == (short)RecieveHeaders.SPECTATE_GOT_CHART) { if (speccing != null && speccing.specs.Keys.Contains(userName)) { speccing.specChart[userName] = true; } } }