|
That's around 10MByte/sec, thats quite OK, with good network 100MByte/sec can be achieved. You might want to set the send/receive buffer size of your sockets (setsockopt/SO_SNDBUF/SO_RCVBUF), without this sometimes its not possible to reach the best throughput with the default buffer sizes (I don't know why, found this out by experimenting). But this is not a parameter that decides between async/blocking. Its rather the protocol. When do you send and when do you receive data? If you can exactly determine when to receive and when to send then use blocking sockets because its easier (the typical protocol for blocking sockets is request - response, one of the peers is active and the other just replies). If you might want to send at arbitrary points and you might have to receive incoming packets at random points then you need to write an async solution that also handles when the bandwith isn't good enough to send data with the desired rate.
EDIT: Transferring data from one thread to another one: this stuff is called blocking queue. It is the most universal and often the most efficient way to send data/events from one thread to another. The worker thread has a blocking queue and it always gets the first item from it, if the queue is empty then the worker thread blocks on the get() method of the queue until another thread puts an item to it. After getting the item from the queue the worker thread processes that item (sends data on the socket) and then it goes back to the beginning of the loop and gets another item from the loop for processing. The other thread or threads can put data to the other side of the queue. The put method doesn't block. You should just store pointers in this queue, pointers to data objects to minimize the synchronization overhead (by avoiding data copying) in the blocking queue. Unfortunately I have no favorite threading tutorial but I'm currently working on a codeproject article about threading for dummies. That will describe threading step by step and will teach using blocking queues too.
EDIT: Since in your case the data flow goes to a single direction you should use a blocking socket. You should also handle somehow when your bandwidth goes too low, in that case you should avoid the blocking queue from growing ultra large causing an out of memory error. You should somehow suspend the generation of messages in that case because sooner or later you will not have space to store the newly generated messages.
EDIT: A very simple example to using a multi-producer single consumer blocking message queue (with only a single producer thread - the main thread):
#include <windows.h>
#include <stdio.h>
#include <assert.h>
#include <queue>
template <typename T>
class BlockingQueue
{
public:
BlockingQueue()
{
InitializeCriticalSection(&m_Lock);
m_hBecameNonEmptyEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
}
~BlockingQueue()
{
assert(m_Queue.empty());
DeleteCriticalSection(&m_Lock);
CloseHandle(m_hBecameNonEmptyEvent);
}
void Put(const T& item)
{
EnterCriticalSection(&m_Lock);
if (m_Queue.empty())
SetEvent(m_hBecameNonEmptyEvent);
m_Queue.push(item);
LeaveCriticalSection(&m_Lock);
}
void Get(T& item)
{
EnterCriticalSection(&m_Lock);
if (m_Queue.empty())
{
ResetEvent(m_hBecameNonEmptyEvent);
LeaveCriticalSection(&m_Lock);
WaitForSingleObject(m_hBecameNonEmptyEvent, INFINITE);
EnterCriticalSection(&m_Lock);
}
item = m_Queue.front();
m_Queue.pop();
LeaveCriticalSection(&m_Lock);
}
private:
BlockingQueue(const BlockingQueue&);
BlockingQueue& operator=(const BlockingQueue&);
private:
CRITICAL_SECTION m_Lock;
HANDLE m_hBecameNonEmptyEvent;
std::queue<T> m_Queue;
};
BlockingQueue<int> q;
DWORD __stdcall ThreadProc(void* param)
{
printf("thread started\n");
for (;;)
{
int i;
q.Get(i);
printf("processing %d\n", i);
if (i == 0)
break;
}
printf("thread is out of the loop, returning from threadproc...\n");
return 0;
}
int main(int argc, char* argv[])
{
printf("Creating thread...\n");
DWORD thread_id;
HANDLE hThread = ::CreateThread(NULL, 0, ThreadProc, NULL, 0, &thread_id);
if (!hThread)
{
fprintf(stderr, "CreateThread error!\n");
return 1;
}
for (;;)
{
char buf[0x100];
if (!gets(buf))
break;
int i;
if (1 == sscanf(buf, "%d", &i))
{
q.Put(i);
if (i == 0)
break;
}
else
{
printf("Not an integer: %s\n", buf);
}
}
printf("Waiting for the thread to finish...\n");
WaitForSingleObject(hThread, INFINITE);
printf("Wait finished.\n");
CloseHandle(hThread);
return 0;
}
You should store pointers in the blocking queue and you can use for example the NULL pointer to ask the other thread to exit like I used the zero value.
modified 27-Aug-12 16:11pm.
|
|
|
|
|
bkelly13 wrote: This application will be sending
Then why are you creating a listener?
bkelly13 wrote: This application will be sending between three and nine messages per millisecond
That means it must generate 9,000 messages a second.
To send that many the app must create that many. And given that you are suggesting that a message is 9k I doubt the creation is trivial.
|
|
|
|
|
Hello, my first post here.
I have a question. I have a pretty old PS2 optical mouse which has the standard two buttons, wheel + middle button, and it has two additional buttons for clicking with thumb. The mouse works great for a few years already, but I lost the drivers and they were on a floppy disk anyways.
I was wondering how can I get the input from these additional buttons, I wanna make a program in C/C++ which would get the input from the buttons and convert it to a virtual joystick. I can handle the joystick part, it's easy (I'm using PPJoy for this).
Please note that I never did any port programming, but I'm very good at anything else.
So, I'd like to know how to get the input from these keys. I'm using Win XP SP3.
And, please don't tell me to get a new mouse, I want to improve my C++ skills and get two additional keys for free
Thank you. David.
|
|
|
|
|
You should try these: WM_XBUTTONDOWN and related window messages, WM_INPUT window message, directinput, and here is a hack on codeproject that might come handy: How to use your extra mouse buttons in games or apps[^]. Note: I havent used WM_XBUTTON messages and used WM_INPUT only to get mouse movement delta instead of mouse pos but trying these isnt a big waste of time.
|
|
|
|
|
Hi all,
i am writing a program that opens an explorer window, when CreateProcess open new window the main program must be minimized when i will close Explorer.exe i want that program return in SW_SHOW state.
I simply need to detect when Explorer.exe will be Close.
I have seen that when i use createprocess with microsoft MSDN example it will check only one time.
What i have to do to complete my task please help me
|
|
|
|
|
It is possible to wait for a program to exit when you start it with CreateProcess() but in your case you shouldn't do that. Many text editor and browser and whatever programs work like this: They allow you to open many documents on tabs. When you use CreateProcess() to start the editing or viewing of a document (or a webpage in your case) you launch an instance of the editor program that does the following: It checks if another instance of that program is already running and if that is the case then the process you created is just passing some info to the already running process and the process you started exits immediately. The already running process shows your document/webpage on a new tab. You shouldn't check for closing the explorer because you can't for the reasons I listed previously, moreover, I know that many users never exit their web browser - they use it open all they with hundreds of tabs for some reason.
|
|
|
|
|
You can wait on a process handle:
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
BOOL bOk = CreateProcess(
_T("C:\\Windows\\system32\\notepad.exe"),
NULL,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi
);
if (bOk)
{
CloseHandle(pi.hThread);
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
MessageBox(NULL, _T("Finished"), _T("Create and wait"), MB_OK);
}
Steve
|
|
|
|
|
Thanks for ask
I have tried your solution and it goes good when i need to close my opened app,
but i need this:
i want open an explorer.exe window and when i close my caller program i need that program closes explorer window too.
Do you think it's possible?
|
|
|
|
|
Now I'm using VC++ 2010, but the
syntax_option_type of VC++ 2010 only contains the following options:
static const flag_type icase = regex_constants::icase;
static const flag_type nosubs = regex_constants::nosubs;
static const flag_type optimize = regex_constants::optimize;
static const flag_type collate = regex_constants::collate;
static const flag_type ECMAScript = regex_constants::ECMAScript;
static const flag_type basic = regex_constants::basic;
static const flag_type extended = regex_constants::extended;
static const flag_type awk = regex_constants::awk;
static const flag_type grep = regex_constants::grep;
static const flag_type egrep = regex_constants::egrep;
It doesn't contain
perl_syntax_group (Boost Library has the option). However, I don't want to use the Boost Library.
There are many regular expression written in Perl, So I want to convert the existing Perl regular expressions to
ECMAScript (or any one that VC++ 2010 support). After conversion I can use the equivalent regular expressions directly in VC++ 2010 without using the third party libray.
One example:
const boost::tregex e(__T("\\A(\\d{3,4})[- ]?(\\d{4})[- ]?(\\d{4})[- ]?(\\d{4})\\z"));
const CString human_format = __T("$1-$2-$3-$4");
CString human_readable_card_number(const CString& s)
{
return boost::regex_replace(s, e, human_format);
}
CString credit_card_number = "1234567887654321";
credit_card_number = human_readable_card_number(credit_card_number);
assert(credit_card_number == "1234-5678-8765-4321");
In the above example, what I want to do is convert e and format to ECMAScript style expressions.
ECMA Script regex is a subset of Perl regex, if I don't use the features exclusive to Perl regex.
Is it possible to find a general way to convert all Perl regular expressions to
ECMAScript style? Are there some tools to do this?
Any help will be appreciated!
|
|
|
|
|
I'm having a very strange problem with a third party dll. When I try to use this dll in my program an exception is thrown even before WinMain is called, yet other programs use it without any problems.
Eventually, I narrowed it down. If any program uses the dll and also calls a certain Windows function (for example SHGetSpecialFolderPath, or timeGetTime) then the the exception happens.
This problems seems really bizarre to me. Does anyone know what could be causing it?
|
|
|
|
|
You stated that the exception is thrown before your WinMain(). Statically linked dlls are loaded by windows for your executables and DLLs. The DllMain() of statically linked DLLs execute eralier than the main of the module (dll or exe) that actually uses the DLL. I would suspect that something is screwed up in your 3rd party DLL and it might be sensitive to DLL loading order. If you exe uses SHGetSpecialFolderPath() from shell32.dll and the 3rd party library also uses a function from shell.dll then the loading of shell32.dll might happen before or after the loading of your 3rd party depending on whether your program loads shell32.dll or the 3rd party first. However this whole situation seems to be totally screwed up for me. Try to set a breakpoint on "module load" events and debug the loading order of your DLLs, maybe you find out something useful. Try to search for the exception code too in google.
|
|
|
|
|
Post a call stack. Always post a call stack!
Steve
|
|
|
|
|
Ok:
> ntdll.dll!7c967406()
[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]
ntdll.dll!7c940e5a()
ntdll.dll!7c918790()
ntdll.dll!7c90e457()
libGLESv2.dll!00360062()
I doubt if that's any use though
|
|
|
|
|
The one useful bit is the last line that suggests the problem started in libGLESv2.dll. Assuming this is the 3rd party library you should contact the author(s) for help.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Yeah, I already contacted them, but no answer. I think I'm just going to give up and use an alterate library.
|
|
|
|
|
Use a symbol server when getting a stack trace.
Steve
|
|
|
|
|
ed welch wrote: yet other programs use it without any problems.
That suggests along with your other responses that something about the build, rather than usage, is different.
There are different ways of setting up the runtime environment based on linker options.
|
|
|
|
|
no, I comfirmed that it's not any compile or link option
|
|
|
|
|
hi !!!!! in my project am going to use prediction algorithm for predicting the rail track allocation for incoming trains in a station.. any one please help in finding the algorithm or the
C code .
|
|
|
|
|
Without knowing what the algorithm is it's a bit difficult to guess what the C code is. You say that you are going to use this algorithm so maybe you could provide details.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
|
Google is your friend. Moreover, we have a 'Algorithms' forum here at CodeProject .
Veni, vidi, vici.
|
|
|
|
|
How about reading the time table? I've found it to be surprisingly efficient at predicting what track each train would be incoming at.
Seriously:
1. go to the 'algorithms' sub forum. Until you have an algorithm, there is no C/C++ question to solve.
2. specify precisely the input you have and the output you want. Without that information it is impossible to find an algorithm. For all I know now, my answer above may in fact be sufficient (although I doubt it)
3. use the internet search engine of your choice and search for similar projects. You probably won't find exactly what you need, but if you see something similar, you can try and adapt it to your project, or ask for help in doing so.
|
|
|
|
|
Hi,
I have a dialog box. I have 10 buttons on that dialog box. I am using CSkinButton class to use images for those buttons. I want to provide scrollbar to the dialog box dynamically. For example, if I use 6 buttons, will fit the size of the dialog box. If I want to use 10 buttons, then vertical and horizontal scroll bar should appear on the dialog box automatically. As I using Images (Bitmaps) for buttons, scroll bar doesn't appear automatically.
I have tried to set the property of the dialog box for scroll bar. It doesn't work.
Anybody have anny idea regarding this.?
Please let me know if you have any doubt in understanding the above questions.
Regards,
Mbatra
|
|
|
|
|
This should be a good start.[^]
Can I advise you to re-think your UI to not have to have scrollbars AT ALL ? It is quite counter-intuitive for users to have to scroll in a dialog to be able to see buttons.
Max,
Watched code never compiles.
|
|
|
|