- From: Worik Macky Turei Stanton <worik@noggon.co.nz>
- Date: 11 Mar 2001 17:29:13 +1300
- To: www-lib@w3.org
Friends
I am still trying to come to grips with this API. I am trying to get
a multithreaded style thing happening in the full knowledge that I
cannot expect the library to be thread safe. So I am looking at using
an event loop.
What I want to do is open a socket then be able to deal with clients
connecting simultaneously(ish). I want lowlevel access to the bytes
that are comming in on the socket, but I cannot work out when to read
it.
My plan is to listen on a socket, register it with HTEvent_ACCEPT or
CONNECT (??) and handle connections there.
I start my programme then connect to it with telnet and monitor what
it does in a debugger or with progress statements.
I am getting nowhere much mostly because I do not know how I should
handle the events, what I should do with them etc.
I do not know as much about socket functions as I should and I am
(still!) a complete beginner with libwww.
There are no examples that do this sort of stuff in the
Library/Examples. Is there any where I can find some examples? Can
someone tell me what I am doing right and wrong with this code?
/*
*/
#include <string>
using namespace std;
#include "proxy.h"
#include "WWWLib.h"
#include "WWWHTTP.h"
#include "WWWInit.h"
#define MAX_COUNT 1024
/* ----------------------------------------------------------------- */
int
printer (const char * fmt, va_list pArgs) {
return (vfprintf(stdout, fmt, pArgs));
}
int
tracer (const char * fmt, va_list pArgs) {
return (vfprintf(stderr, fmt, pArgs));
}
int
terminateHandler (HTRequest * request, HTResponse * response,
void * param, int status) {
HTChunk * chunk = (HTChunk *) HTRequest_context(request);
if (status == HT_LOADED && chunk && HTChunk_data(chunk))
HTPrint("%s", HTChunk_data(chunk));
/* Remember to delete our chunk of data */
if (chunk) HTChunk_delete(chunk);
/* We are done with this request */
HTRequest_delete(request);
return HT_OK;
}
int Connect (SOCKET s, void * vp, HTEventType e){
cout << "Connect "<<s<<"\n";
return 0;
}
int Read (SOCKET s, void * vp, HTEventType e){
cout << "Read "<<s<<"\n";
return 0;
}
int Accept (SOCKET s, void * vp, HTEventType e){
cout << "Accept from "<<s<<" to ";
struct sockaddr *cliaddr;
socklen_t clilen;
cliaddr = new struct sockaddr;
SOCKET connfd = accept (s, cliaddr, &clilen);
cout << connfd <<endl;
HTEvent_register(connfd, HTEvent_READ,
HTEvent_new (Read, 0, _HTPriority(0), 10000));
HTEvent_register(connfd, HTEvent_CONNECT,
HTEvent_new (Connect, 0, _HTPriority(0), 10000));
HTEvent_register(connfd, HTEvent_ACCEPT,
HTEvent_new (Accept, 0, _HTPriority(0), 10000));
return 0;
}
void
InitW3C() {
/* Create a new premptive client */
HTProfile_newNoCacheClient("libwww-MGET", "1.0");
/* Need our own trace and print functions */
HTPrint_setCallback(printer);
HTTrace_setCallback(tracer);
/* Add our own filter to handle termination */
HTNet_addAfter(terminateHandler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
/* Set up default event loop */
HTEventInit();
/* Turn on tracing */
#if 0
HTSetTraceMessageMask("*");
#endif
/* We don't wany any progress notification or other user stuff */
HTAlert_setInteractive(NO);
}
int
main (int argc, char ** argv)
{
InitW3C();
socklen_t addrlen;
const char * src_port = "8088";
int sdIn = tcp_listen (NULL, src_port, &addrlen);
HTEvent_register(sdIn, HTEvent_CONNECT,
HTEvent_new (Connect, 0, _HTPriority(0), 10000));
HTEvent_register(sdIn, HTEvent_ACCEPT,
HTEvent_new (Accept, 0, _HTPriority(0), 10000));
HTEventList_newLoop ();
while(1){
sleep(1);
cout << '.';
}
return 0;
}
I connect with telnet 127.0.0.1 8088
The output I get is....
Accept from 4 to 5
Connect 5
Connect 5
Connect 5
:
:
Connect 5
Connect 5
Accept from 5 to -1
Connect 5
Accept from 5 to -1
:
:
I have at this point killed the telnet session.
Worik
--
Worik Macky Turei Stanton
worik@noggon.co.nz
Aotearoa
This line would not have seven words if only it had eight words less.
Received on Saturday, 10 March 2001 23:26:30 UTC