public static int createUser(String firstname, String lastname, String password, bool superuser) { XmlDocument doc = new XmlDocument(); doc.Load(userXML); XmlNodeList elemList = doc.GetElementsByTagName("user"); int newid = elemList.Count + 1; XmlElement e = doc.CreateElement("user"); XmlAttribute a = doc.CreateAttribute("id"); a.Value = newid.ToString(); e.Attributes.Append(a); XmlElement fchild = doc.CreateElement("firstname"); fchild.AppendChild(doc.CreateTextNode(firstname)); XmlElement lchild = doc.CreateElement("lastname"); lchild.AppendChild(doc.CreateTextNode(lastname)); XmlElement pchild = doc.CreateElement("password"); pchild.AppendChild(doc.CreateTextNode(password)); XmlElement schild = doc.CreateElement("superuser"); schild.AppendChild(doc.CreateTextNode(superuser.ToString())); XmlElement goldchild = doc.CreateElement("gold"); goldchild.AppendChild(doc.CreateTextNode(Convert.ToString(0))); XmlElement manachild = doc.CreateElement("mana"); manachild.AppendChild(doc.CreateTextNode(Convert.ToString(100))); XmlElement xpchild = doc.CreateElement("xp"); xpchild.AppendChild(doc.CreateTextNode(Convert.ToString(0))); XmlElement groupchild = doc.CreateElement("group"); groupchild.AppendChild(doc.CreateTextNode("None")); XmlElement avatarchild = doc.CreateElement("avatar"); avatarchild.AppendChild(doc.CreateTextNode("None")); XmlElement emailchild = doc.CreateElement("email"); emailchild.AppendChild(doc.CreateTextNode("None")); for (int i = 0; i < Missions.getMissionNumber(); i++) { XmlElement nodee = doc.CreateElement("mission"); XmlAttribute nodea = doc.CreateAttribute("id"); nodea.Value = i.ToString(); nodee.Attributes.Append(nodea); XmlElement gradechild = doc.CreateElement("grade"); gradechild.AppendChild(doc.CreateTextNode("N/A")); XmlElement attemptschild = doc.CreateElement("attempts"); attemptschild.AppendChild(doc.CreateTextNode("0")); nodee.AppendChild(gradechild); nodee.AppendChild(attemptschild); e.AppendChild(nodee); } e.AppendChild(fchild); e.AppendChild(lchild); e.AppendChild(pchild); e.AppendChild(schild); e.AppendChild(goldchild); e.AppendChild(manachild); e.AppendChild(xpchild); e.AppendChild(groupchild); e.AppendChild(avatarchild); e.AppendChild(emailchild); doc.ChildNodes.Item(1).AppendChild(e); doc.Save(userXML); //doc.FirstChild.AppendChild return(newid); }
public static void ReadCallback(IAsyncResult ar) { try { String content = String.Empty; // Retrieve the state object and the handler socket // from the asynchronous state object. StateObject state = (StateObject)ar.AsyncState; Socket handler = state.workSocket; // Read data from the client socket. int bytesRead = handler.EndReceive(ar); Console.WriteLine("Processing Packet of " + bytesRead + " bytes"); if (bytesRead > 0) { // There might be more data, so store the data received so far. state.sb.Append(Encoding.ASCII.GetString( state.buffer, 0, bytesRead)); // Check for end-of-file tag. If it is not there, read // more data. content = state.sb.ToString(); Console.WriteLine(content); if (content.IndexOf("<EOF>") > -1) { Console.WriteLine("<EOF> Found with a buffer of " + state.buffer[0]); //Process data here: byte[] loginpacket = new byte[6]; if (state.buffer[0] == 1) { //Packet type: Login verification protocol Console.WriteLine("Loading login data..."); //String packet = Encoding.ASCII.GetString(state.buffer, 1, state.buffer.Length-1); String[] items = content.Split('\0'); String firstname = items[0]; firstname = firstname.Substring(1); String lastname = items[1]; //NEEDS TRY CATCH String password = items[2]; // String lastname = Encoding.ASCII.GetString(state.buffer, 22, Int32.Parse(Encoding.ASCII.GetString(state.buffer, 21, 1));); //String password = Encoding.ASCII.GetString(state.buffer, 42, Int32.Parse(Encoding.ASCII.GetString(state.buffer, 41, 1));); Console.WriteLine("Received login protocol: " + firstname + ", " + lastname + ", " + password); //Check if the credentials correspond to actual int id = findUser(firstname, lastname, password); Console.WriteLine(id); loginpacket[0] = 1; if (id == 0 || id == -1) { loginpacket[1] = 0; } else { Random random = new Random(); loginpacket[1] = 1; loginpacket[2] = Convert.ToByte(random.Next(0, 256)); loginpacket[3] = Convert.ToByte(random.Next(0, 256)); loginpacket[4] = Convert.ToByte(random.Next(0, 256)); byte[] userpacket = new byte[3]; userpacket[0] = loginpacket[2]; userpacket[1] = loginpacket[3]; userpacket[2] = loginpacket[4]; if (User.getSuperUser(id)) { loginpacket[5] = 1; } else { loginpacket[5] = 0; } //USER FOUND WITH ID 'id' Console.WriteLine("ID IS" + id); userList.Add(userpacket, id); } SendBytes(handler, loginpacket); } else if (state.buffer[0] == 2) { //Received XP protocol byte[] userpacket = new byte[3]; byte[] xppacket = new byte[15]; xppacket[0] = 2; userpacket[0] = state.buffer[1]; userpacket[1] = state.buffer[2]; userpacket[2] = state.buffer[3]; int id = 0; if (userList.TryGetValue(userpacket, out id)) { xppacket[1] = 1; //Indicates success xppacket[2] = userpacket[0]; xppacket[3] = userpacket[1]; xppacket[4] = userpacket[2]; int xp = User.getXP(id); //retrieve xp from xml! byte[] fnl = Encoding.ASCII.GetBytes("\0"); fnl.CopyTo(xppacket, 5); byte[] temp = Encoding.ASCII.GetBytes(xp.ToString()); temp.CopyTo(xppacket, 6); fnl.CopyTo(xppacket, 6 + Encoding.ASCII.GetByteCount(xp.ToString())); } else { Console.WriteLine("Failure"); xppacket[1] = 0; //Indicates failure xppacket[2] = 0; xppacket[3] = 0; xppacket[4] = 0; } SendBytes(handler, xppacket); } else if (state.buffer[0] == 6) { //Received String Array protocol byte[] userpacket = new byte[3]; byte[] finalpacket = new byte[60]; finalpacket[0] = 6; String[] b = content.Split('\0'); String fn = b[0].Substring(1); int fnn = Convert.ToInt32(fn); userpacket[0] = state.buffer[1]; userpacket[1] = state.buffer[2]; userpacket[2] = state.buffer[3]; int id = 0; if (userList.TryGetValue(userpacket, out id)) { Console.WriteLine("Confirmed string array protocol"); finalpacket[1] = 1; //Indicates success finalpacket[2] = userpacket[0]; finalpacket[3] = userpacket[1]; finalpacket[4] = userpacket[2]; byte[] fnl = Encoding.ASCII.GetBytes("\0"); fnl.CopyTo(finalpacket, 5); //Add payload (String array) here: String[] payload = new String[2]; payload[0] = User.getFirstName(fnn); payload[1] = User.getLastName(fnn); int arcount = 6; foreach (String s in payload) { if (s != null && s != String.Empty) { byte[] tempa = Encoding.ASCII.GetBytes(s); tempa.CopyTo(finalpacket, arcount); fnl.CopyTo(finalpacket, tempa.Length + arcount + 1); arcount += tempa.Length + 2; } } byte[] tempb = Encoding.ASCII.GetBytes("<EOF>"); tempb.CopyTo(finalpacket, arcount); } else { Console.WriteLine("Failure"); finalpacket[1] = 0; //Indicates failure finalpacket[2] = 0; finalpacket[3] = 0; finalpacket[4] = 0; } SendBytes(handler, finalpacket); } else if (state.buffer[0] == 7) { //Remove user from logged in user's list byte[] userpacket = new byte[3]; byte[] userdeletepacket = new byte[2]; userdeletepacket[0] = 7; userpacket[0] = state.buffer[1]; userpacket[1] = state.buffer[2]; userpacket[2] = state.buffer[3]; int id = 0; if (userList.TryGetValue(userpacket, out id)) { userdeletepacket[1] = 1; userList.Remove(userpacket); } else { userdeletepacket[1] = 0; } SendBytes(handler, userdeletepacket); } else if (state.buffer[0] == 3) { //Level transmission processing byte[] userpacket = new byte[3]; byte[] payload = new byte[5]; payload[0] = 3; userpacket[0] = state.buffer[1]; userpacket[1] = state.buffer[2]; userpacket[2] = state.buffer[3]; payload[1] = userpacket[0]; payload[2] = userpacket[1]; payload[3] = userpacket[2]; int id = 0; if (userList.TryGetValue(userpacket, out id)) { int lvl = Settings.getLevel(User.getXP(id)); if (lvl > 0 && lvl < 256) { payload[4] = 1; } } else { payload[4] = 0; } SendBytes(handler, payload); } else if (state.buffer[0] == 4) { //MinXP transmission processing byte[] userpacket = new byte[3]; byte[] payload = new byte[9]; payload[0] = 4; userpacket[0] = state.buffer[1]; userpacket[1] = state.buffer[2]; userpacket[2] = state.buffer[3]; payload[1] = userpacket[0]; payload[2] = userpacket[1]; payload[3] = userpacket[2]; int id = 0; if (userList.TryGetValue(userpacket, out id)) { int lvl = Settings.getLevel(User.getXP(id)); byte[] fnl = Encoding.ASCII.GetBytes("\0"); byte[] xp = Encoding.ASCII.GetBytes(Settings.getXp(lvl).ToString()); fnl.CopyTo(payload, 4); xp.CopyTo(payload, 5); fnl.CopyTo(payload, xp.Length + 5); if (xp.Length > 3) { Console.WriteLine("FATAL: Byte overflow error in packet type 4 processing! XP length too long!"); } } else { payload[4] = 0; } SendBytes(handler, payload); } else if (state.buffer[0] == 5) { //MaxXP transmission processing byte[] userpacket = new byte[3]; byte[] payload = new byte[9]; payload[0] = 5; userpacket[0] = state.buffer[1]; userpacket[1] = state.buffer[2]; userpacket[2] = state.buffer[3]; payload[1] = userpacket[0]; payload[2] = userpacket[1]; payload[3] = userpacket[2]; int id = 0; if (userList.TryGetValue(userpacket, out id)) { int lvl = Settings.getLevel(User.getXP(id)) + 1; byte[] fnl = Encoding.ASCII.GetBytes("\0"); byte[] xp = Encoding.ASCII.GetBytes(Settings.getXp(lvl).ToString()); fnl.CopyTo(payload, 4); xp.CopyTo(payload, 5); fnl.CopyTo(payload, xp.Length + 5); if (xp.Length > 3) { Console.WriteLine("FATAL: Byte overflow error in packet type 5 processing! XP length too long!"); } } else { payload[4] = 0; } SendBytes(handler, payload); } else if (state.buffer[0] == 8) { //Modify password byte[] userpacket = new byte[3]; byte[] payload = new byte[5]; payload[0] = 8; userpacket[0] = state.buffer[1]; userpacket[1] = state.buffer[2]; userpacket[2] = state.buffer[3]; payload[1] = userpacket[0]; payload[2] = userpacket[1]; payload[3] = userpacket[2]; int id = 0; if (userList.TryGetValue(userpacket, out id)) { Console.WriteLine("HERE"); String[] items = content.Split('\0'); //items[1] is the new password and items[2] is the old password Console.WriteLine(items[1]); Console.WriteLine(User.getPassword(id)); Console.WriteLine(items[2]); if (items[2].Equals(User.getPassword(id))) { Console.WriteLine("REACHED"); User.setPassword(id, items[1]); payload[4] = 1; //Indicates succcess } else { //Indicates wrong old password entered payload[4] = 2; } } else { //Means that could not verify user based on logincode authorization payload[4] = 0; } Console.WriteLine("HI"); SendBytes(handler, payload); } else if (state.buffer[0] == 13) { //Return positive that mission was successfully created byte[] userpacket = new byte[3]; byte[] payload = new byte[5]; payload[0] = 13; userpacket[0] = state.buffer[1]; userpacket[1] = state.buffer[2]; userpacket[2] = state.buffer[3]; payload[1] = userpacket[0]; payload[2] = userpacket[1]; payload[3] = userpacket[2]; int id = 0; if (userList.TryGetValue(userpacket, out id)) { payload[4] = 1; byte[] missionpacket = new byte[state.buffer.Length - 9]; try { Array.Copy(state.buffer, 4, missionpacket, 0, state.buffer.Length - 9); Object obj = ByteArrayToObject(missionpacket); Mission mis = (Mission)obj; Missions.createMission(mis); } catch (Exception f) { Console.WriteLine(f.ToString()); } } else { payload[4] = 0; } SendBytes(handler, payload); } else if (state.buffer[0] == 23) { //Gold transmision processing byte[] userpacket = new byte[3]; byte[] payload = new byte[9]; payload[0] = 23; userpacket[0] = state.buffer[1]; userpacket[1] = state.buffer[2]; userpacket[2] = state.buffer[3]; payload[1] = userpacket[0]; payload[2] = userpacket[1]; payload[3] = userpacket[2]; int id = 0; if (userList.TryGetValue(userpacket, out id)) { int gold = User.getGold(id); byte[] fnl = Encoding.ASCII.GetBytes("\0"); byte[] goldpacket = Encoding.ASCII.GetBytes(gold.ToString()); fnl.CopyTo(payload, 4); goldpacket.CopyTo(payload, 5); fnl.CopyTo(payload, goldpacket.Length + 5); if (goldpacket.Length > 3) { Console.WriteLine("FATAL: Byte overflow error in packet type 5 processing! Gold length too long!"); } } else { payload[4] = 0; } SendBytes(handler, payload); } else if (state.buffer[0] == 24) { //Mana transmission processing byte[] userpacket = new byte[3]; byte[] payload = new byte[9]; payload[0] = 24; userpacket[0] = state.buffer[1]; userpacket[1] = state.buffer[2]; userpacket[2] = state.buffer[3]; payload[1] = userpacket[0]; payload[2] = userpacket[1]; payload[3] = userpacket[2]; int id = 0; if (userList.TryGetValue(userpacket, out id)) { int mana = User.getMana(id); byte[] fnl = Encoding.ASCII.GetBytes("\0"); byte[] manapacket = Encoding.ASCII.GetBytes(mana.ToString()); fnl.CopyTo(payload, 4); manapacket.CopyTo(payload, 5); fnl.CopyTo(payload, manapacket.Length + 5); if (manapacket.Length > 3) { Console.WriteLine("FATAL: Byte overflow error in packet type 5 processing! Mana length too long!"); } } else { payload[4] = 0; } SendBytes(handler, payload); } else if (state.buffer[0] == 29) { //Request for list of users byte[] userpacket = new byte[3]; byte[] finalpacket = new byte[1024 + 5]; finalpacket[0] = 29; userpacket[0] = state.buffer[1]; userpacket[1] = state.buffer[2]; userpacket[2] = state.buffer[3]; int id = 0; if (userList.TryGetValue(userpacket, out id)) { finalpacket[1] = userpacket[0]; finalpacket[2] = userpacket[1]; finalpacket[3] = userpacket[2]; byte[] fnl = Encoding.ASCII.GetBytes("\0"); fnl.CopyTo(finalpacket, 4); byte[] users = User.getUserList(); users.CopyTo(finalpacket, 5); } else { Console.WriteLine("Failure"); finalpacket[1] = 0; //Indicates failure finalpacket[2] = 0; finalpacket[3] = 0; finalpacket[4] = 0; } SendBytes(handler, finalpacket); } // All the data has been read from the // client. Display it on the console. Console.WriteLine("Read {0} bytes from socket. \n Data : {1}", content.Length, content); // Echo the data back to the client. } else { // Not all data received. Get more. handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); } } } catch { return; } }