Beispiel #1
0
	void ParsePacket(Packet &Data)
	{
		// Only process if receiving PKT_USERINFO or the user is already logged in
		if ((Data.Header() == PKT_USERINFO) || (m_Username.length()))
			switch (Data.Header())
			{
				case PKT_USERINFO:
				{
					AddLog(L"Received PKT_USERINFO from socket " + m_wsSocket + L".");

					char Result = 0;
					std::wstring Username = Data.RemoveNTString();
					std::wstring Password = Data.RemoveNTString();

					for (int i = 0; i < USERS_COUNT; ++i)
						// Check if Username and Password matches
						if (MatchString(Username, Users[i].Username) && MatchString(Password, Users[i].Password, true))
						{
							// If user already logged in then fail new login attempt
							if (Users[i].LoggedIn)
								break;

							Users[i].LoggedIn = true;
							m_Username = Users[i].Username;
							Result = 1;
							break;
						}

					Packet UserInfo(PKT_USERINFO);
					UserInfo.InsertByte(Result);
					UserInfo.SendPacket(m_Socket);

					AddLog(L"Sent PKT_USERINFO to " + m_wsSocket + L".");

					int Index = FindIndex(m_Socket);

					if (Index >= 0)
					{
						SendMessage(lsClients, LB_DELETESTRING, (WPARAM)Index, 0);
						SendMessage(lsClients, LB_INSERTSTRING, (WPARAM)Index, (LPARAM)std::wstring(m_wsSocket + L" - " + m_Username).c_str());
					}
				}
				break;

				case PKT_FILE:
				{
					// Read filename
					std::wstring wsFilename(Data.RemoveNTString());
					std::string sFilename(wsFilename.begin(), wsFilename.end());

					// Check if Filename is in Files list already
					File *CurrentFile = NULL;
					std::list<File *>::const_iterator FindFile = Files.begin();

					while (FindFile != Files.end())
					{
						std::string CheckFile = ((File *)*FindFile)->Filename();

						if (CheckFile == sFilename)
						{
							CurrentFile = *FindFile;
							break;
						}

						++FindFile;
					}

					// If file not found then create new file and add it to Files list
					if (!CurrentFile)
					{
						CurrentFile = new File(sFilename, (int)Data.RemoveDWORD());
						Files.push_back(CurrentFile);
					}

					// Write to file
					CurrentFile->Write(Data.RawData());

					// if completed writing then close file and delete file object
					if (CurrentFile->Complete())
					{
						// Remove file from Files list and delete File object
						Files.erase(FindFile);
						delete CurrentFile;
					}
				}
				break;
			}
	};