flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > High Level Languages > c win question, can someone help me with IOCP worker thread

Author
Thread Post new topic Reply to topic
sleepsleep



Joined: 05 Oct 2006
Posts: 4816
c win question, can someone help me with IOCP worker thread
hi there,
sorry, i don't know where is the suitable subforum for this question, admin please move it to where it fits.

i am learning, writing a iocp winsock server using c language.(based on some source code online).

i don't quite understand the following

Code:

    while(1) {
             ifGetQueuedCompletionStatus(cport, &btrans, (LPDWORD) &phandledata, (LPOVERLAPPED *)&piodataINFINITE) == 0 ) {
                 LogTime();
                  printf("GetQueuedCompletionStatus() failed - %d\n"GetLastError());
                     return 0;
           }

          ifbtrans > 0) {
                   printf("btrans > 0 : %d bytes\n"btrans);
                        //printf("%s\n"piodata->buffer);
                    ZeroMemory( &(piodata->ov), sizeof(OVERLAPPED) );
                    //ZeroMemory( &(piodata->buffer), sizeof(BUFFERSIZE) );
                      piodata->wsabuff.len = BUFFERSIZE;
                       piodata->wsabuff.buf = piodata->buffer;
                       flags = 0;
                  if(WSARecvphandledata->s, &(piodata->wsabuff), 1, &brecv, &flags, &(piodata->ov), NULL ) == SOCKET_ERROR) {
                         ifWSAGetLastError() !ERROR_IO_PENDING ) {
                                  LogTime();
                                  printf("WSARevc() 182 failed %d\n"WSAGetLastError());
                                  return 1;
                           }
                      }
              }
              
            ifbrecv > 0) {
                    printf("brecv > 0 : %d bytes\n"brecv);
          } else {
                  printf("brecv <= 0\n");
               }
              
            ifbtrans == 0 ) {
                    printf("btrans ==0\n");
          }
      }




above is the worker thread partial code.
am using the quetannon client to access my server.

btrans == 0 means disconnect,

btrans > 0 means i receive something

if the buffer is large enough to receive incoming data 1 time, then brecv will be <= 0, if buffer is not large enough, then brecv will equal buffersize (full load)

then it will repeat btrans > 0 part and do WSARecv

the output i got is like this

Code:

2011-12-31 18:46:35 (2961912 -accepted
btrans > 0 :  40 bytes
brecv <= 0
btrans > 0 :  1024 bytes
brecv > 0 : 1024 bytes
btrans > 0 :  1024 bytes
brecv > 0 : 1024 bytes
btrans > 0 :  1024 bytes
brecv > 0 : 156 bytes
btrans > 0 :  156 bytes
brecv > 0 : 156 bytes
brecv > 0 : 156 bytes
btrans ==0
^C




what i don't understand is,

1. should i receive full incoming data, then only i start doing something, eg. response back.

2. how should i check/peek the WSARecv data length? i want to close socket on any socket that send out > 512bytes to me.

3. could send & recv occur concurrently? or after recv finish, then only send? and what would happen if during send i got recv

4. how to terminate browser request? send back http header with close connection? or closesocket?

thank you very much...
Post 31 Dec 2011, 10:59
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2581
Location: NOVI, MI, USA
The threading part is for console apps I suppose. But if you go back to quetannon, you'll find that it's using the WSAAsyncSelect API for a non blocking style socket. Your app must be processing window messages in order to receive socket notifications.

You must register for the specific notifications you wish to receive, I.e when connected or received some bytes.

So after you receive the bytes you check the length as you mentioned and then continue to parse the HTTP headers and construct a response afterwards.

There's a trick of converting a socket handle to a FILE handle using fdopen but I don't recommend it.
Post 31 Dec 2011, 20:39
View user's profile Send private message Visit poster's website Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 4816
found out google chrome opens up 2 sockets in each visit.

Code:

2012-01-01 12:27:49 (4531912 -accepted
2012-01-01 12:27:49 (468socket (1912transmitted : 392 bytes
2012-01-01 12:27:49 (4681888 -accepted
2012-01-01 12:28:01 (1091888 -closing fromserver[0], toserver[0]




the first (1912) transmit 392 bytes HTTP get request, then another socket which transmit nothing.

after around 11 seconds then auto close.

maybe some internal API, handshake is expected? websocket? idk.



typedef, thanks, but ma stuck with this iocp, will still try to make it functions, hopefully.
Post 01 Jan 2012, 04:44
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 4816
wanna ask,

after we WSASend ""Connection: close\r\n\r\n" to connected browser client, the connection will get "disconnected" by browser.

but the server get no notification about this "disconnection", how to get such notification?

because btrans == 0 doesn't work when browser disconnects on their own.
Post 01 Jan 2012, 15:56
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2581
Location: NOVI, MI, USA
You have to actually close the connection to the server. That Header command just tells the server not to use a persistent connection.
Post 01 Jan 2012, 17:11
View user's profile Send private message Visit poster's website Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 4816
thanks,
Smile

finally, ma sort of figure out how the whole shit running Smile

need to use DisconnectEx to close the socket after send out Connection: close, so the pending overlapped IO will exit gracefully.

damn it, it tooks hours!!, maybe years since i first interested to build my own http iocp web server.
Post 01 Jan 2012, 21:04
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >

Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2013, Tomasz Grysztar.