• Welcome to Valhalla Legends Archive.
 

sockets (C++)

Started by K, April 05, 2003, 10:14 AM

Previous topic - Next topic

K

Hullo. I'm currently working on a (chat)bot in C++.  While my current code works, I'm getting the feeling that there's a better way this could be done, so I was looking for suggestions (not code, preferably). What I'm doing as of now is creating a seperate thread that polls the socket for data / the queue for outbound messages and if either are found processes them, and if not sleeps for a specified amount of time. This seems like a really poor idea to me as a programmer, so I was looking for alternatives.

TIA,
/K

[Edit: just read through the thread titled "BNCS Chatgate Event Handler" and found a good starting point. Suggestions still welcome, though.]

Kp

#1
What operating system are you designing for?  The efficient methods for Win32 and *nix are extremely different.  Specifically, the win32 one is win32-specific (surprise!) and the *nix one works very poorly under Win32.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

K

Sorry, should have specified Win32.  I've been looking at WSARecv and WSASend, but I'm not sure I understand exactly what the benefit of the callback function is.

Zakath

There's absolutely no need for a separate thread simply for socket processing: consider BinaryChat, which can use a dynamic number of Battle.net connections, and does ALL the GUI and socket handling in one thread.

The easiest way, in Windows GUI programming, is probably to use WSAAsyncSelect()...look it up in the MSDN.
Quote from: iago on February 02, 2005, 03:07 PM
Yes, you can't have everybody...contributing to the main source repository.  That would be stupid and create chaos.

Opensource projects...would be dumb.

K

Thanks Zakath, I had come to that conclusion which is why I posted my question here. I was looking at WSAAsyncSelect(), but in the thread I referenced, Skywing recommended against using it. Thanks for your reply.

Skywing

#5
Quote from: K on April 05, 2003, 01:53 PM
Sorry, should have specified Win32.  I've been looking at WSARecv and WSASend, but I'm not sure I understand exactly what the benefit of the callback function is.
Overlapped operations allow you to guarantee to the system that a buffer will remain valid for the entire operation, which in turn allows the system to take signifigant optimizations (such as not having to copy data to and from internal buffers).

Additionally, overlapped operations typically execute through the system's dedicated I/O worker thread system, with completion notifications being posted to the thread which issued the overlapped request.  The system directs a thread to execute all pending asynchronous procedure calls (APCs) - such as a pending overlapped completion notification function call - whenever it enters an alertable wait state (see SleepEx, SignalObjectAndWait, WaitForSingleObjectEx, WaitForMultipleObjectsEx, MsgWaitForMultipleObjectsEx, and WSAWaitForMultipleEvents).

On a side note, using overlapped sockets operations is a great way to remove the "64 socket limitation" (the wait functions can only wait on up to 64 objects).