?? serverprogramdlg.cpp
字號:
_itoa(nUsersOnline, strUsersOnline, 10);
_itoa(nTotalUsers, strTotalUsers, 10);
m_nUsersOnline = strUsersOnline;
m_nTotalUsers = strTotalUsers;
UpdateData(false);
//If the Server is in the System Tray update its mouse over text
if (bSystemTray && bMinimized_)
pTrayIcon_->SetTooltipText("Total Users Connected: " + m_nTotalUsers + " Users Currently Online: " + m_nUsersOnline);
//If Sound is enabled, make a beep to signify a user connection.
if (bSound)
MessageBeep(0xFFFFFFFF);
return (0);
}
//Called whenever a User disconnects from the Server
LRESULT CServerProgramDlg::OnUserDisconnect(WPARAM message,LPARAM nSocket)
{
int *pUser = (int *)nSocket;
int nUser = (int)pUser;
CString userName;
for (int x=m_Users.GetItemCount()-1;x>-1;x--)
{
if (atoi(m_Users.GetItemText(x,1))==nUser)
{
//If User Logging is enabled, this code logs the disconnecting of the user.
if (bLog)
{
userName = m_Users.GetItemText(x,0);
GetLocalTime(&st);
GetTimeFormat(LOCALE_USER_DEFAULT,DATE_LONGDATE,&st,NULL,szTime,80);
GetDateFormat(LOCALE_USER_DEFAULT,DATE_LONGDATE,&st,NULL,szDate, 80);
log.SeekToEnd();
log.WriteString(userName+" Disconnected On "+CString(szDate)+" at "+szTime+"\n");
}
//This delets the user off the list control.
m_Users.DeleteItem(x);
//This reduces the number of users online by one
nUsersOnline--;
char strUsersOnline[8];
_itoa(nUsersOnline, strUsersOnline, 10);
m_nUsersOnline = strUsersOnline;
UpdateData(false);
//If the Server is in the System Tray update its mouse over text
if (bSystemTray && bMinimized_)
pTrayIcon_->SetTooltipText("Total Users Connected: " + m_nTotalUsers + " Users Currently Online: " + m_nUsersOnline);
}
}
return (0);
}
//This is called when a message arrives from any User. Ignore its contents, as
//it fixes the message up and sends it to MessageReceive, which is where you should focus.
//Just a note: Never use the character '<' in your messages, as this will screw things up!
LRESULT CServerProgramDlg::OnReceiveMessage(WPARAM receiveMessage,LPARAM nSocket)
{
char *pMessage = (char *)receiveMessage;
CString message = pMessage;
int *pSocket = (int *)nSocket;
int numSocket = (int)pSocket;
MessageReceive(message,numSocket);
return (0);
}
void CServerProgramDlg::MessageReceive(CString message, int numSocket)
{
if (message[0] == 'A') // User Sending message intended for another user
{
message.Delete(0);
CString targetUser = ParseMessage(message,1);
message = message.Mid(targetUser.GetLength()+1);
SendUserMessage(message, atoi(targetUser));
}
else if (message[0] == 'B') // User Sending message intended for all users
{
message.Delete(0);
SendAllUsersMessage(message);
}
else if (message[0] == 'C') // User Sending message to all users but one
{
message.Delete(0);
CString exceptionUser = ParseMessage(message,1);
message = message.Mid(exceptionUser.GetLength()+1);
SendAllUsersMessageExceptForOne(message, atoi(exceptionUser));
}
else if (message[0] == 'D') // User Sending the server a message
{
message.Delete(0);
if (message[0] == 'C') // User requesting to connect
{
message.Delete(0);
//Parses the message to get their name, version, and password.
CString userName = ParseMessage(message, 1);
CString userVersion = ParseMessage(message, 2);
CString userPassword = ParseMessage(message, 3);
int result = UserLogOn(userName, userVersion, userPassword, numSocket);
if (result==-1) //User's Version not correct
SendUserMessage("1", numSocket);
else if (result==-2) //User's Password not correct
SendUserMessage("3", numSocket);
else if (result==0) //Inform User they are logged on.
{
SendUserMessage("2", numSocket);
SendAllUsersUserList();
}
}
}
// Add any other message receiving code here //
}
//This code helps to parse individual messages. See MessageReceive for how
//to use it.
CString CServerProgramDlg::ParseMessage(CString message, int nSlot)
{
int TempSize;
CString singleMessage;
for (int x=0;x<nSlot;x++)
{
TempSize=message.Find("|",0);
if (TempSize==-1)
return "ERROR";
singleMessage=message.Left(TempSize);
message=message.Mid(TempSize+1);
}
return singleMessage;
}
//Sends a User a message. This function is used by the other functions,
//and you probably won't need to call it directly.
void CServerProgramDlg::GiveMessage(CString message, int nSock)
{
long messageLength = message.GetLength();
//Sends the length of the message to the client
Sock[nSock].Send(&messageLength,4);
//Sends the actual message to the client
Sock[nSock].Send(message, messageLength);
}
//Sends a user a message
void CServerProgramDlg::SendUserMessage(CString message, int nSock)
{
GiveMessage(message, nSock);
}
//Sends all users connected a message
void CServerProgramDlg::SendAllUsersMessage(CString message)
{
for (int x=0;x<m_Users.GetItemCount();x++)
GiveMessage(message, atoi(m_Users.GetItemText(x,1)));
}
//Sends all users connected a message except for the user with socket nSock.
void CServerProgramDlg::SendAllUsersMessageExceptForOne(CString message, int nSock)
{
for (int x=0;x<m_Users.GetItemCount();x++)
{
if (atoi(m_Users.GetItemText(x,1)) != nSock)
GiveMessage(message, atoi(m_Users.GetItemText(x,1)));
}
}
//Sends all users the list of users online
void CServerProgramDlg::SendAllUsersUserList()
{
CString UserInfo;
for (int x=0;x<m_Users.GetItemCount();x++)
{
UserInfo += m_Users.GetItemText(x,0)+"|"+m_Users.GetItemText(x,1);
}
SendAllUsersMessage("4"+UserInfo);
}
//Reads the saved server settings which are stored in the registry (so that you don't have
//to reselect each option each time you run the server).
int CServerProgramDlg::ReadSettings()
{
//These are the default settings the FIRST time that this program is run.
if (!Registry.Open(HKEY_LOCAL_MACHINE,"Software\\ServerProgram"))
{
bSound = false;
bLog = false;
logPath = "C:\\Server.log";
bVersionCheck = false;
version = "1.0";
bPassword = false;
userFilePath = "C:\\Users.ini";
nPort = 4306;
bSystemTray = true;
bMaintenance = false;
nMaintenanceDays = 90;
return (1);
}
//Reads all the saved settings.
if (Registry.CreateKey(HKEY_LOCAL_MACHINE,"Software\\ServerProgram",""))
{
Registry.Open(HKEY_LOCAL_MACHINE,"Software\\ServerProgram");
DWORD soundBuffer;
if (Registry.ReadDWORD("Sound",&soundBuffer))
if (soundBuffer==1)
bSound = true;
else
bSound = false;
DWORD logBuffer;
if (Registry.ReadDWORD("Log",&logBuffer))
if (logBuffer==1)
bLog = true;
else
bLog = false;
char logPathBuffer[50];
if (Registry.ReadString("Log Path",logPathBuffer,50))
logPath = logPathBuffer;
DWORD versionCheckBuffer;
if (Registry.ReadDWORD("Version",&versionCheckBuffer))
if (versionCheckBuffer==1)
bVersionCheck = true;
else
bVersionCheck = false;
char versionBuffer[50];
if (Registry.ReadString("Version Number",versionBuffer,50))
version = versionBuffer;
DWORD passwordBuffer;
if (Registry.ReadDWORD("Password",&passwordBuffer))
if (passwordBuffer==1)
bPassword = true;
else
bPassword = false;
char userFilePathBuffer[50];
if (Registry.ReadString("User File",userFilePathBuffer,50))
userFilePath = userFilePathBuffer;
DWORD portBuffer;
if (Registry.ReadDWORD("Port",&portBuffer))
nPort = portBuffer;
DWORD systemTrayBuffer;
if (Registry.ReadDWORD("System Tray",&systemTrayBuffer))
if (systemTrayBuffer==1)
bSystemTray = true;
else
bSystemTray = false;
DWORD maintenanceBuffer;
if (Registry.ReadDWORD("Maintenance",&maintenanceBuffer))
if (maintenanceBuffer==1)
bMaintenance = true;
else
bMaintenance = false;
DWORD maintenanceDaysBuffer;
if (Registry.ReadDWORD("Maintenance Days",&maintenanceDaysBuffer))
nMaintenanceDays = maintenanceDaysBuffer;
}
return (0);
}
//Called when you want to update the Settings.
void CServerProgramDlg::OnSettings()
{
//for checking if the server, log file, or user file will need to be reinitialized.
int nOriginalPort = nPort;
CString originalLogPath = logPath;
CString originalUserFilePath = userFilePath;
Settings Settings;
Settings.m_Sound = bSound;
Settings.m_Log = bLog;
Settings.m_LogFile = logPath;
Settings.m_Version = bVersionCheck;
Settings.m_VersionNum = version;
Settings.m_Password = bPassword;
Settings.m_UserFile = userFilePath;
Settings.m_SystemTray = bSystemTray;
Settings.m_Maintenance = bMaintenance;
char port[50];
_itoa(nPort,port,10);
Settings.m_Port = port;
char maintenanceDays[10];
_itoa(nMaintenanceDays,maintenanceDays,10);
Settings.m_MaintenanceDays = maintenanceDays;
if (Settings.DoModal()==IDOK)
{
//Updates the Settings variables.
if (Settings.m_Sound==1)
bSound = true;
else
bSound = false;
if (Settings.m_Log==1)
bLog = true;
else
bLog = false;
logPath = Settings.m_LogFile;
if (Settings.m_Version==1)
bVersionCheck = true;
else
bVersionCheck = false;
version = Settings.m_VersionNum;
if (Settings.m_Password==1)
bPassword = true;
else
bPassword = false;
userFilePath = Settings.m_UserFile;
nPort = atoi(Settings.m_Port);
if (Settings.m_SystemTray==1)
bSystemTray = true;
else
bSystemTray = false;
if (Settings.m_Maintenance==1)
bMaintenance = true;
else
bMaintenance = false;
nMaintenanceDays = atoi(Settings.m_MaintenanceDays);
//Saves these settings to the registry.
if (Registry.CreateKey(HKEY_LOCAL_MACHINE,"Software\\ServerProgram",""))
{
Registry.Open(HKEY_LOCAL_MACHINE,"Software\\ServerProgram");
Registry.WriteDWORD("Sound",bSound);
Registry.WriteDWORD("Log",bLog);
char logPathBuffer[50];
strcpy(logPathBuffer, logPath);
Registry.WriteString("Log Path",&logPathBuffer);
Registry.WriteDWORD("Version",bVersionCheck);
char versionBuffer[50];
strcpy(versionBuffer, version);
Registry.WriteString("Version Number",&versionBuffer);
Registry.WriteDWORD("Password",bPassword);
char userBuffer[50];
strcpy(userBuffer, userFilePath);
Registry.WriteString("User File",&userBuffer);
Registry.WriteDWORD("Port",nPort);
Registry.WriteDWORD("System Tray",bSystemTray);
Registry.WriteDWORD("Maintenance",bMaintenance);
Registry.WriteDWORD("Maintenance Days",nMaintenanceDays);
}
//If modified, this code reinitialized the Log File.
if (bLog && (originalLogPath != logPath))
{
if(log.m_pStream != NULL)
log.Close();
WIN32_FIND_DATA fData;
HANDLE handle = FindFirstFile(LPCTSTR(logPath),&fData);
if (handle == INVALID_HANDLE_VALUE)
{
if (log.Open (logPath, CFile::modeCreate | CFile::modeWrite) == TRUE)
if(log.m_pStream != NULL)
log.Close();
}
log.Open (logPath, CFile::modeWrite);
}
//If modified, this code reinitializes the Users File.
if (bPassword && (originalUserFilePath != userFilePath))
{
usersFile.WriteFile();
usersFile.Reset();
usersFile.SetPath(userFilePath);
usersFile.ReadFile();
}
//If modified, this code reinitializes the Server.
if (nOriginalPort != nPort)
{
listenSock.Close();
StartServer();
}
}
}
//Called when you want to view the User Log. (While viewing it the Server will
//continue to operate, as well as the log file will continue to record users actions
//(although you will not see this until you reload the file)
void CServerProgramDlg::OnViewlog()
{
if(log.m_pStream != NULL)
log.Close();
ShellExecute( NULL, "open", logPath, NULL, NULL, SW_SHOWNORMAL);
Sleep(1000);
log.Open (logPath,CFile::modeWrite);
}
//Called when you want to edit the Users File.
void CServerProgramDlg::OnViewusers()
{
usersFile.WriteFile();
ShellExecute( NULL, "open", userFilePath, NULL, NULL, SW_SHOWNORMAL);
AfxMessageBox("Press OK once you are done editing the users file. After you press OK, the edited changes will take effect.");
usersFile.ReadFile();
}
//Called when you Shut Down the Server.
void CServerProgramDlg::OnExitServer()
{
int Disconnect=MessageBox("Exiting Will Terminate all Users! Are You Sure You Want to Exit the Server?","Shut Down Server?",MB_YESNO|MB_ICONQUESTION);
if (Disconnect==IDYES)
{
if (bLog)
{
GetLocalTime(&st);
GetTimeFormat(LOCALE_USER_DEFAULT,DATE_LONGDATE,&st,NULL,szTime,80);
GetDateFormat(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL, szDate, 80);
log.SeekToEnd();
log.WriteString("Server Exited on "+CString(szDate)+" at "+CString(szTime)+"\n");
}
OnOK();
}
}
void CServerProgramDlg::OnTimer(UINT nIDEvent)
{
if (nIDEvent==1) //Daily Maintenance - Delete Old Users
{
//Run through each individual user in the Users File
for (int a=usersFile.names.GetSize()-1; a>=0 ;a--)
{
int LastTimeOnline = usersFile.GetValueI(usersFile.names[a],"Days Since Last Time Online");
if (bMaintenance && LastTimeOnline > nMaintenanceDays)
usersFile.DeleteKey(usersFile.names[a]); //Deletes the Old User
else
{
LastTimeOnline++;
usersFile.SetValueI(usersFile.names[a],"Days Since Last Time Online",LastTimeOnline); //Increments all other user one day older
}
}
usersFile.WriteFile(); //Saves the changes
}
CDialog::OnTimer(nIDEvent);
}
void CServerProgramDlg::OnClose()
{
OnExitServer();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -