summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--protocols/EmLanProto/src/Version.h18
-rw-r--r--protocols/EmLanProto/src/amdproto.cpp120
-rw-r--r--protocols/EmLanProto/src/lan.cpp96
-rw-r--r--protocols/EmLanProto/src/mlan.cpp412
-rw-r--r--protocols/EmLanProto/src/mlan.h4
5 files changed, 250 insertions, 400 deletions
diff --git a/protocols/EmLanProto/src/Version.h b/protocols/EmLanProto/src/Version.h
index 4f8387fa25..5ed559952e 100644
--- a/protocols/EmLanProto/src/Version.h
+++ b/protocols/EmLanProto/src/Version.h
@@ -1,15 +1,15 @@
-#define __MAJOR_VERSION 0
-#define __MINOR_VERSION 0
-#define __RELEASE_NUM 1
-#define __BUILD_NUM 2
+#define __MAJOR_VERSION 0
+#define __MINOR_VERSION 1
+#define __RELEASE_NUM 0
+#define __BUILD_NUM 1
#include <stdver.h>
-#define __FILEVERSION_DWORD PLUGIN_MAKE_VERSION(__MAJOR_VERSION,__MINOR_VERSION,__RELEASE_NUM,__BUILD_NUM)
+#define __FILEVERSION_DWORD PLUGIN_MAKE_VERSION(__MAJOR_VERSION,__MINOR_VERSION,__RELEASE_NUM,__BUILD_NUM)
#define __PLUGIN_NAME "E-mage LAN protocol"
#define __FILENAME "EmLanProto.dll"
-#define __DESCRIPTION "E-mage LAN protocol support for Miranda NG."
-#define __AUTHOR "kva"
+#define __DESCRIPTION "E-mage LAN protocol support for Miranda NG."
+#define __AUTHOR "kva"
#define __AUTHOREMAIL "kva@fromru.com"
-#define __AUTHORWEB "http://miranda-ng.org/p/EmLanProto/"
-#define __COPYRIGHT "© Viktor Kuzmin"
+#define __AUTHORWEB "http://miranda-ng.org/p/EmLanProto/"
+#define __COPYRIGHT "© Viktor Kuzmin"
diff --git a/protocols/EmLanProto/src/amdproto.cpp b/protocols/EmLanProto/src/amdproto.cpp
index abfb8bedfb..49b41bd1d6 100644
--- a/protocols/EmLanProto/src/amdproto.cpp
+++ b/protocols/EmLanProto/src/amdproto.cpp
@@ -5,7 +5,7 @@
//////////////////////////////////////////////////////////////////////////
-PLUGININFOEX pluginInfo={
+PLUGININFOEX pluginInfo = {
sizeof(PLUGININFOEX),
__PLUGIN_NAME,
PLUGIN_MAKE_VERSION(__MAJOR_VERSION, __MINOR_VERSION, __RELEASE_NUM, __BUILD_NUM),
@@ -16,12 +16,11 @@ PLUGININFOEX pluginInfo={
__AUTHORWEB,
UNICODE_AWARE,
// {E08CE7C4-9EEB-4272-B544-0D32E18D90DE}
- {0xe08ce7c4, 0x9eeb, 0x4272, {0xb5, 0x44, 0xd, 0x32, 0xe1, 0x8d, 0x90, 0xde}}
+ { 0xe08ce7c4, 0x9eeb, 0x4272, { 0xb5, 0x44, 0xd, 0x32, 0xe1, 0x8d, 0x90, 0xde } }
};
HINSTANCE g_hInstance = NULL;
CMLan* g_lan = NULL;
-HANDLE g_heOptions = NULL;
int hLangpack;
bool g_InitOptions = false;
@@ -38,7 +37,7 @@ extern "C" __declspec(dllexport) PLUGININFOEX* __cdecl MirandaPluginInfoEx(DWOR
//////////////////////////////////////////////////////////////////////////
// Interface information
-extern "C" __declspec(dllexport) const MUUID MirandaInterfaces[] = {MIID_PROTOCOL, MIID_LAST};
+extern "C" __declspec(dllexport) const MUUID MirandaInterfaces[] = { MIID_PROTOCOL, MIID_LAST };
//////////////////////////////////////////////////////////////////////////
@@ -47,7 +46,7 @@ BOOL APIENTRY DllMain(HINSTANCE hInstDLL, DWORD reason, LPVOID)
g_hInstance = hInstDLL;
if (reason == DLL_PROCESS_ATTACH) {
EMLOG("EmLan Started");
- DisableThreadLibraryCalls( hInstDLL);
+ DisableThreadLibraryCalls(hInstDLL);
}
else if (reason == DLL_PROCESS_DETACH) {
EMLOG("EmLan Stopped");
@@ -58,13 +57,13 @@ BOOL APIENTRY DllMain(HINSTANCE hInstDLL, DWORD reason, LPVOID)
//////////////////////////////////////////////////////////////////////////
-static INT_PTR __cdecl EMPGetCaps(WPARAM wParam,LPARAM )
+static INT_PTR __cdecl EMPGetCaps(WPARAM wParam, LPARAM)
{
- switch(wParam) {
+ switch (wParam) {
case PFLAGNUM_1:
- return PF1_IM|PF1_BASICSEARCH|PF1_ADDSEARCHRES|PF1_PEER2PEER|PF1_INDIVSTATUS|
- PF1_URL|PF1_MODEMSG|PF1_FILE|PF1_CANRENAMEFILE|PF1_FILERESUME;
- case PFLAGNUM_2:
+ return PF1_IM | PF1_BASICSEARCH | PF1_ADDSEARCHRES | PF1_PEER2PEER | PF1_INDIVSTATUS |
+ PF1_URL | PF1_MODEMSG | PF1_FILE | PF1_CANRENAMEFILE | PF1_FILERESUME;
+ case PFLAGNUM_2:
return PF2_ONLINE | PF2_SHORTAWAY | PF2_LONGAWAY | PF2_LIGHTDND | PF2_HEAVYDND | PF2_FREECHAT;
case PFLAGNUM_3:
return PF2_SHORTAWAY | PF2_LONGAWAY | PF2_LIGHTDND | PF2_HEAVYDND | PF2_FREECHAT;
@@ -78,7 +77,7 @@ static INT_PTR __cdecl EMPGetCaps(WPARAM wParam,LPARAM )
}
}
-static INT_PTR __cdecl EMPGetName(WPARAM wParam,LPARAM lParam)
+static INT_PTR __cdecl EMPGetName(WPARAM wParam, LPARAM lParam)
{
mir_tstrncpy((char*)lParam, "EmLan", wParam);
return 0;
@@ -86,14 +85,14 @@ static INT_PTR __cdecl EMPGetName(WPARAM wParam,LPARAM lParam)
static INT_PTR __cdecl EMPLoadIcon(WPARAM wParam, LPARAM)
{
- UINT id = IDI_ICON_ONLINE;
+ UINT id = IDI_ICON_ONLINE;
if ((wParam & 0xFFFF) == PLI_OFFLINE)
- id = IDI_ICON_OFFLINE;
- HICON res = LoadIcon(g_hInstance, MAKEINTRESOURCE(id));
+ id = IDI_ICON_OFFLINE;
+ HICON res = LoadIcon(g_hInstance, MAKEINTRESOURCE(id));
return (INT_PTR)res;
}
-static INT_PTR __cdecl EMPGetStatus(WPARAM ,LPARAM)
+static INT_PTR __cdecl EMPGetStatus(WPARAM, LPARAM)
{
return g_lan->GetMirandaStatus();
}
@@ -104,23 +103,23 @@ INT_PTR __cdecl EMPSetStatus(WPARAM new_status, LPARAM lParam)
return 0;
}
-static INT_PTR __cdecl EMPSendMessage(WPARAM ,LPARAM lParam)
+static INT_PTR __cdecl EMPSendMessage(WPARAM, LPARAM lParam)
{
return g_lan->SendMessageUrl((CCSDATA*)lParam, false);
}
-static INT_PTR __cdecl EMPSendUrl(WPARAM ,LPARAM lParam)
+static INT_PTR __cdecl EMPSendUrl(WPARAM, LPARAM lParam)
{
return g_lan->SendMessageUrl((CCSDATA*)lParam, true);
}
-static INT_PTR __cdecl EMPRecvMessageUrl(WPARAM ,LPARAM lParam)
+static INT_PTR __cdecl EMPRecvMessageUrl(WPARAM, LPARAM lParam)
{
g_lan->RecvMessageUrl((CCSDATA*)lParam);
return 0;
}
-static INT_PTR __cdecl EMPAddToList(WPARAM flags,LPARAM lParam)
+static INT_PTR __cdecl EMPAddToList(WPARAM flags, LPARAM lParam)
{
return g_lan->AddToContactList((u_int)flags, (EMPSEARCHRESULT*)lParam);
}
@@ -188,9 +187,8 @@ INT_PTR CALLBACK EMPDlgProcMainOpts(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPAR
int count = g_lan->GetHostAddrCount();
in_addr caddr = g_lan->GetCurHostAddress();
int cind = 0;
- for (int i=0; i<count; i++)
- {
- in_addr addr = g_lan->GetHostAddress(i);
+ for (int i = 0; i < count; i++) {
+ in_addr addr = g_lan->GetHostAddress(i);
char* ipStr = inet_ntoa(addr);
if (addr.S_un.S_addr == caddr.S_un.S_addr)
cind = i;
@@ -198,14 +196,12 @@ INT_PTR CALLBACK EMPDlgProcMainOpts(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPAR
}
SendDlgItemMessage(hwndDlg, IDC_LIST_IP, LB_SETCURSEL, cind, 0);
SetDlgItemText(hwndDlg, IDC_EDIT_NAME, g_lan->GetName());
- if (g_lan->GetUseHostName())
- {
+ if (g_lan->GetUseHostName()) {
CheckDlgButton(hwndDlg, IDC_RADIO_USECOMPNAME, BST_CHECKED);
CheckDlgButton(hwndDlg, IDC_RADIO_USEOWN, BST_UNCHECKED);
EnableWindow(GetDlgItem(hwndDlg, IDC_EDIT_NAME), FALSE);
}
- else
- {
+ else {
CheckDlgButton(hwndDlg, IDC_RADIO_USECOMPNAME, BST_UNCHECKED);
CheckDlgButton(hwndDlg, IDC_RADIO_USEOWN, BST_CHECKED);
EnableWindow(GetDlgItem(hwndDlg, IDC_EDIT_NAME), TRUE);
@@ -220,7 +216,7 @@ INT_PTR CALLBACK EMPDlgProcMainOpts(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPAR
break;
bool changed = false;
- switch(LOWORD(wParam)) {
+ switch (LOWORD(wParam)) {
case IDC_RADIO_USECOMPNAME:
g_lan->SetUseHostName(true);
EnableWindow(GetDlgItem(hwndDlg, IDC_EDIT_NAME), FALSE);
@@ -234,29 +230,26 @@ INT_PTR CALLBACK EMPDlgProcMainOpts(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPAR
case IDC_LIST_IP:
{
u_long addr = g_lan->GetHostAddress(SendDlgItemMessage(hwndDlg, IDC_LIST_IP, LB_GETCURSEL, 0, 0)).S_un.S_addr;
- if (addr != g_lan->GetCurHostAddress().S_un.S_addr)
- {
+ if (addr != g_lan->GetCurHostAddress().S_un.S_addr) {
g_lan->SetRequiredIp(addr);
changed = true;
}
}
break;
case IDC_EDIT_NAME:
- if (HIWORD(wParam)==EN_CHANGE)
+ if (HIWORD(wParam) == EN_CHANGE)
changed = true;
break;
}
if (changed)
- SendMessage(GetParent(hwndDlg), PSM_CHANGED,0,0);
+ SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
}
break;
case WM_NOTIFY:
- switch(((LPNMHDR)lParam)->idFrom)
- {
+ switch (((LPNMHDR)lParam)->idFrom) {
case 0:
- switch (((LPNMHDR)lParam)->code)
- {
- case PSN_RESET:
+ switch (((LPNMHDR)lParam)->code) {
+ case PSN_RESET:
g_lan->LoadSettings();
return TRUE;
case PSN_APPLY:
@@ -279,7 +272,7 @@ INT_PTR CALLBACK EMPDlgProcMainOpts(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPAR
return FALSE;
}
-int __cdecl EMPCreateOptionsDlg(WPARAM wParam,LPARAM)
+int __cdecl EMPCreateOptionsDlg(WPARAM wParam, LPARAM)
{
OPTIONSDIALOGPAGE odp = { 0 };
odp.position = 100000000;
@@ -298,45 +291,35 @@ int __cdecl EMPCreateOptionsDlg(WPARAM wParam,LPARAM)
INT_PTR CALLBACK EMPDlgProcMessage(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
+ HWND hwndOwner;
+ RECT rc, rcDlg, rcOwner;
+
switch (uMsg) {
case WM_INITDIALOG:
- {
- HWND hwndOwner;
- RECT rc, rcDlg, rcOwner;
- if ((hwndOwner = GetParent(hwndDlg)) == NULL)
- {
- hwndOwner = GetDesktopWindow();
- }
+ if ((hwndOwner = GetParent(hwndDlg)) == NULL)
+ hwndOwner = GetDesktopWindow();
- GetWindowRect(hwndOwner, &rcOwner);
- GetWindowRect(hwndDlg, &rcDlg);
- CopyRect(&rc, &rcOwner);
-
- OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
- OffsetRect(&rc, -rc.left, -rc.top);
- OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
-
- SetWindowPos(hwndDlg,
- HWND_TOP,
- rcOwner.left + (rc.right / 2),
- rcOwner.top + (rc.bottom / 2),
- 0, 0, // ignores size arguments
- SWP_NOSIZE);
-
- if (GetDlgCtrlID((HWND) wParam) != IDOK) {
- SetFocus(GetDlgItem(hwndDlg, IDOK));
- return FALSE;
- }
- return TRUE;
+ GetWindowRect(hwndOwner, &rcOwner);
+ GetWindowRect(hwndDlg, &rcDlg);
+ CopyRect(&rc, &rcOwner);
+
+ OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
+ OffsetRect(&rc, -rc.left, -rc.top);
+ OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
+ SetWindowPos(hwndDlg, HWND_TOP, rcOwner.left + (rc.right / 2), rcOwner.top + (rc.bottom / 2), 0, 0, SWP_NOSIZE);
+
+ if (GetDlgCtrlID((HWND)wParam) != IDOK) {
+ SetFocus(GetDlgItem(hwndDlg, IDOK));
+ return FALSE;
}
- break;
+ return TRUE;
case WM_CLOSE:
EndDialog(hwndDlg, 0);
return TRUE;
case WM_COMMAND:
- switch(LOWORD(wParam)) {
+ switch (LOWORD(wParam)) {
case IDOK:
EndDialog(hwndDlg, 0);
return TRUE;
@@ -377,15 +360,12 @@ extern "C" int __declspec(dllexport) __cdecl Load()
CreateProtoServiceFunction(PROTONAME, PSS_FILE, EMPSendFile);
CreateProtoServiceFunction(PROTONAME, PSR_FILE, EMPRecvFile);
- g_heOptions = HookEvent(ME_OPT_INITIALISE,EMPCreateOptionsDlg);
-
+ HookEvent(ME_OPT_INITIALISE, EMPCreateOptionsDlg);
return 0;
}
extern "C" int __declspec(dllexport) __cdecl Unload()
{
- UnhookEvent(g_heOptions);
-
delete g_lan;
return 0;
}
diff --git a/protocols/EmLanProto/src/lan.cpp b/protocols/EmLanProto/src/lan.cpp
index 1828e782b5..d473bca9df 100644
--- a/protocols/EmLanProto/src/lan.cpp
+++ b/protocols/EmLanProto/src/lan.cpp
@@ -22,19 +22,16 @@ CLan::~CLan()
void CLan::Startup()
{
WSADATA wsa;
- if (WSAStartup(MAKEWORD(2, 2), &wsa) == 0)
- {
+ if (WSAStartup(MAKEWORD(2, 2), &wsa) == 0) {
m_status = LS_OK;
m_mode = LM_ON;
char hostname[256];
- if (gethostname(hostname, 256) == 0)
- {
+ if (gethostname(hostname, 256) == 0) {
hostent* host = gethostbyname(hostname);
char** pAddr = host->h_addr_list;
m_hostAddrCount = 0;
- while (*pAddr && m_hostAddrCount < MAX_INTERNAL_IP)
- {
+ while (*pAddr && m_hostAddrCount < MAX_INTERNAL_IP) {
in_addr addr;
addr.S_un.S_addr = *((u_long*)(*pAddr));
m_hostAddr[m_hostAddrCount++] = addr;
@@ -42,13 +39,11 @@ void CLan::Startup()
}
m_curAddr = m_hostAddr[0];
}
- else
- {
+ else {
m_status = LS_CANT_GET_HOSTADDR;
}
}
- else
- {
+ else {
m_status = LS_OK;
m_mode = LM_OFF;
}
@@ -71,23 +66,20 @@ void CLan::StopListen()
if (m_mode == LM_OFF)
return;
- if (m_hListenThread)
- {
- TerminateThread(m_hListenThread, 0);
+ if (m_hListenThread) {
+ shutdown(m_income, SD_BOTH);
+ WaitForSingleObject(m_hListenThread, INFINITE);
m_hListenThread = NULL;
}
- if (m_hAcceptTCPThread)
- {
+ if (m_hAcceptTCPThread) {
TerminateThread(m_hAcceptTCPThread, 0);
m_hAcceptTCPThread = NULL;
}
- if (m_income != INVALID_SOCKET)
- {
+ if (m_income != INVALID_SOCKET) {
closesocket(m_income);
m_income = INVALID_SOCKET;
}
- if (m_filesoc != INVALID_SOCKET)
- {
+ if (m_filesoc != INVALID_SOCKET) {
closesocket(m_filesoc);
m_filesoc = INVALID_SOCKET;
}
@@ -101,16 +93,13 @@ void CLan::StartListen()
m_income = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
m_filesoc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (m_income == INVALID_SOCKET || m_filesoc == INVALID_SOCKET)
- {
+ if (m_income == INVALID_SOCKET || m_filesoc == INVALID_SOCKET) {
m_status = LS_CANT_CREATE_SOCKET;
StopListen();
}
- else
- {
+ else {
int enable = 1;
- if (setsockopt(m_income, SOL_SOCKET, SO_BROADCAST, (const char*)&enable, sizeof(enable)) != 0)
- {
+ if (setsockopt(m_income, SOL_SOCKET, SO_BROADCAST, (const char*)&enable, sizeof(enable)) != 0) {
m_mode = LM_ON;
m_status = LS_CANT_TURN_ON_BROADCAST;
StopListen();
@@ -121,24 +110,21 @@ void CLan::StartListen()
addr.sin_addr = m_curAddr;
addr.sin_family = AF_INET;
addr.sin_port = PORT_NUMBER;
- if (bind(m_income, (sockaddr*)&addr, sizeof(addr)) != 0)
- {
+ if (bind(m_income, (sockaddr*)&addr, sizeof(addr)) != 0) {
m_mode = LM_ON;
m_status = LS_CANT_BIND_SOCKET;
StopListen();
return;
}
- if (bind(m_filesoc, (sockaddr*)&addr, sizeof(addr)) != 0)
- {
+ if (bind(m_filesoc, (sockaddr*)&addr, sizeof(addr)) != 0) {
m_mode = LM_ON;
m_status = LS_CANT_BIND_SOCKET;
StopListen();
return;
}
- if (listen(m_filesoc, SOMAXCONN) != 0)
- {
+ if (listen(m_filesoc, SOMAXCONN) != 0) {
m_mode = LM_ON;
m_status = LS_CANT_START_LISTEN;
StopListen();
@@ -147,8 +133,7 @@ void CLan::StartListen()
m_hListenThread = mir_forkthread(ListenProc, (void*)this);
m_hAcceptTCPThread = mir_forkthread(AcceptTCPProc, (void*)this);
- if (m_hListenThread == NULL || m_hAcceptTCPThread == NULL)
- {
+ if (m_hListenThread == NULL || m_hAcceptTCPThread == NULL) {
m_mode = LM_ON;
m_status = LS_CANT_CREATE_THREADS;
StopListen();
@@ -159,8 +144,7 @@ void CLan::StartListen()
void CLan::SetCurHostAddress(in_addr addr)
{
- if (m_mode != LM_OFF)
- {
+ if (m_mode != LM_OFF) {
int oldMode = m_mode;
StopListen();
m_curAddr = addr;
@@ -177,19 +161,20 @@ void __cdecl CLan::ListenProc(void *lpParameter)
void CLan::Listen()
{
- if (m_mode == LM_LISTEN)
- {
+ if (m_mode == LM_LISTEN) {
char buf[65536];
- while (1)
- {
+ while (true) {
sockaddr_in addr;
int addrLen = sizeof(addr);
Sleep(20);
int recLen = recvfrom(m_income, buf, 65536, 0, (sockaddr*)&addr, &addrLen);
- if (recLen != SOCKET_ERROR)
- OnRecvPacket((u_char*)buf, recLen, addr.sin_addr);
+ if (recLen == SOCKET_ERROR)
+ break;
+
+ OnRecvPacket((u_char*)buf, recLen, addr.sin_addr);
}
}
+ m_hListenThread = NULL;
}
void CLan::SendPacketBroadcast(const u_char* mes, int len)
@@ -201,8 +186,7 @@ void CLan::SendPacketBroadcast(const u_char* mes, int len)
void CLan::SendPacket(in_addr addr, const u_char* mes, int len)
{
- if (m_mode == LM_LISTEN)
- {
+ if (m_mode == LM_LISTEN) {
sockaddr_in addrTo;
addrTo.sin_addr = addr;
addrTo.sin_family = AF_INET;
@@ -221,22 +205,21 @@ void __cdecl CLan::AcceptTCPProc(void *lpParameter)
void CLan::AcceptTCP()
{
- while (1)
- {
- SOCKET in_socket;
+ while (true) {
sockaddr_in addrFrom;
int addrLen = sizeof(addrFrom);
- in_socket = accept(m_filesoc, (sockaddr*)&addrFrom, &addrLen);
- if (in_socket != INVALID_SOCKET)
- {
- TTCPConnect* tcp_conn = new TTCPConnect;
- tcp_conn->m_addr = addrFrom.sin_addr.S_un.S_addr;
- tcp_conn->m_lan = this;
- tcp_conn->m_socket = in_socket;
- mir_forkthread(OnInTCPConnectionProc, (void*)tcp_conn);
- }
+ SOCKET in_socket = accept(m_filesoc, (sockaddr*)&addrFrom, &addrLen);
+ if (in_socket == SOCKET_ERROR)
+ break;
+
+ TTCPConnect* tcp_conn = new TTCPConnect;
+ tcp_conn->m_addr = addrFrom.sin_addr.S_un.S_addr;
+ tcp_conn->m_lan = this;
+ tcp_conn->m_socket = in_socket;
+ mir_forkthread(OnInTCPConnectionProc, (void*)tcp_conn);
Sleep(100);
}
+ m_hAcceptTCPThread = NULL;
}
void __cdecl CLan::OnInTCPConnectionProc(void *lpParameter)
@@ -258,8 +241,7 @@ SOCKET CLan::CreateTCPConnection(u_long addr, LPVOID lpParameter)
addrTo.sin_addr.S_un.S_addr = addr;
addrTo.sin_family = AF_INET;
addrTo.sin_port = PORT_NUMBER;
- if (connect(out_socket, (sockaddr*)&addrTo, sizeof(addrTo)) != 0)
- {
+ if (connect(out_socket, (sockaddr*)&addrTo, sizeof(addrTo)) != 0) {
closesocket(out_socket);
out_socket = INVALID_SOCKET;
}
diff --git a/protocols/EmLanProto/src/mlan.cpp b/protocols/EmLanProto/src/mlan.cpp
index f8d4f40c5c..daccd9332f 100644
--- a/protocols/EmLanProto/src/mlan.cpp
+++ b/protocols/EmLanProto/src/mlan.cpp
@@ -75,8 +75,7 @@ void CMLan::DeleteCache()
{
TContact* pCont = m_pRootContact;
m_pRootContact = NULL;
- while (pCont)
- {
+ while (pCont) {
delete[] pCont->m_nick;
TContact* pPrev = pCont->m_prev;
delete pCont;
@@ -93,23 +92,19 @@ int CMLan::GetMirandaStatus()
void CMLan::SetMirandaStatus(u_int status)
{
- if (status == ID_STATUS_INVISIBLE)
- {
+ if (status == ID_STATUS_INVISIBLE) {
ProtoBroadcastAck(PROTONAME, NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)m_mirStatus, m_mirStatus);
return;
}
u_int old_status = m_mirStatus;
m_mirStatus = status;
- if (old_status == ID_STATUS_OFFLINE && m_mirStatus != ID_STATUS_OFFLINE)
- {
+ if (old_status == ID_STATUS_OFFLINE && m_mirStatus != ID_STATUS_OFFLINE) {
StartChecking();
}
- else if (old_status != ID_STATUS_OFFLINE && m_mirStatus == ID_STATUS_OFFLINE)
- {
+ else if (old_status != ID_STATUS_OFFLINE && m_mirStatus == ID_STATUS_OFFLINE) {
StopChecking();
}
- else if (m_mirStatus != ID_STATUS_OFFLINE && m_mirStatus != old_status)
- {
+ else if (m_mirStatus != ID_STATUS_OFFLINE && m_mirStatus != old_status) {
RequestStatus(false);
}
@@ -140,17 +135,18 @@ void CMLan::StartChecking()
void CMLan::StopChecking()
{
- mir_cslock lck(m_csAccessClass);
- if (m_hCheckThread)
{
- TerminateThread(m_hCheckThread, 0);
- m_hCheckThread = NULL;
+ mir_cslock lck(m_csAccessClass);
+ if (m_hCheckThread) {
+ TerminateThread(m_hCheckThread, 0);
+ m_hCheckThread = NULL;
+ }
}
- mir_cslock lck2(m_csReceiveThreadLock);
+
m_mirStatus = ID_STATUS_OFFLINE;
RequestStatus(false);
StopListen();
-
+
for (TFileConnection *fc = m_pFileConnectionList; fc; fc = fc->m_pNext)
fc->Terminate();
@@ -168,21 +164,17 @@ void __cdecl CMLan::CheckProc(void *lpParameter)
void CMLan::Check()
{
- while (1)
- {
+ while (true) {
Sleep(MLAN_SLEEP);
mir_cslock lck(m_csAccessClass);
-
- for (TContact *cont = m_pRootContact; cont; cont = cont->m_prev)
- {
- if (cont->m_status != ID_STATUS_OFFLINE)
- {
+
+ for (TContact *cont = m_pRootContact; cont; cont = cont->m_prev) {
+ if (cont->m_status != ID_STATUS_OFFLINE) {
if (cont->m_time)
cont->m_time--;
if (cont->m_time == MLAN_TIMEOUT)
RequestStatus(true, cont->m_addr.S_un.S_addr);
- if (!cont->m_time)
- {
+ if (!cont->m_time) {
cont->m_status = ID_STATUS_OFFLINE;
MCONTACT hContact = FindContact(cont->m_addr, cont->m_nick, false, false, false);
if (hContact)
@@ -245,27 +237,20 @@ MCONTACT CMLan::FindContact(in_addr addr, const char* nick, bool add_to_list, bo
void CMLan::OnRecvPacket(u_char* mes, int len, in_addr from)
{
- mir_cslock lck(m_csReceiveThreadLock);
-
- if (len)
- {
+ if (len) {
TPacket pak;
ParsePacket(pak, mes, len);
- if (pak.idVersion != 0)
- {
+ if (pak.idVersion != 0) {
TContact* cont = m_pRootContact;
- while (cont)
- {
+ while (cont) {
if (cont->m_addr.S_un.S_addr == from.S_un.S_addr)
break;
cont = cont->m_prev;
}
- if (pak.idStatus)
- {
+ if (pak.idStatus) {
mir_cslock lck(m_csAccessClass);
- if (!cont)
- {
+ if (!cont) {
if (!pak.strName)
pak.strName = "Unknown";
cont = new TContact;
@@ -277,10 +262,8 @@ void CMLan::OnRecvPacket(u_char* mes, int len, in_addr from)
memcpy(cont->m_nick, pak.strName, nlen + 1);
m_pRootContact = cont;
}
- else
- {
- if (pak.strName && mir_strcmp(pak.strName, cont->m_nick) != 0)
- {
+ else {
+ if (pak.strName && mir_strcmp(pak.strName, cont->m_nick) != 0) {
delete[] cont->m_nick;
int nlen = (int)mir_strlen(pak.strName);
cont->m_nick = new char[nlen + 1];
@@ -292,31 +275,27 @@ void CMLan::OnRecvPacket(u_char* mes, int len, in_addr from)
u_int old_status = cont->m_status;
cont->m_status = pak.idStatus;
MCONTACT hContact = FindContact(cont->m_addr, cont->m_nick, false, false, false);
- if (hContact)
- {
+ if (hContact) {
db_set_w(hContact, PROTONAME, "Status", cont->m_status);
if (db_get_dw(hContact, PROTONAME, "RemoteVersion", 0) != cont->m_ver)
db_set_dw(hContact, PROTONAME, "RemoteVersion", cont->m_ver);
- if (old_status == ID_STATUS_OFFLINE)
- {
+ if (old_status == ID_STATUS_OFFLINE) {
u_int rip = cont->m_addr.S_un.S_addr;
int tip = (rip << 24) | ((rip & 0xff00) << 8) | ((rip & 0xff0000) >> 8) | (rip >> 24);
db_set_dw(hContact, PROTONAME, "IP", tip);
-// HOSTENT* host = gethostbyaddr((const char*)&rip, sizeof(rip), AF_INET);
-// if (host)
-// db_set_s(hContact, PROTONAME, "UID", host->h_name);
+ // HOSTENT* host = gethostbyaddr((const char*)&rip, sizeof(rip), AF_INET);
+ // if (host)
+ // db_set_s(hContact, PROTONAME, "UID", host->h_name);
}
}
}
if (pak.flReqStatus)
RequestStatus(false, from.S_un.S_addr);
- if (pak.strMessage)
- {
+ if (pak.strMessage) {
if (!cont)
RequestStatus(true, from.S_un.S_addr);
- else
- {
+ else {
PROTORECVEVENT pre = { 0 };
pre.timestamp = get_time();
pre.szMessage = pak.strMessage;
@@ -331,15 +310,13 @@ void CMLan::OnRecvPacket(u_char* mes, int len, in_addr from)
}
}
- if (pak.idAckMessage && cont)
- {
+ if (pak.idAckMessage && cont) {
MCONTACT hContact = FindContact(cont->m_addr, cont->m_nick, false, false, false);
if (hContact)
ProtoBroadcastAck(PROTONAME, hContact, pak.flIsUrl ? ACKTYPE_URL : ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, (HANDLE)pak.idAckMessage, 0);
}
- if (pak.strAwayMessage && cont)
- {
+ if (pak.strAwayMessage && cont) {
PROTORECVEVENT pre = { 0 };
pre.timestamp = get_time();
pre.szMessage = pak.strAwayMessage;
@@ -347,32 +324,30 @@ void CMLan::OnRecvPacket(u_char* mes, int len, in_addr from)
ProtoChainRecv(FindContact(cont->m_addr, cont->m_nick, true, false, false, cont->m_status), PSR_AWAYMSG, 0, (LPARAM)&pre);
}
- if (pak.idReqAwayMessage && cont)
- {
+ if (pak.idReqAwayMessage && cont) {
MCONTACT hContact = FindContact(cont->m_addr, cont->m_nick, true, false, false);
-// Removed - it causes that whoisreadingawaymessage plugin was not working
-// if (hContact)
-// {
-// int IcqStatus = 0;
-// switch (m_mirStatus)
-// {
-// case ID_STATUS_AWAY: IcqStatus = ICQ_MSGTYPE_GETAWAYMSG; break;
-// case ID_STATUS_NA: IcqStatus = ICQ_MSGTYPE_GETNAMSG; break;
-// case ID_STATUS_OCCUPIED: IcqStatus = ICQ_MSGTYPE_GETOCCUMSG; break;
-// case ID_STATUS_DND: IcqStatus = ICQ_MSGTYPE_GETDNDMSG; break;
-// case ID_STATUS_FREECHAT: IcqStatus = ICQ_MSGTYPE_GETFFCMSG; break;
-// }
-// // HACK: this is a real hack
-// db_set_dw(hContact, "ICQ", "UIN", 1/*0xffffffff*/);
-// NotifyEventHooks(m_hookIcqMsgReq, IcqStatus, 1/*0xffffffff*/);
-// db_unset(hContact, "ICQ", "UIN");
-// }
+ // Removed - it causes that whoisreadingawaymessage plugin was not working
+ // if (hContact)
+ // {
+ // int IcqStatus = 0;
+ // switch (m_mirStatus)
+ // {
+ // case ID_STATUS_AWAY: IcqStatus = ICQ_MSGTYPE_GETAWAYMSG; break;
+ // case ID_STATUS_NA: IcqStatus = ICQ_MSGTYPE_GETNAMSG; break;
+ // case ID_STATUS_OCCUPIED: IcqStatus = ICQ_MSGTYPE_GETOCCUMSG; break;
+ // case ID_STATUS_DND: IcqStatus = ICQ_MSGTYPE_GETDNDMSG; break;
+ // case ID_STATUS_FREECHAT: IcqStatus = ICQ_MSGTYPE_GETFFCMSG; break;
+ // }
+ // // HACK: this is a real hack
+ // db_set_dw(hContact, "ICQ", "UIN", 1/*0xffffffff*/);
+ // NotifyEventHooks(m_hookIcqMsgReq, IcqStatus, 1/*0xffffffff*/);
+ // db_unset(hContact, "ICQ", "UIN");
+ // }
mir_cslock lck(m_csAccessAwayMes);
char* mesAway = NULL;
- switch (m_mirStatus)
- {
+ switch (m_mirStatus) {
case ID_STATUS_AWAY: mesAway = m_amesAway; break;
case ID_STATUS_NA: mesAway = m_amesNa; break;
case ID_STATUS_OCCUPIED: mesAway = m_amesOccupied; break;
@@ -380,8 +355,7 @@ void CMLan::OnRecvPacket(u_char* mes, int len, in_addr from)
case ID_STATUS_FREECHAT: mesAway = m_amesFfc; break;
}
- if (mesAway)
- {
+ if (mesAway) {
TPacket npak;
memset(&npak, 0, sizeof(npak));
npak.idAckAwayMessage = pak.idReqAwayMessage;
@@ -410,8 +384,7 @@ void CMLan::RecvMessageUrl(CCSDATA* ccs)
dbei.timestamp = pre->timestamp;
dbei.flags = pre->flags & PREF_CREATEREAD ? DBEF_READ : 0;
dbei.cbBlob = (DWORD)mir_tstrlen(pre->szMessage) + 1;
- if (!mir_strcmp(ccs->szProtoService, PSR_URL))
- {
+ if (!mir_strcmp(ccs->szProtoService, PSR_URL)) {
dbei.cbBlob += 2 + (DWORD)mir_tstrlen(pre->szMessage + dbei.cbBlob + 1);
}
dbei.pBlob = (PBYTE)pre->szMessage;
@@ -444,14 +417,12 @@ int CMLan::SendMessageUrl(CCSDATA* ccs, bool isUrl)
{
int cid = GetRandomProcId();
size_t len;
- if (isUrl)
- {
+ if (isUrl) {
len = mir_tstrlen((char*)ccs->lParam);
((char*)ccs->lParam)[len] = 1;
}
TDataHolder* hold = new TDataHolder(ccs, cid, isUrl ? LEXT_SENDURL : LEXT_SENDMESSAGE, this);
- if (isUrl)
- {
+ if (isUrl) {
((char*)ccs->lParam)[len] = 0;
hold->msg[len] = 0;
}
@@ -483,8 +454,7 @@ int CMLan::RecvAwayMsg(CCSDATA* ccs)
void __cdecl CMLan::LaunchExt(void *lpParameter)
{
TDataHolder* hold = (TDataHolder*)lpParameter;
- switch (hold->op)
- {
+ switch (hold->op) {
case LEXT_SENDMESSAGE:
case LEXT_SENDURL:
hold->lan->SendMessageExt(hold);
@@ -506,10 +476,8 @@ void CMLan::SearchExt(TDataHolder* hold)
memset(&psr, 0, sizeof(psr));
psr.cbSize = sizeof(psr);
- for (TContact *cont = m_pRootContact; cont; cont = cont->m_prev)
- {
- if (mir_strcmp(hold->msg, cont->m_nick) == 0 || mir_strcmp(hold->msg, "*") == 0)
- {
+ for (TContact *cont = m_pRootContact; cont; cont = cont->m_prev) {
+ if (mir_strcmp(hold->msg, cont->m_nick) == 0 || mir_strcmp(hold->msg, "*") == 0) {
char buf[MAX_HOSTNAME_LEN];
mir_tstrcpy(buf, cont->m_nick);
size_t len = mir_tstrlen(buf);
@@ -533,13 +501,11 @@ void CMLan::SearchExt(TDataHolder* hold)
void CMLan::SendMessageExt(TDataHolder* hold)
{
Sleep(0);
- if (db_get_w(hold->hContact, PROTONAME, "Status", ID_STATUS_OFFLINE) == ID_STATUS_OFFLINE)
- {
+ if (db_get_w(hold->hContact, PROTONAME, "Status", ID_STATUS_OFFLINE) == ID_STATUS_OFFLINE) {
Sleep(20);
ProtoBroadcastAck(PROTONAME, hold->hContact, (hold->op == LEXT_SENDURL) ? ACKTYPE_URL : ACKTYPE_MESSAGE, ACKRESULT_FAILED, (HANDLE)hold->id, 0);
}
- else
- {
+ else {
TPacket pak;
memset(&pak, 0, sizeof(pak));
u_long addr = db_get_dw(hold->hContact, PROTONAME, "ipaddr", 0);
@@ -570,8 +536,7 @@ void CMLan::GetAwayMsgExt(TDataHolder* hold)
int CMLan::SetAwayMsg(u_int status, char* msg)
{
char** ppMsg;
- switch (status)
- {
+ switch (status) {
case ID_STATUS_AWAY:
ppMsg = &m_amesAway;
break;
@@ -624,8 +589,7 @@ u_char* CMLan::CreatePacket(TPacket& pak, int* pBufLen)
len += 1 + 1 + 2;
size_t nameLen;
- if (pak.strName)
- {
+ if (pak.strName) {
nameLen = mir_tstrlen(pak.strName);
len += 1 + 1 + nameLen + 1;
}
@@ -634,8 +598,7 @@ u_char* CMLan::CreatePacket(TPacket& pak, int* pBufLen)
len += 1 + 1;
size_t mesLen = 0;
- if (pak.strMessage)
- {
+ if (pak.strMessage) {
mesLen = mir_tstrlen(pak.strMessage);
if (pak.flIsUrl)
mesLen += 1 + mir_tstrlen(pak.strMessage + mesLen + 1);
@@ -649,8 +612,7 @@ u_char* CMLan::CreatePacket(TPacket& pak, int* pBufLen)
len += 1 + 1 + 4;
size_t awayLen = 0;
- if (pak.strAwayMessage)
- {
+ if (pak.strAwayMessage) {
awayLen = mir_tstrlen(pak.strAwayMessage);
len += 3 + 1 + 4 + awayLen + 1;
}
@@ -660,24 +622,21 @@ u_char* CMLan::CreatePacket(TPacket& pak, int* pBufLen)
u_char* buf = new u_char[len];
u_char* pb = buf;
- if (pak.idVersion)
- {
+ if (pak.idVersion) {
*pb++ = 1 + 4;
*pb++ = MCODE_SND_VERSION;
*((u_int*)pb) = pak.idVersion;
pb += sizeof(u_int);
}
- if (pak.idStatus)
- {
+ if (pak.idStatus) {
*pb++ = 3;
*pb++ = MCODE_SND_STATUS;
*((u_short*)pb) = pak.idStatus;
pb += sizeof(u_short);
}
- if (pak.strName)
- {
+ if (pak.strName) {
*pb++ = 1 + (BYTE)nameLen + 1;
*pb++ = MCODE_SND_NAME;
memcpy(pb, pak.strName, nameLen);
@@ -685,14 +644,12 @@ u_char* CMLan::CreatePacket(TPacket& pak, int* pBufLen)
*pb++ = 0;
}
- if (pak.flReqStatus)
- {
+ if (pak.flReqStatus) {
*pb++ = 2;
*pb++ = MCODE_REQ_STATUS;
}
- if (pak.strMessage)
- {
+ if (pak.strMessage) {
*pb++ = 255;
*((u_short*)pb) = 1 + 4 + (BYTE)mesLen + 1;
pb += sizeof(u_short);
@@ -708,8 +665,7 @@ u_char* CMLan::CreatePacket(TPacket& pak, int* pBufLen)
*pb++ = 0;
}
- if (pak.idAckMessage)
- {
+ if (pak.idAckMessage) {
*pb++ = 1 + 4;
if (pak.flIsUrl)
*pb++ = MCODE_ACK_URL;
@@ -719,16 +675,14 @@ u_char* CMLan::CreatePacket(TPacket& pak, int* pBufLen)
pb += sizeof(u_int);
}
- if (pak.idReqAwayMessage)
- {
+ if (pak.idReqAwayMessage) {
*pb++ = 1 + 4;
*pb++ = MCODE_REQ_AWAYMSG;
*((u_int*)pb) = pak.idReqAwayMessage;
pb += sizeof(u_int);
}
- if (pak.strAwayMessage)
- {
+ if (pak.strAwayMessage) {
*pb++ = 255;
*((u_short*)pb) = 1 + 4 + (BYTE)awayLen + 1;
pb += sizeof(u_short);
@@ -753,18 +707,15 @@ void CMLan::ParsePacket(TPacket& pak, u_char* buf, int len)
{
memset(&pak, 0, sizeof(pak));
u_char* buf_end = buf + len;
- while (*buf && buf < buf_end)
- {
+ while (*buf && buf < buf_end) {
int tlen = *buf++;
- if (tlen == 255)
- {
+ if (tlen == 255) {
tlen = *((u_short*)buf);
buf += sizeof(u_short);
}
u_char* pb = buf;
int comm = *pb++;
- switch (comm)
- {
+ switch (comm) {
case MCODE_SND_STATUS:
pak.idStatus = *((u_short*)pb);
break;
@@ -833,14 +784,11 @@ void CMLan::LoadSettings()
}
m_nameLen = (int)mir_tstrlen(m_name);
- if (GetStatus() != LM_LISTEN)
- {
+ if (GetStatus() != LM_LISTEN) {
int ipcount = GetHostAddrCount();
- for (int i = 0; i < ipcount; i++)
- {
+ for (int i = 0; i < ipcount; i++) {
in_addr addr = GetHostAddress(i);
- if (addr.S_un.S_addr == m_RequiredIp)
- {
+ if (addr.S_un.S_addr == m_RequiredIp) {
SetCurHostAddress(addr);
break;
}
@@ -865,16 +813,13 @@ CMLan::TFileConnection::TFileConnection()
CMLan::TFileConnection::~TFileConnection()
{
- if (m_pLan)
- {
+ if (m_pLan) {
m_pLan->FileRemoveFromList(this);
}
delete[] m_szDescription;
- if (m_szFiles)
- {
+ if (m_szFiles) {
char** cp = m_szFiles;
- while (*cp)
- {
+ while (*cp) {
delete[] * cp;
cp++;
}
@@ -890,18 +835,15 @@ int CMLan::TFileConnection::Recv(bool halt)
{
// It is supposed that we're having not less then 2 bytes buffer size :)
EMLOG("Checking for data");
- while (1)
- {
+ while (true) {
u_long len;
- if (ioctlsocket(m_socket, FIONREAD, &len) != 0)
- {
+ if (ioctlsocket(m_socket, FIONREAD, &len) != 0) {
EMLOGERR();
return FCS_TERMINATE;
}
if (len >= 3)
break;
- if (!halt)
- {
+ if (!halt) {
EMLOG("No data - halting Recv (only " << len << " bytes)");
m_recSize = -1;
delete[] m_buf;
@@ -909,8 +851,7 @@ int CMLan::TFileConnection::Recv(bool halt)
return FCS_OK;
}
Sleep(10);
- if (m_state == FCS_TERMINATE)
- {
+ if (m_state == FCS_TERMINATE) {
EMLOG("Terminate requested, exiting recv");
return FCS_TERMINATE;
}
@@ -920,13 +861,11 @@ int CMLan::TFileConnection::Recv(bool halt)
int res;
EMLOG("Receiving packet size");
res = recv(m_socket, (char*)&size, 3, 0);
- if (res == SOCKET_ERROR)
- {
+ if (res == SOCKET_ERROR) {
EMLOGERR();
return FCS_TERMINATE;
}
- if (size == 0)
- {
+ if (size == 0) {
EMLOG("Connection was gracefully closed - size is 0");
delete[] m_buf;
m_buf = NULL;
@@ -934,23 +873,23 @@ int CMLan::TFileConnection::Recv(bool halt)
return FCS_OK;
}
- Lock();
- delete[] m_buf;
- m_buf = new u_char[size];
- m_recSize = size;
+ {
+ mir_cslock lck(m_csAccess);
+ delete[] m_buf;
+ m_buf = new u_char[size];
+ m_recSize = size;
+ }
EMLOG("Waiting for the whole packet (" << size << " bytes)");
- int csize = 0;
- while (csize != size)
- {
- while (1)
- {
+ u_long csize = 0;
+ while (csize != size) {
+ while (true) {
u_long len;
if (ioctlsocket(m_socket, FIONREAD, &len) != 0) {
EMLOGERR();
return FCS_TERMINATE;
}
- if (len >= min(size, FILE_MIN_BLOCK))
+ if (len >= min(u_long(size), FILE_MIN_BLOCK))
break;
Sleep(10);
if (m_state == FCS_TERMINATE) {
@@ -959,8 +898,10 @@ int CMLan::TFileConnection::Recv(bool halt)
}
}
EMLOG("Getting data (approx " << size << " bytes)");
- Lock();
- res = recv(m_socket, (char*)m_buf + csize, size - csize, 0);
+ {
+ mir_cslock lck(m_csAccess);
+ res = recv(m_socket, (char*)m_buf + csize, size - csize, 0);
+ }
EMLOGERR();
EMLOGIF("Connection was gracefully closed", res == 0);
if (res == 0 || res == SOCKET_ERROR)
@@ -975,24 +916,20 @@ int CMLan::TFileConnection::Recv(bool halt)
int CMLan::TFileConnection::SendRaw(u_char* buf, int size)
{
- while (size > 0)
- {
- if (m_state == FCS_TERMINATE)
- {
+ while (size > 0) {
+ if (m_state == FCS_TERMINATE) {
EMLOG("Terminate requested, exiting sendraw");
return FCS_TERMINATE;
}
int err = send(m_socket, (char*)buf, size, 0);
- if (err == SOCKET_ERROR)
- {
+ if (err == SOCKET_ERROR) {
EMLOGERR();
return FCS_TERMINATE;
}
size -= err;
buf += err;
EMLOGIF("Send " << err << " bytes", size == 0);
- if (size > 0)
- {
+ if (size > 0) {
EMLOG("Partial send (only " << err << " bytes");
Sleep(10);
}
@@ -1002,8 +939,7 @@ int CMLan::TFileConnection::SendRaw(u_char* buf, int size)
int CMLan::TFileConnection::Send(u_char* buf, int size)
{
- if (m_state == FCS_TERMINATE)
- {
+ if (m_state == FCS_TERMINATE) {
EMLOG("Terminate requested, exiting send");
return FCS_TERMINATE;
}
@@ -1020,7 +956,7 @@ int CMLan::TFileConnection::Send(u_char* buf, int size)
void CMLan::FileAddToList(TFileConnection* conn)
{
mir_cslock lck(m_csFileConnectionList);
- conn->Lock();
+ mir_cslock connLck(conn->m_csAccess);
conn->m_pNext = m_pFileConnectionList;
conn->m_pPrev = NULL;
if (m_pFileConnectionList)
@@ -1032,7 +968,7 @@ void CMLan::FileAddToList(TFileConnection* conn)
void CMLan::FileRemoveFromList(TFileConnection* conn)
{
mir_cslock lck(m_csFileConnectionList);
- conn->Lock();
+ mir_cslock connLck(conn->m_csAccess);
if (conn->m_pPrev)
conn->m_pPrev->m_pNext = conn->m_pNext;
else
@@ -1079,8 +1015,7 @@ void CMLan::OnInTCPConnection(u_long addr, SOCKET in_sock)
conn->m_socket = in_sock;
conn->m_cid = GetRandomProcId();
- if (conn->Recv() || conn->m_recSize == 0 || conn->m_buf[0] != FCODE_SND_FILEREQ)
- {
+ if (conn->Recv() || conn->m_recSize == 0 || conn->m_buf[0] != FCODE_SND_FILEREQ) {
EMLOG("Not passed synchro data");
EMLOGIF("Rec size is 0", conn->m_recSize == 0);
EMLOGIF("Wrong data in packet", conn->m_buf[0] != FCODE_SND_FILEREQ);
@@ -1103,8 +1038,7 @@ void CMLan::OnInTCPConnection(u_long addr, SOCKET in_sock)
conn->m_szFiles = new char*[rcTotalFiles + 1];
conn->m_szFiles[rcTotalFiles] = NULL;
- for (int i = 0; i < rcTotalFiles; i++)
- {
+ for (int i = 0; i < rcTotalFiles; i++) {
conn->m_szFiles[i] = _strdup(pf_fr);
if (i)
*pf_to++ = ' ';
@@ -1130,19 +1064,16 @@ void CMLan::OnInTCPConnection(u_long addr, SOCKET in_sock)
while (!conn->m_state)
Sleep(10);
- if (conn->m_state != TFileConnection::FCS_ALLOW)
- {
+ if (conn->m_state != TFileConnection::FCS_ALLOW) {
conn->Send(NULL, 0);
delete conn;
return;
}
- conn->Lock();
conn->m_state = TFileConnection::FCS_OK;
u_char buf = FCODE_SND_ACCEPT;
- if (conn->Send(&buf, 1))
- {
+ if (conn->Send(&buf, 1)) {
ProtoBroadcastAck(PROTONAME, conn->m_hContact, ACKTYPE_FILE, ACKRESULT_FAILED, (HANDLE)conn->m_cid, (LPARAM)"Connection aborted");
delete conn;
return;
@@ -1152,13 +1083,11 @@ void CMLan::OnInTCPConnection(u_long addr, SOCKET in_sock)
char path[MAX_PATH];
char* pathpart;
GetFullPathName(conn->m_szDir, MAX_PATH, path, &pathpart);
- if (!SetCurrentDirectory(path))
- {
+ if (!SetCurrentDirectory(path)) {
if (rcTotalFiles == 1)
conn->m_szRenamedFile = _strdup(pathpart);
*pathpart = 0;
- if (!SetCurrentDirectory(path))
- {
+ if (!SetCurrentDirectory(path)) {
conn->Send(NULL, 0);
ProtoBroadcastAck(PROTONAME, conn->m_hContact, ACKTYPE_FILE, ACKRESULT_FAILED, (HANDLE)conn->m_cid, (LPARAM)"Can't open output directory");
delete conn;
@@ -1181,11 +1110,9 @@ void CMLan::OnInTCPConnection(u_long addr, SOCKET in_sock)
bool err = false;
- for (int fileNo = 0; fileNo < rcTotalFiles; fileNo++)
- {
+ for (int fileNo = 0; fileNo < rcTotalFiles; fileNo++) {
EMLOG("Waiting for 'next file'");
- if (conn->Recv() || conn->m_recSize == 0 || conn->m_buf[0] != FCODE_SND_NEXTFILE)
- {
+ if (conn->Recv() || conn->m_recSize == 0 || conn->m_buf[0] != FCODE_SND_NEXTFILE) {
err = true;
break;
}
@@ -1198,20 +1125,16 @@ void CMLan::OnInTCPConnection(u_long addr, SOCKET in_sock)
fts.currentFileTime = get_time();
EMLOG("Waiting for ACCEPT");
- if (!ProtoBroadcastAck(PROTONAME, conn->m_hContact, ACKTYPE_FILE, ACKRESULT_FILERESUME, (HANDLE)conn->m_cid, (LPARAM)&fts))
- {
- conn->Lock();
+ if (!ProtoBroadcastAck(PROTONAME, conn->m_hContact, ACKTYPE_FILE, ACKRESULT_FILERESUME, (HANDLE)conn->m_cid, (LPARAM)&fts)) {
conn->m_state = TFileConnection::FCS_OVERWRITE;
}
- else
- {
+ else {
while (!conn->m_state)
Sleep(10);
}
EMLOG("Ok");
EMLOG("Checking if we're terminated");
- if (conn->m_state == TFileConnection::FCS_TERMINATE)
- {
+ if (conn->m_state == TFileConnection::FCS_TERMINATE) {
err = true;
break;
}
@@ -1220,14 +1143,11 @@ void CMLan::OnInTCPConnection(u_long addr, SOCKET in_sock)
u_char snd_buf[5];
EMLOG("Checking if we're skipping file");
- if (conn->m_state == TFileConnection::FCS_SKIP)
- {
+ if (conn->m_state == TFileConnection::FCS_SKIP) {
EMLOG("Skipped");
- conn->Lock();
conn->m_state = TFileConnection::FCS_OK;
snd_buf[0] = FCODE_SND_FILESKIP;
- if (conn->Send(snd_buf, 1))
- {
+ if (conn->Send(snd_buf, 1)) {
EMLOG("Error during sending 'skip' code'");
err = true;
break;
@@ -1244,13 +1164,11 @@ void CMLan::OnInTCPConnection(u_long addr, SOCKET in_sock)
if (conn->m_state == TFileConnection::FCS_RESUME)
mode_open = OPEN_ALWAYS;
- conn->Lock();
conn->m_state = TFileConnection::FCS_OK;
EMLOG("Creating file");
HANDLE hFile = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL, mode_open, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE)
- {
+ if (hFile == INVALID_HANDLE_VALUE) {
EMLOG("Can't create file");
conn->Send(NULL, 0);
ProtoBroadcastAck(PROTONAME, conn->m_hContact, ACKTYPE_FILE, ACKRESULT_FAILED, (HANDLE)conn->m_cid, (LPARAM)"Can't create file");
@@ -1268,8 +1186,7 @@ void CMLan::OnInTCPConnection(u_long addr, SOCKET in_sock)
fts.totalProgress += fsize;
EMLOG("Sending ack");
- if (conn->Send(snd_buf, 5))
- {
+ if (conn->Send(snd_buf, 5)) {
EMLOG("Error sending ACK");
CloseHandle(hFile);
err = true;
@@ -1281,12 +1198,10 @@ void CMLan::OnInTCPConnection(u_long addr, SOCKET in_sock)
ProtoBroadcastAck(PROTONAME, conn->m_hContact, ACKTYPE_FILE, ACKRESULT_DATA, (HANDLE)conn->m_cid, (LPARAM)&fts);
EMLOG("Ok");
int refr = 0;
- while (fts.currentFileProgress < fts.currentFileSize)
- {
+ while (fts.currentFileProgress < fts.currentFileSize) {
EMLOG("Waiting for data");
BOOL isErr = conn->Recv();
- if (isErr || conn->m_recSize == 0 || conn->m_buf[0] != FCODE_SND_FILEDATA)
- {
+ if (isErr || conn->m_recSize == 0 || conn->m_buf[0] != FCODE_SND_FILEDATA) {
EMLOGIF("Error conn->Recv()", isErr);
EMLOGIF("Error conn->m_recSize!=0", conn->m_recSize == 0);
EMLOGIF("Error conn->m_buf[0]==FCODE_SND_FILEDATA", conn->m_buf[0] != FCODE_SND_FILEDATA);
@@ -1302,8 +1217,7 @@ void CMLan::OnInTCPConnection(u_long addr, SOCKET in_sock)
fts.currentFileProgress += conn->m_recSize - 1;
fts.totalProgress += conn->m_recSize - 1;
refr += conn->m_recSize - 1;
- if (refr >= FILE_INFO_REFRESH)
- {
+ if (refr >= FILE_INFO_REFRESH) {
EMLOG("Refreshing progress bar");
ProtoBroadcastAck(PROTONAME, conn->m_hContact, ACKTYPE_FILE, ACKRESULT_DATA, (HANDLE)conn->m_cid, (LPARAM)&fts);
refr = 0;
@@ -1335,8 +1249,7 @@ void CMLan::OnOutTCPConnection(u_long addr, SOCKET out_socket, LPVOID lpParamete
EMLOG("Sending OUT TCP connection");
TFileConnection* conn = (TFileConnection*)lpParameter;
- if (out_socket == INVALID_SOCKET)
- {
+ if (out_socket == INVALID_SOCKET) {
EMLOG("Can't create OUT socket");
ProtoBroadcastAck(PROTONAME, conn->m_hContact, ACKTYPE_FILE, ACKRESULT_FAILED, (HANDLE)conn->m_cid, (LPARAM)"Can't initiate transfer");
delete conn;
@@ -1358,13 +1271,11 @@ void CMLan::OnOutTCPConnection(u_long addr, SOCKET out_socket, LPVOID lpParamete
int size = 0;
int filecount = 0;
char** pf = conn->m_szFiles;
- while (*pf)
- {
+ while (*pf) {
// TODO: FIX IT !
EMLOG("Opening file");
HANDLE hFile = CreateFile(*pf, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
- if (hFile == INVALID_HANDLE_VALUE)
- {
+ if (hFile == INVALID_HANDLE_VALUE) {
EMLOG("Can't open file for reading");
ProtoBroadcastAck(PROTONAME, conn->m_hContact, ACKTYPE_FILE, ACKRESULT_FAILED, (HANDLE)conn->m_cid, (LPARAM)"Can't open one of the files");
delete conn;
@@ -1403,8 +1314,7 @@ void CMLan::OnOutTCPConnection(u_long addr, SOCKET out_socket, LPVOID lpParamete
fts.pszFiles = conn->m_szFiles;
EMLOG("Sending file size");
- if (conn->Send(buf, len))
- {
+ if (conn->Send(buf, len)) {
EMLOG("Failed");
ProtoBroadcastAck(PROTONAME, conn->m_hContact, ACKTYPE_FILE, ACKRESULT_FAILED, (HANDLE)conn->m_cid, (LPARAM)"Connection aborted");
delete conn;
@@ -1412,8 +1322,7 @@ void CMLan::OnOutTCPConnection(u_long addr, SOCKET out_socket, LPVOID lpParamete
}
EMLOG("Waiting for ACK");
- if (conn->Recv() || conn->m_recSize == 0 || conn->m_buf[0] != FCODE_SND_ACCEPT)
- {
+ if (conn->Recv() || conn->m_recSize == 0 || conn->m_buf[0] != FCODE_SND_ACCEPT) {
EMLOG("Failed");
ProtoBroadcastAck(PROTONAME, conn->m_hContact, ACKTYPE_FILE, ACKRESULT_DENIED, (HANDLE)conn->m_cid, 0);
delete conn;
@@ -1422,12 +1331,10 @@ void CMLan::OnOutTCPConnection(u_long addr, SOCKET out_socket, LPVOID lpParamete
bool err = false;
- for (int fileNo = 0; fileNo < filecount; fileNo++)
- {
+ for (int fileNo = 0; fileNo < filecount; fileNo++) {
EMLOG("Opening file for reading (once more)");
HANDLE hFile = CreateFile(conn->m_szFiles[fileNo], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE)
- {
+ if (hFile == INVALID_HANDLE_VALUE) {
EMLOG("Failed");
conn->Send(NULL, 0);
ProtoBroadcastAck(PROTONAME, conn->m_hContact, ACKTYPE_FILE, ACKRESULT_FAILED, (HANDLE)conn->m_cid, (LPARAM)"Can't open file");
@@ -1444,8 +1351,7 @@ void CMLan::OnOutTCPConnection(u_long addr, SOCKET out_socket, LPVOID lpParamete
int fsize = GetFileSize(hFile, NULL);
*((int*)(snd_buf + 1)) = fsize;
EMLOG("Sending file size");
- if (conn->Send(snd_buf, 5))
- {
+ if (conn->Send(snd_buf, 5)) {
CloseHandle(hFile);
err = true;
break;
@@ -1453,16 +1359,14 @@ void CMLan::OnOutTCPConnection(u_long addr, SOCKET out_socket, LPVOID lpParamete
EMLOG("Ok");
EMLOG("Waiting for ACK");
- if (conn->Recv() || conn->m_recSize == 0 || (conn->m_buf[0] != FCODE_SND_ACCEPT && conn->m_buf[0] != FCODE_SND_FILESKIP))
- {
+ if (conn->Recv() || conn->m_recSize == 0 || (conn->m_buf[0] != FCODE_SND_ACCEPT && conn->m_buf[0] != FCODE_SND_FILESKIP)) {
CloseHandle(hFile);
err = true;
break;
}
EMLOG("Ok");
- if (conn->m_buf[0] != FCODE_SND_FILESKIP)
- {
+ if (conn->m_buf[0] != FCODE_SND_FILESKIP) {
EMLOG("File is not skipped");
int filepos = *((int*)(conn->m_buf + 1));
SetFilePointer(hFile, filepos, NULL, FILE_BEGIN);
@@ -1480,8 +1384,7 @@ void CMLan::OnOutTCPConnection(u_long addr, SOCKET out_socket, LPVOID lpParamete
fsize -= filepos;
- while (fsize > 0)
- {
+ while (fsize > 0) {
DWORD readbytes;
int tosend = FILE_SEND_BLOCK;
if (tosend > fsize)
@@ -1491,8 +1394,7 @@ void CMLan::OnOutTCPConnection(u_long addr, SOCKET out_socket, LPVOID lpParamete
EMLOG("Ok");
buf[0] = FCODE_SND_FILEDATA;
- if (readbytes != tosend)
- {
+ if (readbytes != tosend) {
EMLOG("Error during reading file. File was changed");
CloseHandle(hFile);
conn->Send(NULL, 0);
@@ -1501,8 +1403,7 @@ void CMLan::OnOutTCPConnection(u_long addr, SOCKET out_socket, LPVOID lpParamete
return;
}
EMLOG("Sending data buffer");
- if (conn->Send(buf, tosend + 1))
- {
+ if (conn->Send(buf, tosend + 1)) {
//CloseHandle(hFile);
err = true;
break;
@@ -1513,14 +1414,12 @@ void CMLan::OnOutTCPConnection(u_long addr, SOCKET out_socket, LPVOID lpParamete
fts.totalProgress += tosend;
fsize -= tosend;
refr += tosend;
- if (refr >= FILE_INFO_REFRESH || fsize <= 0)
- {
+ if (refr >= FILE_INFO_REFRESH || fsize <= 0) {
EMLOG("Refreshing file info");
ProtoBroadcastAck(PROTONAME, conn->m_hContact, ACKTYPE_FILE, ACKRESULT_DATA, (HANDLE)conn->m_cid, (LPARAM)&fts);
refr = 0;
EMLOG("Checking for 'abort'");
- if (conn->Recv(false) || conn->m_recSize != -1)
- {
+ if (conn->Recv(false) || conn->m_recSize != -1) {
EMLOG("Aborted");
//CloseHandle(hFile);
err = true;
@@ -1529,8 +1428,7 @@ void CMLan::OnOutTCPConnection(u_long addr, SOCKET out_socket, LPVOID lpParamete
EMLOG("Ok");
}
- if (conn->m_state)
- {
+ if (conn->m_state) {
EMLOG("Interrupted by user");
conn->Send(NULL, 0);
//CloseHandle(hFile);
@@ -1544,8 +1442,7 @@ void CMLan::OnOutTCPConnection(u_long addr, SOCKET out_socket, LPVOID lpParamete
break;
}
- if (err)
- {
+ if (err) {
EMLOG("There was error during file transfering");
conn->Send(NULL, 0);
ProtoBroadcastAck(PROTONAME, conn->m_hContact, ACKTYPE_FILE, ACKRESULT_FAILED, (HANDLE)conn->m_cid, (LPARAM)"Connection aborted");
@@ -1584,8 +1481,7 @@ int CMLan::FileAllow(CCSDATA* ccs)
{
int cid = (int)ccs->wParam;
TFileConnection* conn = m_pFileConnectionList;
- while (conn)
- {
+ while (conn) {
if (conn->m_cid == cid)
break;
conn = conn->m_pNext;
@@ -1593,7 +1489,7 @@ int CMLan::FileAllow(CCSDATA* ccs)
if (!conn)
return 0;
- conn->Lock();
+ mir_cslock connLck(conn->m_csAccess);
conn->m_state = TFileConnection::FCS_ALLOW;
conn->m_szDir = _strdup((char*)ccs->lParam);
return cid;
@@ -1603,8 +1499,7 @@ int CMLan::FileDeny(CCSDATA* ccs)
{
int cid = (int)ccs->wParam;
TFileConnection* conn = m_pFileConnectionList;
- while (conn)
- {
+ while (conn) {
if (conn->m_cid == cid)
break;
conn = conn->m_pNext;
@@ -1612,7 +1507,6 @@ int CMLan::FileDeny(CCSDATA* ccs)
if (!conn)
return 0;
- conn->Lock();
conn->m_state = TFileConnection::FCS_TERMINATE;
return 0;
}
@@ -1621,8 +1515,7 @@ int CMLan::FileCancel(CCSDATA* ccs)
{
int cid = (int)ccs->wParam;
TFileConnection* conn = m_pFileConnectionList;
- while (conn)
- {
+ while (conn) {
if (conn->m_cid == cid)
break;
conn = conn->m_pNext;
@@ -1630,7 +1523,6 @@ int CMLan::FileCancel(CCSDATA* ccs)
if (!conn)
return 0;
- conn->Lock();
conn->m_state = TFileConnection::FCS_TERMINATE;
return 0;
}
@@ -1641,8 +1533,7 @@ int CMLan::FileResume(int cid, PROTOFILERESUME* pfr)
//PROTOFILERESUME* pfr = (PROTOFILERESUME*)ccs->lParam;
TFileConnection* conn = m_pFileConnectionList;
- while (conn)
- {
+ while (conn) {
if (conn->m_cid == cid)
break;
conn = conn->m_pNext;
@@ -1650,9 +1541,8 @@ int CMLan::FileResume(int cid, PROTOFILERESUME* pfr)
if (!conn)
return 0;
- conn->Lock();
- switch (pfr->action)
- {
+ mir_cslock connLck(conn->m_csAccess);
+ switch (pfr->action) {
case FILERESUME_OVERWRITE:
conn->m_state = TFileConnection::FCS_OVERWRITE;
break;
diff --git a/protocols/EmLanProto/src/mlan.h b/protocols/EmLanProto/src/mlan.h
index 3eb65620d1..ed151e1212 100644
--- a/protocols/EmLanProto/src/mlan.h
+++ b/protocols/EmLanProto/src/mlan.h
@@ -96,7 +96,6 @@ private:
int m_nameLen;
mir_cs m_csAccessClass;
- mir_cs m_csReceiveThreadLock;
mir_cs m_csAccessAwayMes;
void RequestStatus(bool answer = false, u_long m_addr = INADDR_BROADCAST);
@@ -161,8 +160,7 @@ private:
TFileConnection();
~TFileConnection();
- void Lock() { mir_cslock lck(m_csAccess); }
- void Terminate() { Lock(); m_state = FCS_TERMINATE; }
+ void Terminate() { m_state = FCS_TERMINATE; }
int Recv(bool halt = true);
int Send(u_char* buf, int size);
int SendRaw(u_char* buf, int size);