📄 main.cpp
字号:
tasks.erase(task->task_id);
return true;
// tasks_type::iterator pos = tasks.begin(), end = tasks.end();
// for (; pos != end; ++pos) {
// if (pos->second == task) {
// tasks.erase(pos);
// return true;
// }
// }
// return false;
}
_task_recv_file * CSender::_find_a_recv_task(u_long addr)
{ //PK 2004/10/19 - 12/17
EnterCriticalSection(&m_locker.m_critical);
tasks_type::iterator pos = m_recv_file_tasks.begin(), end = m_recv_file_tasks.end();
for (; pos != end; ++pos)
if (((_task_recv_file*)(pos->second))->peer_addr == addr) return (_task_recv_file*)(pos->second);
LeaveCriticalSection(&m_locker.m_critical);
return 0;
}
bool CSender::_erase_a_send_user(_task_send_file * task, u_long addr)
{ //PK 2004/10/10 - 21
task->users.erase(addr);
if (task->users.empty()) {
EnterCriticalSection(&m_locker.m_critical);
_task_send_file * temp = (_task_send_file *)(m_send_file_tasks[task->task_id]);
m_send_file_tasks.erase(task->task_id);
delete temp;
LeaveCriticalSection(&m_locker.m_critical);
}
return true;
}
bool CSender::_task_is_canceled(u_long task_id, u_long addr)
{ //PK 2004/10/21 - 25
char buf[BUFSIZE];
buf[BUFLEN] = '\0';
char * pos;
int len = write_msg_head_to_buf(flag_task_cancel, buf, &pos);
memcpy(pos, &task_id, sizeof(task_id));
len += sizeof(task_id);
return _p2p_message(buf, len, addr);
}
void CSender::trim_string(string & str)
{ //PK 2004/10/20
string::iterator pos = str.begin(), end = str.end();
while ((*pos == '\t' || *pos == '\r' || *pos == '\n' || *pos == ' ') && pos != end)
pos = str.erase(pos);
if (pos == end) return;
--end;
while ((*end == '\t' || *end == '\r' || *end == '\n' || *end == ' '))
end-- = str.erase(end);
}
bool CSender::is_emotion_command(CRStr command, int & index, u_long & addr)
{ //PK 2004/10/22 - 28
string my_command = command;
char * pos = (char*)my_command.c_str();
if (pos[0] != '/' || pos[1] != '/') return false;
if (std::string::npos != my_command.find_first_of(' ')) {
pos[my_command.find_first_of(' ')] = '\0';
index = get_emotion(pos + 2);
addr = _get_user_addr(pos + strlen(pos) + 1);
if (0 == addr) return false;
} else {
index = get_emotion(pos + 2);
addr = 0;
}
if (-1 == index) return false;
return true;
}
bool CSender::emotion(int emotion_index, u_long addr)
{ //PK 2004/10/22
char buf[BUFSIZE];
buf[BUFLEN] = '\0';
char * pos;
int len = write_msg_head_to_buf(flag_emotion, buf, &pos);
memcpy(pos, &emotion_index, sizeof(emotion_index));
pos += sizeof(emotion_index);
memcpy(pos, &m_myaddr, sizeof(m_myaddr));
pos += sizeof(m_myaddr);
memcpy(pos, &addr, sizeof(addr));
len += (sizeof(emotion_index) + sizeof(m_myaddr) + sizeof(addr));
return _send_message_to_all(buf, len);
}
void CSender::modify_blacklist(u_long addr)
{ //PK 2004/10/25 - 27
if (m_blacklist.end() == m_blacklist.find(addr))
m_blacklist[addr] = m_users[addr];
else m_blacklist.erase(addr);
}
int CSender::write_msg_head_to_buf(flag_type flag, char * buf, char ** pos)
{
//PK 2004/09/13 - 10/14
int len = sizeof(_msg_head);
_msg_head msg_head;
msg_head.flag = flag;
memcpy(buf, &msg_head, len);
if (0 != pos) *pos = buf + len;
return len;
}
void CSender::_a_user_online(CRStr username, u_long addr)
{ //PK 2004/10/09 - 25
_my_time now;
if (!_is_user_online(addr)) {
m_users[addr] = _user(username, _get_peer_name(addr));
m_users[addr].is_online = true;
m_pwindow->someone_login(get_full_name(addr), addr);
} else {
if (m_users[addr].user_name != username) {
m_users[addr].user_name = username;
m_pwindow->someone_logout(addr);
m_pwindow->someone_login(get_full_name(addr), addr);
}
m_users[addr].last_online = now;
}
if (addr == m_conflict_addr) return;
if ((username == m_myname) && (addr != m_myaddr)) {
m_conflict_addr = addr;
name_conflict(addr);
}
}
bool CSender::_is_newest_version(version_type ver)
{ //PK 2004/10/13
if (ver <= get_version()) return false;
_my_time now;
string message = "(" + now.get_time() + ") " + g_system_info +
" 你的版本已经不是最新了,请及时更新!";
m_pwindow->someone_talk(message);
return true;
}
bool CSender::_is_user_online(u_long addr)
{ //PK 2004/09/13 - 10/25
//PK if user is not in userslist, ask for a reply info
if (m_users.end() == m_users.find(addr)) return false;
return m_users[addr].is_online;
}
string CSender::_get_file_name(CRStr path)
{ //PK 2004/10/20
char fname[MAX_PATH], ext[MAX_PATH];
_splitpath(path.c_str(), 0, 0, fname, ext);
strcat(fname, ext);
return fname;
}
u_long CSender::_get_user_addr(CRStr name)
{ //PK 2004/10/22
users_type::iterator pos = m_users.begin(), end = m_users.end();
for (; pos != end; ++pos)
if (name == pos->second.user_name) return pos->first;
return 0;
}
bool CSender::on_login(_msg_head * msg_head, CPChar msg_body, u_long addr)
{ //PK 2004/10/14
//PK update the user list
string username = msg_body;
_a_user_online(username, addr);
string message = " (" + msg_head->time.get_time() + ") " + g_system_info +
get_full_name(addr) + " 来了!";
//PK reply i'm online
online(addr);
m_pwindow->someone_talk(message);
return true;
}
bool CSender::on_logout(_msg_head * msg_head, CPChar msg_body, u_long addr)
{ //PK 2004/10/14
m_pwindow->someone_logout(addr);
string message = "( " + msg_head->time.get_time() + ") " + g_system_info +
get_full_name(addr) + ") 悄悄的走了!";
m_users.erase(addr);
m_pwindow->someone_talk(message);
return true;
}
bool CSender::on_online(_msg_head * msg_head, CPChar msg_body, u_long addr)
{ //PK 2004/10/14
_a_user_online(msg_body, addr);
return true;
}
bool CSender::on_say(_msg_head * msg_head, CPChar msg_body, u_long addr)
{ //PK 2004/10/14 - 18
string message = "(" + msg_head->time.get_time() + ") " + get_user_name(addr) +
" 说:\r\n " + msg_body;
m_pwindow->someone_talk(message);
return true;
}
bool CSender::on_whisper(_msg_head * msg_head, CPChar msg_body, u_long addr)
{ //PK 2004/10/14 - 18
string message = "(" + msg_head->time.get_time() + ") " + get_user_name(addr) +
" 悄悄的对你说:\r\n " + msg_body;
m_pwindow->someone_talk(message);
return true;
}
bool CSender::on_rename(_msg_head * msg_head, CPChar msg_body, u_long addr)
{ //PK 2004/10/14 - 18
if (m_conflict_addr == addr) m_conflict_addr = 0;
string message = "(" + msg_head->time.get_time() + ")" + g_system_info + " " +
get_user_name(addr) + " 已经改名为: " + msg_body;
_a_user_online(msg_body, addr);
m_pwindow->someone_talk(message);
return true;
}
bool CSender::on_name_conflict(_msg_head * msg_head, CPChar msg_body, u_long addr)
{ //PK 2004/10/14
m_conflict_addr = addr;
m_pwindow->PostMessage(WM_RENAME, 0, 0);
return true;
}
bool CSender::on_recv_file(_msg_head * msg_head, CPChar msg_body, u_long addr)
{ //PK 2004/10/14 - 19
u_long task_id = *((u_long*)msg_body);
char * pos = (char*)msg_body + sizeof(task_id);
DWORD file_size = *((DWORD*)pos);
pos += sizeof(file_size);
string file_name = pos;
_task * task = add_a_recv_file_task(task_id, file_name, file_size, addr);
m_pwindow->PostMessage(WM_RECV_FILE, (WPARAM)task, (LPARAM)addr);
return true;
}
bool CSender::on_confirm_recv_file(_msg_head * msg_head, CPChar msg_body, u_long addr)
{ //PK 2004/10/14 - 12/17
u_long task_id = *((u_long*)msg_body);
_task_send_file * task = 0;
EnterCriticalSection(&m_locker.m_critical);
bool bFind = m_send_file_tasks.find(task_id) != m_send_file_tasks.end();
LeaveCriticalSection(&m_locker.m_critical);
if (bFind) {
EnterCriticalSection(&m_locker.m_critical);
task = (_task_send_file *)m_send_file_tasks[task_id];
LeaveCriticalSection(&m_locker.m_critical);
}
else return false;
//task->state = _task::send;
//task->users[addr] = true;
//_beginthreadex(NULL, 0, send_file_thread, (void*)task, 0, 0);
_my_time now;
string message;
EnterCriticalSection(&m_locker.m_critical);
string filename = ((_task_send_file*)(m_send_file_tasks[task_id]))->file_name;
LeaveCriticalSection(&m_locker.m_critical);
message = g_system_info + " (" + now.get_time() + ") " + get_user_name(addr) + " 开始接收 " +
_get_file_name(filename) + " 文件!";
m_pwindow->someone_talk(message);
return true;
}
bool CSender::on_reject_file(_msg_head * msg_head, CPChar msg_body, u_long addr)
{ //PK 2004/10/14 - 12/17
u_long task_id = *((u_long*)msg_body);
_my_time now;
string message;
if (!is_task_exist(task_id)) return true;
EnterCriticalSection(&m_locker.m_critical);
message = g_system_info + " (" + now.get_time() + ") " + get_user_name(addr) + " 拒收了 " +
_get_file_name(((_task_send_file*)(m_send_file_tasks[task_id]))->file_name) + " 文件!";
m_pwindow->someone_talk(message);
_erase_a_send_user((_task_send_file*)(m_send_file_tasks[task_id]), addr);
LeaveCriticalSection(&m_locker.m_critical);
return true;
}
bool CSender::on_task_cancel(_msg_head * msg_head, CPChar msg_body, u_long addr)
{ //PK 2004/10/14 - 12/17
u_long task_id = *((u_long*)msg_body);
_my_time now;
string message;
EnterCriticalSection(&m_locker.m_critical);
message = g_system_info + " (" + now.get_time() + ") 发送文件 " +
((_task_recv_file*)(m_recv_file_tasks[task_id]))->file_name + " 已被发送方取消,或已超时!请让发送方重发!";
m_pwindow->someone_talk(message);
_erase_a_task(m_recv_file_tasks, m_recv_file_tasks[task_id]);
LeaveCriticalSection(&m_locker.m_critical);
return true;
}
bool CSender::on_emotion(_msg_head * msg_head, CPChar msg_body, u_long addr)
{ //PK 2004/10/22
int index = *((int*)msg_body);
u_long self_addr = *((u_long*)(msg_body + sizeof(index)));
u_long his_addr = *((u_long*)(msg_body + sizeof(index) + sizeof(u_long)));
string msg;
if (his_addr == 0) msg = g_emotion[index][1];
else if (self_addr == his_addr) msg = g_emotion[index][3];
else msg = g_emotion[index][2];
string self_name = get_user_name(self_addr);
string his_name = get_user_name(his_addr);
int placeholder = msg.find_first_of('%');
while (string::npos != placeholder) {
string place_str;
if (msg[placeholder + 1] == 'w') place_str = " " + self_name + " ";
if (msg[placeholder + 1] == 't') place_str = " " + his_name + " ";
msg.replace(placeholder, 2, place_str);
placeholder = msg.find_first_of('%');
}
_my_time now;
msg = "(" + now.get_time() + ")\r\n " + msg;
m_pwindow->someone_talk(msg);
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -