Commit 7251d776 authored by Michael Graff's avatar Michael Graff
Browse files

snapshot

parent da4242bf
......@@ -12,6 +12,7 @@
#include <isc/thread.h>
#include <isc/result.h>
#include <isc/socket.h>
#include <isc/timer.h>
#include <sys/types.h>
#include <sys/socket.h>
......@@ -21,8 +22,7 @@
#include <arpa/inet.h>
isc_memctx_t mctx = NULL;
volatile int tasks_done = 0;
int sockets_active = 0;
static isc_boolean_t my_send(isc_task_t task, isc_event_t event);
static isc_boolean_t my_recv(isc_task_t task, isc_event_t event);
......@@ -51,7 +51,6 @@ my_shutdown(isc_task_t task, isc_event_t event)
return (ISC_TRUE);
}
static isc_boolean_t
my_recv(isc_task_t task, isc_event_t event)
{
......@@ -75,6 +74,10 @@ my_recv(isc_task_t task, isc_event_t event)
isc_event_free(&event);
sockets_active--;
if (sockets_active == 0)
return (1);
return (0);
}
......@@ -190,6 +193,8 @@ my_listen(isc_task_t task, isc_event_t event)
char *name = event->arg;
isc_socket_newconnev_t dev;
struct isc_region region;
isc_socket_t oldsock;
int ret;
dev = (isc_socket_newconnev_t)event;
......@@ -197,6 +202,8 @@ my_listen(isc_task_t task, isc_event_t event)
name, task, event->sender, dev->newsocket, dev->result);
fflush(stdout);
ret = 0;
if (dev->result == ISC_R_SUCCESS) {
/*
* queue another listen on this socket
......@@ -211,15 +218,33 @@ my_listen(isc_task_t task, isc_event_t event)
*/
isc_socket_recv(dev->newsocket, &region, ISC_FALSE,
task, my_recv, event->arg);
sockets_active++;
} else {
/*
* Do something useful here
*/
printf("detaching from socket %p\n", event->sender);
oldsock = event->sender;
isc_socket_detach(&oldsock);
sockets_active--;
ret = 1;
}
isc_event_free(&event);
return 0;
return (ret);
}
static isc_boolean_t
timeout(isc_task_t task, isc_event_t event)
{
isc_socket_t sock = event->arg;
printf("Timeout, canceling IO on socket %p\n", sock);
isc_socket_cancel(sock, NULL, ISC_SOCKCANCEL_ALL);
isc_timer_detach((isc_timer_t *)&event->sender);
return (0);
}
int
......@@ -227,12 +252,17 @@ main(int argc, char *argv[])
{
isc_taskmgr_t manager = NULL;
isc_task_t t1 = NULL, t2 = NULL;
isc_timermgr_t timgr = NULL;
struct isc_time expires, now;
struct isc_interval interval;
isc_timer_t ti1 = NULL;
isc_event_t event;
unsigned int workers;
isc_socketmgr_t socketmgr;
isc_socket_t so1, so2;
struct isc_sockaddr sockaddr;
unsigned int addrlen;
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.type.sin.sin_port = htons(5544);
......@@ -257,12 +287,20 @@ main(int argc, char *argv[])
printf("task 1 = %p\n", t1);
printf("task 2 = %p\n", t2);
/*
* create the timer we'll need
*/
INSIST(isc_timermgr_create(mctx, &timgr) == ISC_R_SUCCESS);
(void)isc_time_get(&now);
socketmgr = NULL;
INSIST(isc_socketmgr_create(mctx, &socketmgr) == ISC_R_SUCCESS);
/*
* open up a listener socket
*/
sockets_active++;
so1 = NULL;
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.type.sin.sin_family = AF_INET;
......@@ -278,11 +316,16 @@ main(int argc, char *argv[])
*/
INSIST(isc_socket_accept(so1, t1, my_listen,
"so1") == ISC_R_SUCCESS);
isc_time_settoepoch(&expires);
isc_interval_set(&interval, 30, 0);
INSIST(isc_timer_create(timgr, isc_timertype_once, &expires, &interval,
t1, timeout, so1, &ti1) == ISC_R_SUCCESS);
/*
* open up a socket that will connect to www.flame.org, port 80.
* Why not. :)
*/
sockets_active++;
so2 = NULL;
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.type.sin.sin_port = htons(80);
......@@ -294,7 +337,7 @@ main(int argc, char *argv[])
INSIST(isc_socket_connect(so2, &sockaddr, addrlen, t1, my_connect,
"so2") == ISC_R_SUCCESS);
sleep(2);
sleep(1);
event = isc_event_allocate(mctx, (void *)main, 1, my_callback, "1",
sizeof *event);
......@@ -330,24 +373,16 @@ main(int argc, char *argv[])
sizeof *event);
isc_task_send(t2, &event);
/*
* Grr! there is no way to say "wake me when it's over"
*/
while (tasks_done != 2) {
fprintf(stderr, "Tasks done: %d\n", tasks_done);
sleep(2);
}
sleep(60);
isc_task_shutdown(t1);
isc_task_shutdown(t2);
isc_task_detach(&t1);
isc_task_detach(&t2);
printf("destroy\n");
isc_socket_detach(&so1);
isc_socket_detach(&so2);
printf("Destroying socket manager\n");
isc_socketmgr_destroy(&socketmgr);
printf("Destroying task manager\n");
isc_taskmgr_destroy(&manager);
printf("destroyed\n");
......
/* $Id: socket.h,v 1.10 1998/12/04 11:21:11 explorer Exp $ */
/* $Id: socket.h,v 1.11 1998/12/05 00:28:13 explorer Exp $ */
#ifndef ISC_SOCKET_H
#define ISC_SOCKET_H 1
......@@ -106,20 +106,31 @@ typedef struct isc_socket_connev {
/*
* Internal events.
*/
#define ISC_SOCKEVENT_INTIO (ISC_EVENTCLASS_SOCKET + 257)
#define ISC_SOCKEVENT_INTCONN (ISC_EVENTCLASS_SOCKET + 258)
#define ISC_SOCKEVENT_INTRECV (ISC_EVENTCLASS_SOCKET + 257)
#define ISC_SOCKEVENT_INTSEND (ISC_EVENTCLASS_SOCKET + 258)
#define ISC_SOCKEVENT_INTACCEPT (ISC_EVENTCLASS_SOCKET + 259)
#define ISC_SOCKEVENT_INTCONN (ISC_EVENTCLASS_SOCKET + 260)
typedef enum {
isc_socket_udp,
isc_socket_tcp
} isc_sockettype_t;
typedef enum {
isc_sockshut_reading,
isc_sockshut_writing,
isc_sockshut_all
} isc_socketshutdown_t;
/*
* How a socket should be shutdown in isc_socket_shutdown() calls.
*/
#define ISC_SOCKSHUT_RECV 0x00000001 /* close read side */
#define ISC_SOCKSHUT_SEND 0x00000002 /* close write side */
#define ISC_SOCKSHUT_ALL 0x00000003 /* close them all */
/*
* What I/O events to cancel in isc_socket_cancel() calls.
*/
#define ISC_SOCKCANCEL_RECV 0x00000001 /* cancel recv */
#define ISC_SOCKCANCEL_SEND 0x00000002 /* cancel send */
#define ISC_SOCKCANCEL_ACCEPT 0x00000004 /* cancel accept */
#define ISC_SOCKCANCEL_CONNECT 0x00000008 /* cancel connect */
#define ISC_SOCKCANCEL_ALL 0x0000000f /* cancel everything */
/***
*** Socket and Socket Manager Functions
......@@ -153,8 +164,8 @@ isc_socket_create(isc_socketmgr_t manager,
* ISC_R_UNEXPECTED
*/
int
isc_socket_cancel(isc_socket_t socket, isc_task_t task,
void
isc_socket_cancel(isc_socket_t sock, isc_task_t task,
unsigned int how);
/*
* Cancel pending I/O of the type specified by "how".
......@@ -178,11 +189,15 @@ isc_socket_cancel(isc_socket_t socket, isc_task_t task,
* ISC_SOCKCANCEL_SEND:
* Cancel pending isc_socket_send() and isc_socket_sendto() calls.
*
* ISC_SOCKCANCEL_
* ISC_SOCKCANCEL_ACCEPT:
* Cancel pending isc_socket_accept() calls.
*
* ISC_SOCKCANCEL_CONNECT:
* Cancel pending isc_socket_connect() call.
*/
void
isc_socket_shutdown(isc_socket_t socket, isc_socketshutdown_t how);
isc_socket_shutdown(isc_socket_t sock, unsigned int how);
/*
* Shutdown 'socket' according to 'how'.
*
......@@ -192,13 +207,13 @@ isc_socket_shutdown(isc_socket_t socket, isc_socketshutdown_t how);
*
* 'task' is NULL or is a valid task.
*
* If 'how' is 'isc_sockshut_reading' or 'isc_sockshut_all' then
* If 'how' is 'ISC_SOCKSHUT_RECV' or 'ISC_SOCKSHUT_ALL' then
*
* The read queue must be empty.
*
* No further read requests may be made.
*
* If 'how' is 'isc_sockshut_writing' or 'isc_sockshut_all' then
* If 'how' is 'ISC_SOCKSHUT_SEND' or 'ISC_SOCKSHUT_ALL' then
*
* The write queue must be empty.
*
......@@ -206,7 +221,7 @@ isc_socket_shutdown(isc_socket_t socket, isc_socketshutdown_t how);
*/
void
isc_socket_attach(isc_socket_t socket, isc_socket_t *socketp);
isc_socket_attach(isc_socket_t sock, isc_socket_t *socketp);
/*
* Attach *socketp to socket.
*
......@@ -249,7 +264,7 @@ isc_socket_detach(isc_socket_t *socketp);
*/
isc_result_t
isc_socket_bind(isc_socket_t socket, struct isc_sockaddr *addressp,
isc_socket_bind(isc_socket_t sock, struct isc_sockaddr *addressp,
int length);
/*
* Bind 'socket' to '*addressp'.
......@@ -273,7 +288,7 @@ isc_socket_bind(isc_socket_t socket, struct isc_sockaddr *addressp,
*/
isc_result_t
isc_socket_listen(isc_socket_t socket, int backlog);
isc_socket_listen(isc_socket_t sock, int backlog);
/*
* Set listen mode on the socket. After this call, the only function that
* can be used (other than attach and detach) is isc_socket_accept().
......@@ -295,7 +310,7 @@ isc_socket_listen(isc_socket_t socket, int backlog);
*/
isc_result_t
isc_socket_accept(isc_socket_t socket,
isc_socket_accept(isc_socket_t sock,
isc_task_t task, isc_taskaction_t action, void *arg);
/*
* Queue accept event. When a new connection is received, the task will
......@@ -318,7 +333,7 @@ isc_socket_accept(isc_socket_t socket,
*/
isc_result_t
isc_socket_connect(isc_socket_t socket, struct isc_sockaddr *addressp,
isc_socket_connect(isc_socket_t sock, struct isc_sockaddr *addressp,
int length, isc_task_t task, isc_taskaction_t action,
void *arg);
/*
......@@ -354,7 +369,7 @@ isc_socket_connect(isc_socket_t socket, struct isc_sockaddr *addressp,
*/
isc_result_t
isc_socket_getpeername(isc_socket_t socket, struct isc_sockaddr *addressp,
isc_socket_getpeername(isc_socket_t sock, struct isc_sockaddr *addressp,
int *lengthp);
/*
* Get the name of the peer connected to 'socket'.
......@@ -373,7 +388,7 @@ isc_socket_getpeername(isc_socket_t socket, struct isc_sockaddr *addressp,
*/
isc_result_t
isc_socket_getsockname(isc_socket_t socket, struct isc_sockaddr *addressp,
isc_socket_getsockname(isc_socket_t sock, struct isc_sockaddr *addressp,
int *lengthp);
/*
* Get the name of 'socket'.
......@@ -392,7 +407,7 @@ isc_socket_getsockname(isc_socket_t socket, struct isc_sockaddr *addressp,
*/
isc_result_t
isc_socket_recv(isc_socket_t socket, isc_region_t region,
isc_socket_recv(isc_socket_t sock, isc_region_t region,
isc_boolean_t partial,
isc_task_t task, isc_taskaction_t action, void *arg);
/*
......@@ -438,10 +453,10 @@ isc_socket_recv(isc_socket_t socket, isc_region_t region,
*/
isc_result_t
isc_socket_send(isc_socket_t socket, isc_region_t region,
isc_socket_send(isc_socket_t sock, isc_region_t region,
isc_task_t task, isc_taskaction_t action, void *arg);
isc_result_t
isc_socket_sendto(isc_socket_t socket, isc_region_t region,
isc_socket_sendto(isc_socket_t sock, isc_region_t region,
isc_task_t task, isc_taskaction_t action, void *arg,
isc_sockaddr_t address, unsigned int addrlength);
/*
......
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment