//~~~~~~~~~~~~~~{Functions}~~~~~~~~~~~~~~ /// <summary>Ticks this connection. Essentially processes any input it may need to parse.</summary> public void Tick() { //If there's data available. if (IsConnected && River.DataAvailable) { //HeadServer.ToLog("Attempting to read message from " + IP.Address.ToString()); //no mas //Save all the bytes to an array List <byte> Bytes = new List <byte>(); while (River.DataAvailable) { Bytes.Add((byte)River.ReadByte()); } //Parse that array of bytes as a ASCII encoded string string Command = Encoding.ASCII.GetString(Bytes.ToArray()); //Handle VBNullChar or \0 in this case. Command = Command.Replace("\0", ""); //Add this to the list of commands. ConsolePreview += IP.Address + "> " + Command; //Now let's try to parse it. string Reply = ""; string[] CommandSplit = Command.Split(' '); switch (CommandSplit[0].ToUpper()) { case "WELCOME": Reply = HeadServer.GetWelcomeMessage(); break; case "LOGIN": if (User != HeadServer.AnonymousUser) { Reply = "2"; } //ALREADY else if (CommandSplit.Length != 3) { Reply = "1"; } //INVALID else { SwitchboardUser myUser = null; //Find the user. foreach (SwitchboardUser User in HeadServer.Users) { if (User.GetUsername().ToUpper() == CommandSplit[1].ToUpper()) { myUser = User; break; } } if (myUser != null && myUser.VerifyPassword(CommandSplit[2])) { if (myUser.IsOnline() && !HeadServer.AllowMultiLogin) { Reply = "3"; } //OTHERLOCALE else { User = myUser; User.SetOnline(true); //Refresh the list, this connection has logged in try { HeadServer.TheForm.ServerBWorker.ReportProgress(0); } catch (Exception) {} //In a try because this may cause a problem if two users try to log in at the same time. The user will still sign in though, so this would sitll //refresh anyways, so no need to worry. Reply = "0"; //SUCCESS } } else { Reply = "1"; } //INVALID } break; case "LOGOUT": if (User == HeadServer.AnonymousUser) { Reply = "0"; } else { User.SetOnline(false); User = HeadServer.AnonymousUser; HeadServer.TheForm.ServerBWorker.ReportProgress(0); //Refresh the list, this connection has logged out. Reply = "1"; } break; case "CLOSE": Close(true); return; case "USERINFO": if (CommandSplit.Length == 1) { Reply = User.InfoString(); } if (CommandSplit.Length == 2) { //Check if they want info on the anonymous user if (CommandSplit[1].ToUpper() == HeadServer.AnonymousUser.GetUsername().ToUpper()) { Reply = HeadServer.AnonymousUser.InfoString(); break; } //Find the given user foreach (SwitchboardUser U in HeadServer.Users) { if (U.GetUsername().ToUpper() == CommandSplit[1].ToUpper()) { Reply = U.InfoString(); break; } } //Make sure that if we haven't found it, we say we didn't find it. if (string.IsNullOrWhiteSpace(Reply)) { Reply = "NOTFOUND~-1~0"; } } break; case "ID": Reply = ID + ""; break; case "REBIND": //Check if the rebind exist try { int OtherConnectionID = int.Parse(CommandSplit[1]); if (OtherConnectionID == ID) { Reply = "Could not rebind connection " + ID + ". Cannot rebind connection to itself"; break; } if (!HeadServer.Connections.ContainsKey(OtherConnectionID)) { Reply = "Could not rebind connection " + OtherConnectionID + ". It was not found."; break; } HeadServer.Connections[OtherConnectionID].AbsorbConnection(this); //Absorbing this connection should stop the thread but just in case TickThread.Abort(); } catch (Exception E) { Reply = "An Exception Occurred:\n\n" + E.Message + "\n" + E.StackTrace; } break; case "REPEAT": Reply = LastMessage; break; default: if (!HeadServer.AllowAnonymous && User == HeadServer.AnonymousUser) { Reply = "You're unauthorized to run any other commands."; } else { foreach (SwitchboardExtension extension in HeadServer.Extensions) { if (!string.IsNullOrEmpty(Reply)) { break; } Reply = extension.Parse(ref User, Command); } if (string.IsNullOrEmpty(Reply)) { Reply = "Could not parse command [" + Command + "]"; } } break; } if (string.IsNullOrWhiteSpace(Reply)) { Reply = "An unspecified error occured"; } //Time to return whatever it is we got. Send(Reply); //and we're done. } }