Esempio n. 1
0
		public int Login(string sNameX, string sPwdX, out string sReturnMessage) {
		//returns valid iTokenNum or sets sReturnMessage
			//debug NYI check if user is already logged in & relogin if errantly logged in
			// (check before calling this function?)
			//sFuncNow="Login("+sNameX+")";
			Random rTokenByte=new Random(PlatformNow.TickCount);
			sReturnMessage="";
			int iTokenX=PacketToken.Invalid; //(invalid token by default)
			try {
				bool bGood=true;//call mysql to check if correct name/pwd in DIFFERENT try block to detect mysql error
				if (bGood==true) {
					//string sTest;
					iTokenX=GenerateTokenNum(out sReturnMessage);
					if ((iTokenX>=0) && (iTokenX!=PacketToken.Invalid)) {
						if (userarr[iTokenX]!=null) {
							sReturnMessage+="Account manager found that the user slot was occupied";
							iTokenX=PacketToken.NoLogin;
						}
						else { //good
							RReporting.Error_WriteLine(" Validated username \""+sNameX+"\" and gave token #"+iTokenX.ToString()+" --starting user init.");
							userarr[iTokenX]=new User();
							userarr[iTokenX].iTokenBytes=iMaxTokenBytes; //debug NYI randomize
							userarr[iTokenX].byarrToken=new byte[userarr[iTokenX].iTokenBytes];
							userarr[iTokenX].sName=sNameX;
							//sFuncNow="Login("+sNameX+") after making blank token";
							//for (int i=0; i<userarr[iTokenX].iTokenBytes; i++) {
							//	userarr[iTokenX].byarrToken[i]=rTokenByte.Next(255);
							//}
							rTokenByte.NextBytes(userarr[iTokenX].byarrToken);
							//sFuncNow="Login("+sNameX+") after generating token";
							userarr[iTokenX].bitsAttrib |= UserAttrib.LoggedIn;
							//int index=-1; //new index for iarrIndexer
							for (int iNow=0; iNow<iMaxUsers; iNow++) {
								if (iarrIndexer[iNow]<0 || iNow>=iIndexers) {//<0 means user logged out so indexer is free
									if (iNow>=iIndexers) iIndexers++;
									iarrIndexer[iNow]=iTokenX;
									break;
								}
							}
							iUsers++;
							//sFuncNow="Login("+sNameX+") after setting indexer (There are now "+iUsers.ToString()+" users) ";
						}
					}
					else {
						sReturnMessage="Sorry, the server is full--please try again later.";
						bGood=false;
					}
				}
				else {
					iTokenX=PacketToken.NoLogin;
					sReturnMessage="The username or password you typed did not match.";
				}
			}
			catch (Exception exn) {
				sReturnMessage="Exception error while trying to login--"+exn.ToString();
				iTokenX=PacketToken.Invalid;
			}
			return iTokenX;
		}//end Login
Esempio n. 2
0
        /// <summary>
        /// Use this constructor instead of the default constructor,
        /// otherwise the client will not be able to initialize
        /// </summary>
        Client(string sServerURL1, ref RetroEngine ParentX)
        {
            sFuncNow  = "Client(" + sServerURL1 + ",...)";
            Parent    = ParentX;
            bContinue = true;
            try {
                packetOut = new Packet();
                //Script scriptIni = new Script();//srNow=File.OpenText(sFile);
                //scriptIni.ReadScript("Client.ini");
                //if (!scriptIni.bErr) {

                if (sServerURL1.StartsWith("http://"))                 //debug if non-http
                {
                    sServerURL = sServerURL1;
                }
                else
                {
                    RReporting.Error_WriteLine("-sServerURL defaulted to " + sServerURL);
                }


                //Create and register remoting channel
                HttpChannel chanRetroEngine = new HttpChannel();
                ChannelServices.RegisterChannel(chanRetroEngine);
                bServer = ClientConnectServer();                 //Init the remoting system
                if (!bServer)
                {
                    RReporting.Error_WriteLine("Couldn't connect to server or single-player game. Both require that you check your firewall settings.");
                }

                //string sOS = Environment.OSVersion.ToString;
                //System.PlatformID platformidOS = Environment.OSVersion.Platform;
                //System.Version versionOS = Environment.OSVersion.Version;
                //System.Type typeOS = Environment.OSVersion.GetType;
                portServer       = new Port();           //new Port("client");
                tsClientPacketer = new ThreadStart(ClientPacketer);
                tClientPacketer  = new Thread(tsClientPacketer);
                tClientPacketer.Start();
                coreInClient = new Core();                 //this is just the mapper not the server
                coreInClient.Start();
            }
            catch (Exception exn) {
                if (bServer)
                {
                    RReporting.WriteLine("Couldn't initialize client");
                    RReporting.ShowExn(exn, "Client constructor", "initializing client");
                }
                //bContinue==false;
            }
            //ProgramMain();
        }
Esempio n. 3
0
		public Packet Deq(Packet packetAuth) { //debug performance make this a reference???
			Packet packetLogin;
			try {
				if (packetAuth==null) {
					packetLogin=new Packet();
					packetLogin.iType=PacketType.ServerMessage;
					packetLogin.Set(0,"The server detected that your software sent a null authorization");
					return packetLogin; 
				}
				else if (packetAuth.iType==PacketType.Login) {
					packetLogin=new Packet();
					packetLogin.iType=PacketType.ServerMessage;
					packetLogin.Set(0,"Server failed to process the login");
					RReporting.Error_WriteLine("*Deq login response packet"); //got packet
					RunLoginPacket(ref packetLogin, ref packetAuth);
					return packetLogin;
				}
				else {
					if (accountant.IsValidPacket(ref packetAuth)) { // && packetAuth.sFrom==userarr[packetAuth.iTokenNum].sTo) {
						if (packetqarr[packetAuth.iTokenNum]!=null) {
							if (!packetqarr[packetAuth.iTokenNum].IsEmpty) {
								return packetqarr[packetAuth.iTokenNum].Deq();//userarr[packetAuth.iTokenNum].packetq.Deq();
							}
							else {
								return null;
							}
						}
						else {
							RReporting.Error_WriteLine("*  -request was made to an user token that wasn't logged in."); 
							packetTemp.Reset();
							packetTemp.iType=PacketType.ServerMessage;
							packetTemp.Set(0,"Server couldn't find your login data, try logging in again");
							return packetTemp;
						}
					}
					else {
						RReporting.Error_WriteLine("*  -an invalid authentication packet was received");
						return packetCorrupt; //i.exn. user is not authenticated
						//debug NYI increment watch level of ALL players (do iLogins-iCorruptions and players with lowest diff are suspects)
					}
				}
			}
			catch (Exception exn) {
				RReporting.ShowExn(exn,"Packeter Deq","user getting packet");
				return packetCorrupt;
			}
			//RReporting.ShowErr("Didn't return a packet","Packeter Deq");
			//return packetCorrupt;
		}//end Deq
Esempio n. 4
0
        //private void ClientThread() { //puts the CLIENT-SIDE copy of the game world into motion
        //eventually this should use the same event loop as the server for consistency
        //}
        private void ClientPacketer()
        {
            //debug NYI need to start this thread and make it manage client packet queues
            //Packet packetAck=null;
            Parent.WriteLine("ClientPacketer started");

            iTickLastIdle = RetroEngine.TickCount;
            while (bContinue)
            {
                if ((RetroEngine.TickCount - iTickLastIdle) > 333)
                {
                    iTickLastIdle = RetroEngine.TickCount;
                    //Send Keepalive if last was 333ms ago:
                    try {
                        if (bLogin)
                        {
                            if (packetOut != null)
                            {
                                packetAck = portServer.ClientGets(packetOut);                                              //get whatever packets are available,
                            }
                            // using packetOut for security credentials
                            else
                            {
                                Parent.WriteLine("Idle packet is null so it couldn't be sent.");
                                packetAck = null;
                            }
                            if (packetAck != null)
                            {
                                RunPacket(packetAck);
                            }
                        }
                        else
                        {
                            Parent.WriteLine("not logged in. ");
                        }
                    }
                    catch (Exception exn) {
                        sLastErr = "Not connected to server address \"" + sServerURL + "\"--" + exn.ToString();
                        bServer  = false;
                        break;
                    }
                }                //if time since lastidle > 333
                else
                {
                    //Parent.WriteLine(RetroEngine.TickCount.ToString()+" was "+iTickLastIdle.ToString());
                }
            }
            RReporting.Error_WriteLine("The client packet manager stopped.");
        }
Esempio n. 5
0
  		private void Packeting() {
			int iPort = 61100; //read from ini later
			string sName = "RetroEngineServer"; //read from ini later
			bool bGood=false;
			//Script scriptIni;
			HttpChannel chanRetroEngine=null;
			//sIni="Server.ini";
			bGood=true;
			if (iPort<1024) {
				iPort=61100;
				RReporting.Error_WriteLine("  -Port was reset to "+iPort.ToString()+" because the config file was bad");
			}
			RReporting.Error_WriteLine("  -Port = "+iPort.ToString());
			RReporting.Error_WriteLine("  -Server name = "+sName);
			if (iPort<1024) bGood=false; //debug NYI if not in standard set of allowed (high-number) ports
			if (bGood) {
				//Create&register channel
				chanRetroEngine = new HttpChannel(iPort);
				ChannelServices.RegisterChannel(chanRetroEngine);
				//Register and count server for remoting
				RemotingConfiguration.RegisterWellKnownServiceType(
					typeof(Port), sName,
					WellKnownObjectMode.Singleton);
			}
			else {
				//scriptIni.Dump();
				RReporting.Error_WriteLine("The server was not initialized because the port number either set or loaded improperly in initialization file!");
			}
			
			
			//if () bContinue=true;
			Packet packetNow;
			bool bEmpty=true;
			while (bContinue) {
				if (bShuttingDown) {
					//if (iPacketsSending==0) bContinue=false; //debug this statement should be fixed and used
					if (PlatformNow.TickCount-iTickShutdown>iTicksToShutdown) bContinue=false;
				}
				if (packetqIn!=null) {
					try {
						try {
							bEmpty=packetqIn.IsEmpty;
						}
						catch (Exception exn) {
							RReporting.ShowExn(exn,"Packeting","checking whether packetqIn IsEmpty");
							bEmpty=true;
						}
						if (!bEmpty) {
							//Now Run the next Packet
							packetNow = packetqIn.Deq();
							if (packetNow==null) RReporting.ShowErr(" (packetq) packet in non-empty queue was null","Packeting");
							else RunPacket(ref packetNow);
						}//end if not empty
					}
					catch (Exception exn) {
						RReporting.ShowExn(exn,"Packeting","trying to process next incoming packet");
					}
				}
				else RReporting.ShowErr("packetqIn is not initialized correctly.","Packeting"); 
			}
			RReporting.WriteLine("Packeting stopped.");
				//if (tPacketer!=null && tPacketer.IsRunning) tPacketer.Abort(); //is this possible (To exit self)?? //debug
		}//end Packeting
Esempio n. 6
0
		}//end RunLoginPacket
		private bool RunPacket(ref Packet packetNow) {
		//called by main packet processing thread
		//and called by Enq() if login is requested (using packetLogin globally to return data in that case) 
			//bErr=false;
			int iTokenNow=0;
			bool bGood=false;
			if (packetNow==null) {
				RReporting.ShowErr("got a null packet","RunLoginPacket");
				return false;
			}
			try {
				iTokenNow=packetNow.iTokenNum;
			}
			catch (Exception exn) {
				RReporting.ShowExn(exn,"RunPacket","accessing a bad packet reference");
				return false;
			}
			try {
				switch (packetNow.iType) {
				case PacketType.Idle:
					RReporting.Error_WriteLine(" Idle recieved from "+accountant.NameOfNum(packetNow.iTokenNum));
				break;
				case PacketType.Shout: //if (packetNow.iType==PacketType.Shout) {
					packetNow.iType=PacketType.ServerMessage;
					//debug NYI only shout to nearby people if inside MMORPG
					int iNum=accountant.iIndexers;
					iTokenNow=0;
					RReporting.Error_WriteLine("--"+packetNow.sFrom+" is shouting to "+iNum.ToString()+" users");
					for (int i=0; i<iNum; i++) {
						try { //debug NYI use iarrIndexer
							iTokenNow=accountant.NumOfIndex(i);
							if (iTokenNow>0) {
								if (packetqarr!=null) {
									if (packetqarr[iTokenNow]!=null) {
										bGood=packetqarr[iTokenNow].Enq(packetNow);
										if (!bGood) RReporting.ShowErr(" QUEUE either full or error for user#"+iTokenNow+" - couldn't shout)","RunPacket");//debug NYI increase queue size to a maximum 
									}//debug NYI else statements: (show error)
									else RReporting.ShowErr("user #"+iTokenNow.ToString()+"'s packetq array is not initialized correctly","RunPacket");
								}
								else RReporting.ShowErr("The server's packetq array is not initialized correctly.  Please restart host program.","RunPacket");
							}
							//if (userarr[i]!=null && userarr[i].packetq!=null) userarr[i].packetq.Enq(packetNow);
							//debug NYI OPTIONALLY check if user is logged in, but
							// packetqarr[i] would be null then so therefore the check is OPTIONAL
						}
						catch (Exception exn) {
							RReporting.ShowExn(exn,"RunPacket","processing user shout to token#"+iTokenNow.ToString());
						} 
					}
					break;
				default:
					RReporting.ShowErr(" BAD PACKET TYPE ignored, iType="+packetNow.iType.ToString(),"RunPacket");
					break;
				}//end switch
			} //end try
			catch (Exception exn) {
				RReporting.ShowExn(exn,"RunPacket","processing packet");
				return false;
			}
			return true;
		}
Esempio n. 7
0
        private void Signal(string sSignal)
        {
            //a Signal is a text command like "/shout ProtoArmor hey" etc
            bool bGood;

            bGood           = false;
            packetOut.iType = PacketType.Invalid;
            if (!sSignal.StartsWith("/"))               //assume "/shout"
            {
                bGood           = true;
                packetOut.iType = PacketType.Shout;
                packetOut.AttribOff(PacketAttrib.Dead);
                packetOut.Set(0, sSignal);
                packetOut.sTo = "";
            }
            else if (sSignal.StartsWith("/login "))
            {
                try {
                    bGood  = true;
                    bLogin = false;                   //debug check if already logged in, if so, skip function and set bLogin=true;
                    packetOut.Reset();
                    packetOut.sTo   = "";
                    packetOut.sFrom = sSignal.Substring(7, sSignal.Length - 7);
                    int iTemp = packetOut.sFrom.IndexOf(" ") + 1;
                    if (iTemp < 2)
                    {
                        bGood = false;
                    }
                    else
                    {
                        packetOut.iarr[0] = iTemp - 1;                     //says what characters of the string are the username
                        if (packetOut.sFrom.Length - iTemp < 1)
                        {
                            bGood = false;
                        }
                        else
                        {
                            string sPwd = packetOut.sFrom.Substring(iTemp, packetOut.sFrom.Length - iTemp);
                            packetOut.sFrom = packetOut.sFrom.Substring(0, iTemp - 1);
                            packetOut.iType = PacketType.Login;
                            packetOut.Set(0, sPwd);                            //1,sPwd); packetOut.Set(0,packetOut.sFrom);//eliminate this?
                        }
                    }
                }
                catch (Exception exn) {
                    RReporting.ShowExn(exn, "Signal");;
                }
            }            //else if login

            if (!bGood)
            {
                packetOut.iType = PacketType.Invalid;
            }
            else               //run the packet
            {
                bGood = true;
                //while (bServer && bContinue) { //main user entry loop (main event loop is really EMRE.Core.Scenarior())
                if (bServer)
                {
                    //sLine=Parent.ReadLine();
                    //Signal(sLine);
                    //send the packet
                    if (sSignal == ("/exit"))
                    {
                        bContinue = false;                       //debug NYI send a PacketType.Logout first and make sure logout worked.
                        bool bTest = false;
                        if (coreInClient != null)
                        {
                            bTest = coreInClient.Stop();                                           //debug NYI track the thread and kill if locked
                        }
                        try {
                            RReporting.Error_WriteLine("Packets iSent=" + iSent.ToString());
                        }
                        catch (Exception exn) {};
                    }
                    else if (packetOut.iType == PacketType.Invalid)
                    {
                        try {
                            RReporting.Error_WriteLine("The command you typed was not understood.");
                        }
                        catch (Exception exn) {};
                    }
                    else if (packetOut.iType == PacketType.Login)                     //this is the only time that ClientGets is used
                    {
                        try {
                            packetOut = portServer.ClientGets(packetOut);
                            if (packetOut != null)
                            {
                                if (packetOut.iType == PacketType.LoginConfirmation)
                                {
                                    bLogin = true;
                                    RReporting.Error_WriteLine("Server responded to the login request: ");
                                    RReporting.Error_WriteLine(packetOut.s);
                                }
                                else
                                {
                                    RReporting.Error_WriteLine("The server couldn't log you in, but instead said:");
                                    RReporting.Error_WriteLine("  " + packetOut.s);
                                }
                                packetOut.sFrom = packetOut.sTo;                               //lets the packet be used as auth. packet from now on.
                            }
                            else
                            {
                                RReporting.Error_WriteLine("The server sent a null reply to the login attempt");
                            }
                        }
                        catch (Exception exn) {
                            sFuncNow = "trying to get login packet";
                            sLastErr = "Exception error--" + exn.ToString();
                        }
                    }
                    else if (!bLogin)
                    {
                        Parent.WriteLine("You don't appear to be logged in.  Type /login username password");
                    }
                    else                       //if command is OK
                    {
                        try {
                            packetOut.iTickSent = RetroEngine.TickCount;
                            bGood = portServer.ClientSends(packetOut);
                            if (!bGood)
                            {
                                Parent.WriteLine("The server wouldn't accept the data you sent.");
                                if (packetOut.iTokenNum > 0)
                                {
                                    Parent.WriteLine("  Try typing the command \"/login username password\" if you didn't.");                                                       //if (packetAck!=null) Parent.WriteLine("Server acknowledgement: {0}", packetAck.s);
                                }
                                else
                                {
                                    Parent.WriteLine("  You do not have permission.  Type \"/login username password\" to login.");
                                    //Parent.WriteLine("-You may need to create an account first if that doesn't work.");//if (packetAck!=null) Parent.WriteLine("Server acknowledgement: {0}", packetAck.s);
                                    //debug NYI// Parent.WriteLine("-You may also have your password sent to your registered exn-mail address.");
                                }
                            }
                            else
                            {
                                iSent++;
                            }
                            //else Parent.WriteLine("The server sent no acknowledgement.");
                        }
                        catch (Exception exn) {
                            bGood    = false;
                            sLastErr = "Not connected to server address \"" + sServerURL + "\"--" + exn.ToString();
                            bServer  = false;
                        }
                    }             //else command is OK
                }                 //end if bServer
                else
                {
                    Parent.WriteLine("No connection to server.");
                    //break;
                }
                //}
                //Thread.Sleep(1000); //debug NYI wait for client packet thread to terminate
                //debug NYI should use a boolean value that tells them to shut down on their own:
                //if (tClientPacketer!=null && tClientPacketer.IsRunning) tClientPacketer.Abort();
            }
        }