[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r8420: added contrib dir + new bsockets (aka liboverlapped) (in bsockets/trunk: . contrib contrib/liboverlapped)



Author: chiussi
Date: 2006-09-19 13:30:05 -0400 (Tue, 19 Sep 2006)
New Revision: 8420

Added:
   bsockets/trunk/contrib/
   bsockets/trunk/contrib/liboverlapped/
   bsockets/trunk/contrib/liboverlapped/Makefile
   bsockets/trunk/contrib/liboverlapped/cmd.c
   bsockets/trunk/contrib/liboverlapped/cmd.h
   bsockets/trunk/contrib/liboverlapped/env.c
   bsockets/trunk/contrib/liboverlapped/env.h
   bsockets/trunk/contrib/liboverlapped/file.c
   bsockets/trunk/contrib/liboverlapped/file.h
   bsockets/trunk/contrib/liboverlapped/list.c
   bsockets/trunk/contrib/liboverlapped/list.h
   bsockets/trunk/contrib/liboverlapped/misc.c
   bsockets/trunk/contrib/liboverlapped/misc.h
   bsockets/trunk/contrib/liboverlapped/overlapped.c
   bsockets/trunk/contrib/liboverlapped/overlapped.h
   bsockets/trunk/contrib/liboverlapped/socket.c
   bsockets/trunk/contrib/liboverlapped/socket.h
   bsockets/trunk/contrib/liboverlapped/test.c
   bsockets/trunk/contrib/liboverlapped/test.h
Log:
added contrib dir + new bsockets (aka liboverlapped)


Added: bsockets/trunk/contrib/liboverlapped/Makefile
===================================================================
--- bsockets/trunk/contrib/liboverlapped/Makefile	2006-09-19 14:57:31 UTC (rev 8419)
+++ bsockets/trunk/contrib/liboverlapped/Makefile	2006-09-19 17:30:05 UTC (rev 8420)
@@ -0,0 +1,26 @@
+
+overlapped_objs = env.o cmd.o list.o misc.o file.o socket.o overlapped.o
+
+overlapped_headers = overlapped.h misc.h
+
+
+default: test.exe
+
+
+liboverlapped.a: $(overlapped_objs)
+	ar -r $@ $^
+
+
+test.exe: test.o liboverlapped.a test.h
+	gcc -g -o test.exe $< -L. -loverlapped -lws2_32
+ 
+
+%.o: %.c $(overlapped_headers)
+	gcc -g -Wall -c $<
+
+
+
+clean:
+	rm -rf *.a *.o *.exe
+	
+


Property changes on: bsockets/trunk/contrib/liboverlapped/Makefile
___________________________________________________________________
Name: svn:executable
   + *

Added: bsockets/trunk/contrib/liboverlapped/cmd.c
===================================================================
--- bsockets/trunk/contrib/liboverlapped/cmd.c	2006-09-19 14:57:31 UTC (rev 8419)
+++ bsockets/trunk/contrib/liboverlapped/cmd.c	2006-09-19 17:30:05 UTC (rev 8420)
@@ -0,0 +1,146 @@
+#include <windows.h>
+#include <stdio.h>
+
+#include "overlapped.h"
+#include "cmd.h"
+#include "env.h"
+#include "misc.h"
+#include "list.h"
+#include "socket.h"
+
+void cmd_post(struct _cmd *cmd, struct _file_env *env)  {
+
+	int r;
+
+	//todo -- come up with a way of recycling WSAEvent's
+	cmd->done_e = CreateEvent(NULL,TRUE,FALSE,NULL);
+
+	if (cmd->done_e == NULL) {
+		cmd->ret = -1;
+		cmd->err = unixify_w32err(WSAGetLastError());
+	}
+
+	r = WaitForSingleObject(env->post_m,INFINITE);
+	ASSERT(r == WAIT_OBJECT_0);
+
+	list_enqueue(cmd,env->cmd_q);
+
+	ASSERT(WSASetEvent(env->ev[0]));
+
+	ASSERT(ReleaseMutex(env->post_m));
+
+	r = WaitForSingleObject(cmd->done_e,INFINITE);
+	ASSERT(r == WAIT_OBJECT_0);
+
+	ASSERT(CloseHandle(cmd->done_e));
+
+}
+
+void cmd_next(struct _cmd **cmd, struct _file_env *env) {
+
+	int r;
+
+	r = WaitForSingleObject(env->post_m,INFINITE);
+	ASSERT(r == WAIT_OBJECT_0);
+
+	if (list_dequeue(cmd,env->cmd_q) != 0) {
+		*cmd = NULL;
+		ASSERT(WSAResetEvent(env->ev[0]) );
+	}
+
+	ASSERT(ReleaseMutex(env->post_m)) ;
+
+}
+
+void cmd_done(struct _cmd *cmd, int ret, int err) {
+
+	cmd->ret = ret;
+	cmd->err = err;
+	ASSERT(SetEvent(cmd->done_e));
+
+}
+
+//cmd2 is ignored
+void cmd_dispatch(struct _cmd *cmd2, struct _file_env *env) {
+
+	struct _cmd *cmd;
+	struct _file *f;
+	struct _socket *s;
+
+
+	int err;
+	int out;
+
+	cmd_next(&cmd,env);
+
+	if (cmd == NULL)
+		return;
+
+	if (cmd->fd != -1) {
+		f = file_get(cmd->fd,env);
+		if (f == NULL) {
+			cmd_done(cmd,-1,errno);
+			cmd = NULL;
+		}
+
+		switch (f->type) {
+
+			case FT_SOCKET:
+				s = f->data;
+			break;
+
+			default:
+				ASSERT(FALSE);
+			break;
+		}
+
+
+	}
+
+	if (cmd != NULL) {
+
+		switch (cmd->type) {
+
+			case CMD_PING:
+				cmd_done(cmd,0,0);
+			break;
+
+			case CMD_SHUTDOWN:
+				env->resume = FALSE;
+				cmd_done(cmd,0,0);
+			break;
+
+			case CMD_SOCKET_NEW:
+				socket_new(cmd,env);
+			break;
+
+			case CMD_SOCKET_CONNECT:
+				socket_connect(f,s,cmd,env);
+			break;
+
+			case CMD_FILE_CLOSE:
+
+				//if there is an outstanding error, be sure to report it
+				if (f->err) {
+					out = -1;
+					err = f->err;
+				} else {
+					out = 0;
+				}
+
+				file_close(f,env);
+
+				cmd_done(cmd,out,err);
+
+			break;
+
+			default:
+				ASSERT(FALSE);
+			break;
+
+		}
+
+	}
+
+}
+


Property changes on: bsockets/trunk/contrib/liboverlapped/cmd.c
___________________________________________________________________
Name: svn:executable
   + *

Added: bsockets/trunk/contrib/liboverlapped/cmd.h
===================================================================
--- bsockets/trunk/contrib/liboverlapped/cmd.h	2006-09-19 14:57:31 UTC (rev 8419)
+++ bsockets/trunk/contrib/liboverlapped/cmd.h	2006-09-19 17:30:05 UTC (rev 8420)
@@ -0,0 +1,39 @@
+#ifndef _CMD_H_
+#define _CMD_H_
+
+#include <windows.h>
+#include "env.h"
+
+struct _cmd {
+
+	HANDLE done_e;
+
+	void *arg[32];
+
+	int type;
+
+	int ret;
+	int err;
+
+	int fd;
+};
+
+void cmd_post(struct _cmd*, struct _file_env*);
+void cmd_next(struct _cmd**, struct _file_env*);
+void cmd_done(struct _cmd*, int, int);
+void cmd_dispatch(struct _cmd *cmd, struct _file_env*);
+
+#define CMD_PING	1
+
+#define CMD_FILE_CLOSE	10
+
+
+#define CMD_SOCKET_NEW		20
+#define CMD_SOCKET_CONNECT	21
+
+
+#define CMD_SHUTDOWN 100
+
+
+#endif
+


Property changes on: bsockets/trunk/contrib/liboverlapped/cmd.h
___________________________________________________________________
Name: svn:executable
   + *

Added: bsockets/trunk/contrib/liboverlapped/env.c
===================================================================
--- bsockets/trunk/contrib/liboverlapped/env.c	2006-09-19 14:57:31 UTC (rev 8419)
+++ bsockets/trunk/contrib/liboverlapped/env.c	2006-09-19 17:30:05 UTC (rev 8420)
@@ -0,0 +1,200 @@
+#include <windows.h>
+#include <stdio.h>
+
+#include "overlapped.h"
+#include "misc.h"
+#include "env.h"
+#include "cmd.h"
+#include "list.h"
+
+static struct _file_env_options default_env_options = {
+	1000,
+	1000
+};
+
+void env_main_loop(struct _file_env *env) {
+
+	int i;
+	int r;
+
+	while(env->resume) {
+
+		r = WSAWaitForMultipleEvents(
+			WSA_MAXIMUM_WAIT_EVENTS,
+			env->ev,
+			FALSE,
+			INFINITE,
+			FALSE
+		);
+
+		switch(r) {
+
+			case WAIT_IO_COMPLETION: break;
+
+			case WSA_WAIT_FAILED:
+			case WSA_WAIT_TIMEOUT:
+				ASSERT(FALSE);
+			break;
+
+			default:
+				i = r - WAIT_OBJECT_0;
+				env->ev_callback[i](env->cmd[i],env);
+			break;
+		}
+	}
+}
+
+void env_ping(struct _file_env *env) {
+
+	struct _cmd cmd;
+	cmd.type = CMD_PING;
+	cmd.fd = -1;
+
+	cmd_post(&cmd,env);
+
+	ASSERT(cmd.ret == 0);
+
+}
+
+void env_stop_thread(struct _file_env *env) {
+
+	int r;
+
+	struct _cmd cmd;
+	cmd.type = CMD_SHUTDOWN;
+
+	cmd_post(&cmd,env);
+
+	r = WaitForSingleObject(env->thread,1000);
+	ASSERT(r == WAIT_OBJECT_0);
+
+	CloseHandle(env->thread);
+
+}
+
+//todo -- clean up each member of _file_env
+void env_shutdown() {
+
+	struct _file_env *env;
+	int i;
+
+	env = __GLOBAL_FILE_ENV_;
+
+	if (env == NULL) {
+		return;
+	}
+
+	if (env->thread != NULL) {
+		env_stop_thread(env);
+	}
+
+	if (env->fd != NULL) {
+		for (i=0; i<env->opt.maxfiles; i++)
+			if (env->fd[i] != NULL) {
+				file_close(env->fd[i],env);
+			}
+
+		for (i=0; i<env->opt.maxfiles; i++)
+			ASSERT(env->fd[i] == NULL);
+	}
+
+	if (env->free_q != NULL) {
+		list_free(env->free_q);
+	}
+
+	if (env->post_m != NULL ) {
+		CloseHandle(env->post_m);
+	}
+
+	if (env->cmd_q != NULL) {
+		list_free(env->cmd_q);
+	}
+
+
+	for (i=0; i<WSA_MAXIMUM_WAIT_EVENTS; i++) {
+		if (env->ev[i] != NULL) {
+			ASSERT(WSACloseEvent(env->ev[i]));
+		}
+	}
+
+	free(env->fd);
+	free(env);
+
+	__GLOBAL_FILE_ENV_ = NULL;
+
+}
+
+int env_init(struct _file_env_options *opt) {
+
+	struct _file_env *env;
+	int i;
+
+	int out;
+
+	//note: the user is responsible for manually init'ing winsock
+
+	if (opt == NULL) {
+		opt = &default_env_options;
+	}
+
+	env = malloc(sizeof(struct _file_env));
+	CHECK(env != NULL,ENOMEM);
+
+	__GLOBAL_FILE_ENV_ = env;
+
+	//todo -- init all members to null
+	env->free_q = NULL;
+	env->fd = NULL;
+	env->resume = TRUE;
+	env->post_m = NULL;
+	env->cmd_q = NULL;
+	env->thread = NULL;
+
+	for (i=0; i<WSA_MAXIMUM_WAIT_EVENTS;i++) {
+		env->ev[i] = NULL;
+	}
+
+	memcpy(&env->opt,opt, sizeof(struct _file_env_options));
+
+	env->fd = malloc(sizeof(struct _file*)*opt->maxfiles);
+
+	for (i=0; i<opt->maxfiles; i++) {
+		env->fd[i] = NULL;
+	}
+
+	env->free_q = list_new();
+
+	for (i=0; i<opt->maxfiles; i++) {
+		list_enqueue((void*) i,env->free_q);
+	}
+
+	for (i=0; i<WSA_MAXIMUM_WAIT_EVENTS; i++) {
+		env->ev[i] = WSACreateEvent();
+		CHECK0(env->ev[i] != WSA_INVALID_EVENT);
+	}
+
+	env->ev_callback[0] = cmd_dispatch;
+
+	env->post_m = CreateMutex(NULL,FALSE,NULL);
+	CHECK0(env->post_m != NULL);
+
+	env->cmd_q = list_new();
+	CHECK(env->cmd_q != NULL,0);
+
+	env->thread = CreateThread(NULL,0,(void*) env_main_loop,env,0,NULL);
+	CHECK0(env->thread != NULL);
+
+	env_ping(env);
+
+	out = 0;
+
+	fail:
+
+	if (out == -1) {
+		env_shutdown();
+	}
+
+	return out;
+
+}
+


Property changes on: bsockets/trunk/contrib/liboverlapped/env.c
___________________________________________________________________
Name: svn:executable
   + *

Added: bsockets/trunk/contrib/liboverlapped/env.h
===================================================================
--- bsockets/trunk/contrib/liboverlapped/env.h	2006-09-19 14:57:31 UTC (rev 8419)
+++ bsockets/trunk/contrib/liboverlapped/env.h	2006-09-19 17:30:05 UTC (rev 8420)
@@ -0,0 +1,42 @@
+#ifndef _ENV_H_
+#define _ENV_H_
+
+#include "overlapped.h"
+#include "file.h"
+
+struct _cmd;
+
+struct _file_env {
+
+	/*environment options*/
+	struct _file_env_options opt;
+
+	/*stuff related to files*/
+	struct _file **fd;
+	struct _list *free_q; // a list of free file descriptors
+
+
+	/*stuff related to thread maintenence*/
+	HANDLE thread;
+
+	int resume;
+
+	/*stuff related to the processing of a command*/
+	HANDLE post_m; //mutex to control queueing of system calls
+	struct _list *cmd_q;
+
+
+	/*stuff related to the events the thread waits on*/
+	WSAEVENT ev[WSA_MAXIMUM_WAIT_EVENTS];
+
+	//this gets called when ev is active
+	void (*ev_callback[WSA_MAXIMUM_WAIT_EVENTS])
+		(struct _cmd*, struct _file_env *);
+
+	//holds the command associated with that spot on ev
+	struct _cmd *cmd[WSA_MAXIMUM_WAIT_EVENTS];
+};
+
+
+#endif
+


Property changes on: bsockets/trunk/contrib/liboverlapped/env.h
___________________________________________________________________
Name: svn:executable
   + *

Added: bsockets/trunk/contrib/liboverlapped/file.c
===================================================================
--- bsockets/trunk/contrib/liboverlapped/file.c	2006-09-19 14:57:31 UTC (rev 8419)
+++ bsockets/trunk/contrib/liboverlapped/file.c	2006-09-19 17:30:05 UTC (rev 8420)
@@ -0,0 +1,111 @@
+#include <windows.h>
+
+#include "overlapped.h"
+#include "file.h"
+#include "misc.h"
+#include "env.h"
+#include "list.h"
+
+struct _file *file_get(int fd, struct _file_env *env) {
+
+	struct _file *out;
+
+	if (fd < 0 || fd >= env->opt.maxfiles) {
+
+		errno = EINVAL;
+		out = NULL;
+
+	} else {
+
+		out = env->fd[fd];
+
+		if (out == NULL) {
+			errno = EBADF;
+		}
+
+	}
+
+	return out;
+
+}
+
+void file_exception(struct _file *f, int code) {
+
+	switch (f->type) {
+
+		case FT_SOCKET:
+			closesocket((SOCKET) f->handle);
+		break;
+
+		default:
+			ASSERT(FALSE);
+		break;
+	}
+}
+
+void file_close(struct _file *f, struct _file_env *env) {
+
+	if (f->fd != -1) {
+		file_exception(f,ECLOSED);
+		ASSERT(env->fd[f->fd] = f);
+		env->fd[f->fd] = NULL;
+	}
+
+	if (f->in_buf != NULL) {
+		//todo dump in buffer
+		list_free(f->in_buf);
+
+	}
+
+	if (f->out_buf != NULL) {
+		//todo dump out buffer
+		list_free(f->out_buf);
+	}
+
+	if (f->data != NULL)
+		free(f->data);
+
+	free(f);
+}
+
+struct _file *file_new(struct _file_env *env) {
+
+	struct _file *f;
+
+	int out;
+
+	f = (struct _file*) malloc(sizeof(struct _file));
+	CHECK(f != NULL,ENOMEM);
+
+	/*init everything to bad*/
+	f->fd = -1;
+	f->type = -1;
+	f->in_buf = NULL;
+	f->out_buf = NULL;
+	f->open = FALSE;
+
+	/*find a fd for our new friend*/
+	CHECK(list_dequeue(&f->fd,env->free_q) == 0, ENFILE);
+
+	ASSERT(f->fd >= 0);
+	ASSERT(f->fd < env->opt.maxfiles);
+
+	env->fd[f->fd] = f;
+
+	f->in_buf = list_new();
+	CHECK(f->in_buf != NULL, 0);
+
+	f->out_buf = list_new();
+	CHECK(f->out_buf != NULL,0);
+
+	fail:
+
+	if (out == -1) {
+		file_close(f,env);
+		f = NULL;
+	}
+
+	return f;
+
+}
+


Property changes on: bsockets/trunk/contrib/liboverlapped/file.c
___________________________________________________________________
Name: svn:executable
   + *

Added: bsockets/trunk/contrib/liboverlapped/file.h
===================================================================
--- bsockets/trunk/contrib/liboverlapped/file.h	2006-09-19 14:57:31 UTC (rev 8419)
+++ bsockets/trunk/contrib/liboverlapped/file.h	2006-09-19 17:30:05 UTC (rev 8420)
@@ -0,0 +1,40 @@
+#ifndef _FILE_H_
+#define _FILE_H_
+
+
+struct _file {
+
+	/*system file identifier*/
+	void *handle;
+
+	/*local file identifier*/
+	int fd;
+
+	/*what time of file are we (ie, disk file, socket, epoll, etc)*/
+	int type;
+	void *data; //pointer to type specific data
+
+	/*input and output streams*/
+	struct _list *in_buf;
+	struct _list *out_buf;
+
+	/*the last error that occured during file processin
+	(should be reset everytime it is looked at)	*/
+	int err;
+
+	/*set if we are open/connected to something*/
+	int open;
+
+	/*true if we are in blocking mode*/
+	int blocking;
+
+};
+
+void file_close(struct _file *, struct _file_env *env);
+struct _file *file_new(struct _file_env *env);
+struct _file *file_get(int fd, struct _file_env *env);
+
+#define FT_SOCKET	1
+#define FT_EPOLL	2
+
+#endif


Property changes on: bsockets/trunk/contrib/liboverlapped/file.h
___________________________________________________________________
Name: svn:executable
   + *

Added: bsockets/trunk/contrib/liboverlapped/list.c
===================================================================
--- bsockets/trunk/contrib/liboverlapped/list.c	2006-09-19 14:57:31 UTC (rev 8419)
+++ bsockets/trunk/contrib/liboverlapped/list.c	2006-09-19 17:30:05 UTC (rev 8420)
@@ -0,0 +1,138 @@
+#include <stdlib.h>
+#include <errno.h>
+#include "list.h"
+
+
+int list_is_empty(struct _list *list) {
+	return list->head == NULL;
+}
+
+void list_data(void *data, struct _list_node *n) {
+		if (data != NULL) {
+			*((void**) data) = n->data;
+		}
+}
+
+struct _list_node *list_enqueue(void *data, struct _list *list) {
+
+	struct _list_node *n;
+	struct _monitor *m;
+
+	n = (struct _list_node*) malloc(sizeof(struct _list_node));
+
+	if (n != NULL) {
+
+		m = data;
+
+		n->next = NULL;
+		n->prev = list->tail;
+		n->parent = list;
+
+		n->data = data;
+
+		if (list->tail != NULL) {
+			list->tail->next = n;
+		}
+
+		list->tail = n;
+
+		if (list->head == NULL) {
+			list->head = n;
+		}
+
+	} else {
+		errno = ENOMEM;
+	}
+
+	return n;
+
+}
+
+void list_node_detach(struct _list_node *n) {
+
+	struct _list_node *ne;
+	struct _list_node *pr;
+
+	struct _list *list;
+
+	list = n->parent;
+
+	ne = n->next;
+	pr = n->prev;
+
+	if (ne != NULL) {
+		ne->prev = pr;
+	}
+
+	if (pr != NULL) {
+		pr->next = ne;
+	}
+
+	if (n == list->head) {
+		list->head = ne;
+	}
+
+	if (n == list->tail) {
+		list->tail = pr;
+	}
+
+	list_node_free(n);
+
+}
+
+int list_queuepeek(void *data, struct _list *list) {
+
+	int out;
+
+	if (list->head != NULL) {
+
+		list_data(data,list->head);
+
+		out = 0;
+	} else {
+		out = 1;
+	}
+
+	return out;
+}
+
+int list_dequeue(void *data, struct _list *list) {
+
+	int out;
+
+	if ( (out = list_queuepeek(data,list)) == 0) {
+		list_node_detach(list->head);
+	}
+
+	return out;
+}
+
+void list_node_free(struct _list_node *node) {
+	free(node);
+}
+
+struct _list *list_new() {
+
+	struct _list *out;
+
+	out = (struct _list*)
+		malloc(sizeof(struct _list));
+
+	if (out != NULL) {
+		out->head = NULL;
+		out->tail = NULL;
+	} else {
+		errno = ENOMEM;
+	}
+
+	return(out);
+
+}
+
+void list_free(struct _list *list) {
+
+	int n;
+
+	while (list_dequeue(&n,list) == 0);
+	free(list);
+}


Property changes on: bsockets/trunk/contrib/liboverlapped/list.c
___________________________________________________________________
Name: svn:executable
   + *

Added: bsockets/trunk/contrib/liboverlapped/list.h
===================================================================
--- bsockets/trunk/contrib/liboverlapped/list.h	2006-09-19 14:57:31 UTC (rev 8419)
+++ bsockets/trunk/contrib/liboverlapped/list.h	2006-09-19 17:30:05 UTC (rev 8420)
@@ -0,0 +1,39 @@
+#ifndef _LIST_H_
+#define _LIST_H_
+
+struct _list_node {
+
+	struct _list_node *next;
+	struct _list_node *prev;
+
+	struct _list *parent;
+
+	void *data;
+
+};
+
+struct _list {
+
+	struct _list_node *head;
+	struct _list_node *tail;
+
+	//some optional data to be associated with the list (ie, list id)
+	void *data;
+
+};
+
+struct _list *list_new();
+void list_free(struct _list *);
+
+struct _list_node *list_enqueue(void*, struct _list *);
+int list_dequeue(void*, struct _list *);
+int list_queuepeek(void*, struct _list *);
+int list_is_empty(struct _list *);
+
+void list_node_detach(struct _list_node *);
+
+void list_node_free(struct _list_node *);
+
+
+#endif
+


Property changes on: bsockets/trunk/contrib/liboverlapped/list.h
___________________________________________________________________
Name: svn:executable
   + *

Added: bsockets/trunk/contrib/liboverlapped/misc.c
===================================================================
--- bsockets/trunk/contrib/liboverlapped/misc.c	2006-09-19 14:57:31 UTC (rev 8419)
+++ bsockets/trunk/contrib/liboverlapped/misc.c	2006-09-19 17:30:05 UTC (rev 8420)
@@ -0,0 +1,24 @@
+#include <windows.h>
+
+#include "misc.h"
+
+int unixify_w32err(DWORD err) {
+
+	int out;
+
+	switch (err) {
+
+
+
+
+		default:
+			out = err;
+		break;
+
+
+	}
+
+	return out;
+
+}
+


Property changes on: bsockets/trunk/contrib/liboverlapped/misc.c
___________________________________________________________________
Name: svn:executable
   + *

Added: bsockets/trunk/contrib/liboverlapped/misc.h
===================================================================
--- bsockets/trunk/contrib/liboverlapped/misc.h	2006-09-19 14:57:31 UTC (rev 8419)
+++ bsockets/trunk/contrib/liboverlapped/misc.h	2006-09-19 17:30:05 UTC (rev 8420)
@@ -0,0 +1,35 @@
+#ifndef _MISC_H_
+#define _MISC_H_
+
+#include <stdio.h>
+
+int unixify_w32err(DWORD err);
+
+#define ASSERT(X)	if (!(X)) {printf("Assertion failed: [%s]\n\t file:%s line:%d errno:%d w32err: %d\n",#X,__FILE__,__LINE__,errno,(int) GetLastError()); fflush(stdout); _exit(1); }
+
+#define CHECK(X,Y)		if (!(X)) {\
+						out = -1;\
+						if (Y) {\
+							errno = Y;\
+						}\
+						goto fail;\
+					}\
+
+
+#define CHECK_(X)\
+					if(!(X)) {\
+						out = -1;\
+						errno = unixify_w32err(WSAGetLastError());\
+						goto fail;\
+					}\
+
+
+#define CHECK0(X)\
+					if(!(X)) {\
+						out = -1;\
+						errno = unixify_w32err(GetLastError());\
+						goto fail;\
+					}\
+
+
+#endif


Property changes on: bsockets/trunk/contrib/liboverlapped/misc.h
___________________________________________________________________
Name: svn:executable
   + *

Added: bsockets/trunk/contrib/liboverlapped/overlapped.c
===================================================================
--- bsockets/trunk/contrib/liboverlapped/overlapped.c	2006-09-19 14:57:31 UTC (rev 8419)
+++ bsockets/trunk/contrib/liboverlapped/overlapped.c	2006-09-19 17:30:05 UTC (rev 8420)
@@ -0,0 +1,51 @@
+#include <stdarg.h>
+#include <stdio.h>
+
+
+#include "overlapped.h"
+#include "cmd.h"
+
+struct _file_env *__GLOBAL_FILE_ENV_;
+
+#define CMD_PROC(X,FD,N,...)\
+	struct _cmd cmd;\
+\
+	cmd.type = X;\
+	cmd.fd = FD;\
+	make_args(&cmd,N,__VA_ARGS__);\
+\
+	cmd_post(&cmd,__GLOBAL_FILE_ENV_);\
+\
+	if (cmd.ret == -1) {\
+		errno = cmd.err;\
+	}\
+	\
+	return cmd.ret;\
+	\
+
+//todo -- there has to be a smarter way to iterate through arguments
+void make_args(struct _cmd *cmd, int n, ...) {
+
+	int i;
+
+	va_list ap;
+
+	va_start(ap,n);
+	for (i=0; i<n; i++) {
+		cmd->arg[i] = va_arg(ap,void*);
+	}
+	va_end(ap);
+}
+
+int bsocket(int af, int type, int protocol) {
+	CMD_PROC(CMD_SOCKET_NEW,-1,3,af,type,protocol);
+}
+
+
+int bclose(int fd) {
+	CMD_PROC(CMD_FILE_CLOSE,fd,0,NULL);
+}
+
+int bconnect(int fd, struct sockaddr *name, int namelen) {
+	CMD_PROC(CMD_SOCKET_CONNECT,fd,2,name,namelen);
+}


Property changes on: bsockets/trunk/contrib/liboverlapped/overlapped.c
___________________________________________________________________
Name: svn:executable
   + *

Added: bsockets/trunk/contrib/liboverlapped/overlapped.h
===================================================================
--- bsockets/trunk/contrib/liboverlapped/overlapped.h	2006-09-19 14:57:31 UTC (rev 8419)
+++ bsockets/trunk/contrib/liboverlapped/overlapped.h	2006-09-19 17:30:05 UTC (rev 8420)
@@ -0,0 +1,46 @@
+#ifndef _OVERLAPPED_H_
+#define _OVERLAPPED_H_
+
+#include <errno.h>
+
+extern struct _file_env *__GLOBAL_FILE_ENV_;
+
+/* */
+
+struct _file_env_options {
+	int maxfiles;
+	int maxsockets;
+};
+
+
+/*user functions*/
+
+int env_init(struct _file_env_options *);
+void env_shutdown();
+
+/**/
+int bsocket(int, int, int );
+
+
+/*provide some unix errno values, in case your win32 compiler doesn't have them*/
+
+#define LOVERLAPPED_PRIVATE_ERRNO	100000
+
+#ifndef ENOMEM
+#define ENOMEM		LOVERLAPPED_PRIVATE_ERRNO+1
+#endif
+
+#ifndef ECLOSED
+#define ECLOSED		LOVERLAPPED_PRIVATE_ERRNO+2
+#endif
+
+#ifndef EISCONN
+#define EISCONN		LOVERLAPPED_PRIVATE_ERRNO+3
+#endif
+
+#ifndef EALREADY
+#define EALREADY	LOVERLAPPED_PRIVATE_ERRNO+3
+#endif
+
+
+#endif


Property changes on: bsockets/trunk/contrib/liboverlapped/overlapped.h
___________________________________________________________________
Name: svn:executable
   + *

Added: bsockets/trunk/contrib/liboverlapped/socket.c
===================================================================
--- bsockets/trunk/contrib/liboverlapped/socket.c	2006-09-19 14:57:31 UTC (rev 8419)
+++ bsockets/trunk/contrib/liboverlapped/socket.c	2006-09-19 17:30:05 UTC (rev 8420)
@@ -0,0 +1,72 @@
+#include <windows.h>
+
+#include "overlapped.h"
+#include "socket.h"
+#include "file.h"
+#include "cmd.h"
+#include "env.h"
+#include "misc.h"
+#include "cmd.h"
+
+void socket_exception( ) {
+
+}
+
+void socket_connect(struct _file *f, struct _socket *s, struct _cmd *cmd, struct _file_env *env) {
+
+	int out;
+
+	out = 0;
+
+	CHECK(!f->open,EISCONN);
+	CHECK(!s->connecting,EALREADY);
+
+
+
+	fail:
+
+	cmd_done(cmd,out,errno);
+
+}
+
+void socket_new(struct _cmd *cmd, struct _file_env *env) {
+
+	struct _socket *s;
+	struct _file *f;
+
+	int out;
+
+	out = 0;
+
+	f = file_new(env);
+	CHECK(f != NULL,0);
+
+	s = (struct _socket*) malloc(sizeof(struct _socket));
+	CHECK(s != NULL,ENOMEM);
+
+
+	f->handle = (void*)
+		WSASocket(	(int) cmd->arg[0],
+					(int) cmd->arg[1],
+					(int) cmd->arg[2],
+					NULL,
+					0,
+					WSA_FLAG_OVERLAPPED);
+	CHECK_(f->handle != (void*) INVALID_SOCKET);
+
+	f->type = FT_SOCKET;
+
+	s->connecting = FALSE;
+
+	f->data = s;
+
+	fail:
+
+	if (out == -1 && f != NULL) {
+		file_close(f,env);
+	}
+
+	cmd_done(cmd,out,errno);
+
+
+}


Property changes on: bsockets/trunk/contrib/liboverlapped/socket.c
___________________________________________________________________
Name: svn:executable
   + *

Added: bsockets/trunk/contrib/liboverlapped/socket.h
===================================================================
--- bsockets/trunk/contrib/liboverlapped/socket.h	2006-09-19 14:57:31 UTC (rev 8419)
+++ bsockets/trunk/contrib/liboverlapped/socket.h	2006-09-19 17:30:05 UTC (rev 8420)
@@ -0,0 +1,16 @@
+#ifndef _SOCKET_H_
+#define _SOCKET_H_
+
+struct _socket {
+	int connecting;
+};
+
+
+struct _cmd;
+struct _file_env;
+
+void socket_new(struct _cmd *, struct _file_env *);
+
+
+#endif
+


Property changes on: bsockets/trunk/contrib/liboverlapped/socket.h
___________________________________________________________________
Name: svn:executable
   + *

Added: bsockets/trunk/contrib/liboverlapped/test.c
===================================================================
--- bsockets/trunk/contrib/liboverlapped/test.c	2006-09-19 14:57:31 UTC (rev 8419)
+++ bsockets/trunk/contrib/liboverlapped/test.c	2006-09-19 17:30:05 UTC (rev 8420)
@@ -0,0 +1,127 @@
+#include <windows.h>
+#include <stdio.h>
+
+#include "overlapped.h"
+#include "test.h"
+
+#define MAX_FILES		2000
+//todo -- implement maximum sockets checking
+#define MAX_SOCKETS		1500
+
+struct _file_env_options fo = {
+	MAX_FILES,
+	MAX_SOCKETS
+};
+
+int test_fileinit() {
+
+	int mega_tests = 10;
+	int fd;
+
+	int i;
+
+	while (mega_tests--) {
+
+		TESTeq(env_init(&fo),0);
+
+		for (i=0; i<MAX_FILES; i++) {
+			fd = bsocket(AF_INET,SOCK_STREAM,0);
+			TEST(fd != -1);
+		}
+
+		fd = bsocket(AF_INET,SOCK_STREAM,0);
+		TESTeq(fd, -1);
+		TESTeq(errno, ENFILE);
+
+		env_shutdown();
+	}
+
+	return 0;
+}
+
+//make sure connect/accept behave properly
+int test_connect() {
+
+	//const int n = 500;
+
+	TESTeq(env_init(&fo),0);
+
+	env_shutdown();
+
+	return 0;
+
+
+}
+
+struct test_case {
+	int (*fun)();
+	char *desc;
+};
+
+struct test_case tc[] = {
+	{test_fileinit,"Test creating and destroying some sockets"},
+	//{test_socketconnect,"Test connection to ."},
+
+	{NULL,NULL}
+};
+
+
+//stolen from msdn
+static int winsock_start() {
+
+	WORD wVersionRequested;
+	WSADATA wsaData;
+	int err;
+
+	wVersionRequested = MAKEWORD( 2, 2 );
+
+	err = WSAStartup( wVersionRequested, &wsaData );
+
+	if ( err != 0 ) {
+		return 1;
+	}
+
+	if ( LOBYTE( wsaData.wVersion ) != 2 ||
+			HIBYTE( wsaData.wVersion ) != 2 ) {
+
+		WSACleanup();
+		return 1;
+	}
+
+	return 0;
+}
+
+int main() {
+
+	int i=0;
+
+	int good=0;
+	int bad=0;
+
+	if ( winsock_start() != 0) {
+		printf("Couldn't init winsock: %d",WSAGetLastError());
+		fflush(stdout);
+		_exit(1);
+	}
+
+	while (tc[i].fun != NULL) {
+
+		printf("Test %d: %s\n",i,tc[i].desc);
+		fflush(stdout);
+
+		if (tc[i].fun()) {
+			bad++;
+		} else {
+			good++;
+		}
+		i++;
+
+	}
+
+	printf("\nTesting report: passed %d, failed %d\n",good,bad);
+	fflush(stdout);
+
+	return 0;
+
+}
+


Property changes on: bsockets/trunk/contrib/liboverlapped/test.c
___________________________________________________________________
Name: svn:executable
   + *

Added: bsockets/trunk/contrib/liboverlapped/test.h
===================================================================
--- bsockets/trunk/contrib/liboverlapped/test.h	2006-09-19 14:57:31 UTC (rev 8419)
+++ bsockets/trunk/contrib/liboverlapped/test.h	2006-09-19 17:30:05 UTC (rev 8420)
@@ -0,0 +1,17 @@
+#ifndef _TEST_H_
+#define _TEST_H_
+
+int res_;
+int res2_;
+
+#define TESTeq(A,B)  	if ( (res_ = A) != (res2_ = B) ) {\
+							printf("\nTest failed: %s [%d] != %s [%d]\n\tline:%d   errno:%d\n"\
+							,#A,res_,#B,res2_,__LINE__,errno\
+								);\
+							return 1;\
+						}\
+
+#define TEST(X)	TESTeq(X,TRUE);
+
+
+#endif


Property changes on: bsockets/trunk/contrib/liboverlapped/test.h
___________________________________________________________________
Name: svn:executable
   + *