KVM test: rss.cpp: use critical section instead of mutex for text buffer access
PATCH 2/2 KVM test: rss.cpp: write all user messages to a log file
In reply to Michael GoldishThe log file will be placed in the current directory.
For example, when rss.exe is run at startup as administrator under WinXP, the current directory will be C:\Documents and Settings\Administrator.
Signed-off-by Michael Goldish <[email protected]>
--- client/tests/kvm/deps/rss.cpp | 21 +++++++++++++++++++++ 1 files changed, 21 insertions(+), 0 deletions(-)
client/tests/kvm/deps/rss.cpp +21 -0
@@ -107,6 +107,8 @@ int text_size = 0; CRITICAL_SECTION critical_section; +FILE *log_file; + struct client_info { SOCKET socket; char addr_str[256];
@@ -157,16 +159,22 @@ void ExitOnError(const char *message, BOOL winsock = FALSE) void FlushTextBuffer() { if (!text_size) return; + // Clear the text box if it contains too much text int len = GetWindowTextLength(hTextBox); while (len > TEXTBOX_LIMIT - sizeof(text_buffer)) { SendMessage(hTextBox, EM_SETSEL, 0, TEXTBOX_LIMIT * 1/4); SendMessage(hTextBox, EM_REPLACESEL, FALSE, (LPARAM)"..."); len = GetWindowTextLength(hTextBox); } + // Append the contents of text_buffer to the text box SendMessage(hTextBox, EM_SETSEL, len, len); SendMessage(hTextBox, EM_REPLACESEL, FALSE, (LPARAM)text_buffer); + // Clear text_buffer text_buffer[0] = 0; text_size = 0; + // Make sure the log file's buffer is flushed as well + if (log_file) + fflush(log_file); } void AppendMessage(const char *message, ...)
@@ -181,8 +189,13 @@ void AppendMessage(const char *message, ...) int len = strlen(str); EnterCriticalSection(&critical_section); + // Write message to the log file + if (log_file) + fwrite(str, len, 1, log_file); + // Flush the text buffer if necessary if (text_size + len + 1 > sizeof(text_buffer)) FlushTextBuffer(); + // Append message to the text buffer strcpy(text_buffer + text_size, str); text_size += len; LeaveCriticalSection(&critical_section);
@@ -881,6 +894,8 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { RECT rect; WSADATA wsaData; + SYSTEMTIME lt; + char log_filename[256]; switch (msg) { case WM_CREATE:
@@ -907,6 +922,12 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) SendMessage(hTextBox, EM_LIMITTEXT, TEXTBOX_LIMIT, 0); // Initialize critical section object for text buffer access InitializeCriticalSection(&critical_section); + // Open log file + GetLocalTime(<); + sprintf(log_filename, "rss_%02d-%02d-%02d_%02d-%02d-%02d.log", + lt.wYear, lt.wMonth, lt.wDay, + lt.wHour, lt.wMinute, lt.wSecond); + log_file = fopen(log_filename, "wb"); // Create text box update thread if (!CreateThread(NULL, 0, UpdateTextBox, NULL, 0, NULL)) ExitOnError("Could not create text box update thread");
Signature · 1 line
1.5.4.1
PATCH 1/2 KVM test: rss.cpp: use critical section instead of mutex for text buffer access
A critical section should be faster. The difference for this application may or may not be noticeable (with a large number of files).
Signed-off-by Michael Goldish <[email protected]>
--- client/tests/kvm/deps/rss.cpp | 15 ++++++++------- 1 files changed, 8 insertions(+), 7 deletions(-)
client/tests/kvm/deps/rss.cpp +8 -7
@@ -101,11 +101,12 @@ int file_transfer_port = 10023; HWND hMainWindow = NULL; HWND hTextBox = NULL; -HANDLE hTextBufferMutex = NULL; char text_buffer[8192] = {0}; int text_size = 0; +CRITICAL_SECTION critical_section; + struct client_info { SOCKET socket; char addr_str[256];
@@ -179,12 +180,12 @@ void AppendMessage(const char *message, ...) strcat(str, "\r\n"); int len = strlen(str); - WaitForSingleObject(hTextBufferMutex, INFINITE); + EnterCriticalSection(&critical_section); if (text_size + len + 1 > sizeof(text_buffer)) FlushTextBuffer(); strcpy(text_buffer + text_size, str); text_size += len; - ReleaseMutex(hTextBufferMutex); + LeaveCriticalSection(&critical_section); } // Flush the text buffer every 250 ms
@@ -192,9 +193,9 @@ DWORD WINAPI UpdateTextBox(LPVOID client_info_ptr) { while (1) { Sleep(250); - WaitForSingleObject(hTextBufferMutex, INFINITE); + EnterCriticalSection(&critical_section); FlushTextBuffer(); - ReleaseMutex(hTextBufferMutex); + LeaveCriticalSection(&critical_section); } return 0; }
@@ -904,8 +905,8 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) MAKELPARAM(FALSE, 0)); // Set size limit SendMessage(hTextBox, EM_LIMITTEXT, TEXTBOX_LIMIT, 0); - // Create mutex for text buffer access - hTextBufferMutex = CreateMutex(NULL, FALSE, NULL); + // Initialize critical section object for text buffer access + InitializeCriticalSection(&critical_section); // Create text box update thread if (!CreateThread(NULL, 0, UpdateTextBox, NULL, 0, NULL)) ExitOnError("Could not create text box update thread");
Signature · 1 line
1.5.4.1