nightmare4_fluffos_v1/
nightmare4_fluffos_v1/bin/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/ChangeLog.old/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/Win32/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/compat/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/compat/simuls/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/include/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/clone/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/command/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/data/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/etc/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/include/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/inherit/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/inherit/master/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/log/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/single/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/single/tests/compiler/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/single/tests/efuns/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/single/tests/operators/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/testsuite/u/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/tmp/
nightmare4_fluffos_v1/fluffos-2.9-ds2.14/windows/
nightmare4_fluffos_v1/lib/cfg/
nightmare4_fluffos_v1/lib/cmds/admins/
nightmare4_fluffos_v1/lib/cmds/common/
nightmare4_fluffos_v1/lib/cmds/creators/include/
nightmare4_fluffos_v1/lib/cmds/creators/include/SCCS/
nightmare4_fluffos_v1/lib/cmds/hm/
nightmare4_fluffos_v1/lib/daemon/services/
nightmare4_fluffos_v1/lib/doc/
nightmare4_fluffos_v1/lib/doc/faq/
nightmare4_fluffos_v1/lib/doc/help/classes/
nightmare4_fluffos_v1/lib/doc/help/creators/
nightmare4_fluffos_v1/lib/doc/help/hm/
nightmare4_fluffos_v1/lib/doc/help/players/
nightmare4_fluffos_v1/lib/doc/help/races/
nightmare4_fluffos_v1/lib/doc/help/religion/
nightmare4_fluffos_v1/lib/doc/mudlib/
nightmare4_fluffos_v1/lib/doc/mudlib/cfg/
nightmare4_fluffos_v1/lib/domains/Ylsrim/
nightmare4_fluffos_v1/lib/domains/Ylsrim/adm/
nightmare4_fluffos_v1/lib/domains/Ylsrim/armour/
nightmare4_fluffos_v1/lib/domains/Ylsrim/fish/
nightmare4_fluffos_v1/lib/domains/Ylsrim/meal/
nightmare4_fluffos_v1/lib/domains/Ylsrim/npc/
nightmare4_fluffos_v1/lib/domains/Ylsrim/virtual/
nightmare4_fluffos_v1/lib/domains/Ylsrim/weapon/
nightmare4_fluffos_v1/lib/domains/Ylsrim/xtra/
nightmare4_fluffos_v1/lib/lib/comp/
nightmare4_fluffos_v1/lib/lib/lvs/
nightmare4_fluffos_v1/lib/lib/std/
nightmare4_fluffos_v1/lib/lib/user/
nightmare4_fluffos_v1/lib/news/
nightmare4_fluffos_v1/lib/obj/
nightmare4_fluffos_v1/lib/obj/include/
nightmare4_fluffos_v1/lib/save/
nightmare4_fluffos_v1/lib/save/kills/a/
nightmare4_fluffos_v1/lib/save/kills/b/
nightmare4_fluffos_v1/lib/save/kills/f/
nightmare4_fluffos_v1/lib/save/kills/m/
nightmare4_fluffos_v1/lib/save/kills/q/
nightmare4_fluffos_v1/lib/save/kills/r/
nightmare4_fluffos_v1/lib/secure/cfg/
nightmare4_fluffos_v1/lib/secure/cfg/classes/
nightmare4_fluffos_v1/lib/secure/cfg/races/SCCS/
nightmare4_fluffos_v1/lib/secure/cmds/creators/include/
nightmare4_fluffos_v1/lib/secure/cmds/players/
nightmare4_fluffos_v1/lib/secure/cmds/players/include/
nightmare4_fluffos_v1/lib/secure/daemon/include/
nightmare4_fluffos_v1/lib/secure/lib/
nightmare4_fluffos_v1/lib/secure/lib/include/
nightmare4_fluffos_v1/lib/secure/lib/net/
nightmare4_fluffos_v1/lib/secure/lib/std/
nightmare4_fluffos_v1/lib/secure/obj/
nightmare4_fluffos_v1/lib/secure/obj/include/
nightmare4_fluffos_v1/lib/secure/save/
nightmare4_fluffos_v1/lib/secure/save/boards/
nightmare4_fluffos_v1/lib/secure/save/votes/
nightmare4_fluffos_v1/lib/spells/
nightmare4_fluffos_v1/lib/verbs/admins/include/
nightmare4_fluffos_v1/lib/verbs/common/
nightmare4_fluffos_v1/lib/verbs/common/include/
nightmare4_fluffos_v1/lib/verbs/creators/
nightmare4_fluffos_v1/lib/verbs/creators/include/
nightmare4_fluffos_v1/lib/verbs/players/include/SCCS/
nightmare4_fluffos_v1/lib/verbs/rooms/
nightmare4_fluffos_v1/lib/verbs/rooms/include/
nightmare4_fluffos_v1/lib/www/
nightmare4_fluffos_v1/old/
nightmare4_fluffos_v1/old/tools/
nightmare4_fluffos_v1/win32/
/*    /lib/net/server.c
 *    From the Nightmare IV LPC Library
 *    a TCP server object
 *    created by Descartes of Borg 950429
 *    Version: @(#) server.c 1.3@(#)
 *    Last modified: 96/12/19
 */

#include <lib.h>
#include <daemons.h>
#include <network.h>
#include <runtime_config.h>

inherit LIB_DAEMON;

class server {
    int         Descriptor;
    int         Blocking;
    int         Closing;
    object      Owner;
    mixed array Buffer;
}

private static int          DestructOnClose= 0;
private static class server Listen         = 0;
private static int          MaxBytes       = get_config(__MAX_BYTE_TRANSFER__);
private static int          Port           = 0;
private static string       SocketObject   = 0;
private static int          SocketType     = STREAM;
private static mapping      Sockets        = ([]);

static void eventSocketError(string msg, int code);

/* ******************* server.c attributes ************************ */
int GetDestructOnClose() {
    return DestructOnClose;
}

static int SetDestructOnClose(int x) {
    return (DestructOnClose = x);
}

int GetSocketType() {
    return SocketType;
}

static int SetSocketType(int x ) {
    return (SocketType = x);
}

/* ******************** server.c events *************************** */
static int eventClose(mixed sock) {
    class server s;

    if( intp(sock) ) {
	sock = Sockets[sock];
    }
    else if( objectp(sock) ) {
	sock = Sockets[sock->GetDescriptor()];
    }
    if( !sock ) {
	return 0;
    }
    s = sock;
    if( s->Blocking ) {
	s->Closing = 1;
	return 1;
    }
    if( Sockets[s->Descriptor] ) {
	map_delete(Sockets, s->Descriptor);
    }
    socket_close(s->Descriptor);
    //    eventSocketClosed(s->Owner);
    if( s->Owner ) {
	s->Owner->eventSocketClosed();
    }
    if( DestructOnClose && sock == Listen ) {
	Destruct();
    }
    sock = 0;
}

int eventCreateSocket(int port) {
    int x;

    Listen = new(class server);
    Listen->Blocking = 0; /* servers are not blocking to start */
    x = socket_create(SocketType,
		      "eventServerReadCallback", 
		      "eventServerAbortCallback");
    if( x < 0 ) {
	eventSocketError("Error in socket_create().", x);
	return x;
    }
    Listen->Descriptor = x;
    x = socket_bind(Listen->Descriptor, port);
    if( x != EESUCCESS ) {
	eventClose(Listen);
	eventSocketError("Error in socket_bind().", x);
	return x;
    }
    x = socket_listen(Listen->Descriptor, "eventServerListenCallback");
    if( x != EESUCCESS ) {
	eventClose(Listen);
	eventSocketError("Error in socket_listen().", x);
	return x;
    }
}

static int Destruct() {
    if( daemon::Destruct() ) {
	foreach(int fd, class server socket in Sockets) {
	    socket->Owner->evenShutdown();
	}
	eventClose(Listen);
	return 1;
    }
    else {
	return 0;
    }
}

int eventDestruct() {
    if( !master()->valid_apply(({})) ) {
	return 0;
    }
    return daemon::eventDestruct();
}

static void eventNewConnection(object socket) {
    class server s = new(class server);
    
    s->Descriptor = socket->GetDescriptor();
    s->Blocking = 0;
    s->Owner = socket;
    Sockets[s->Descriptor] = s;
}

static void eventServerAbortCallback(int fd) {
    eventClose(fd);
}

int eventShutdown() {
    if( previous_object() != find_object(INET_D) ) {
	return 0;
    }
    Destruct();
    return 1;
}

static void eventServerListenCallback(int fd) {
    int x;

    x = socket_accept(fd,
		      "eventServerReadCallback", 
		      "eventServerWriteCallback");
    if( x < 0 ) {
	eventSocketError("Error in socket_accept().", x);
	return;
    }
    if( Sockets[x] ) {
	eventClose(Sockets[x]);
    }
    eventNewConnection(new(SocketObject, x, this_object()));
}

static void eventServerReadCallback(int fd, mixed val) {
    class server s = Sockets[fd];

    if( !s || !s->Owner ) {
	eventClose(fd);
	return;
    }
    else {
	s->Owner->eventRead(val);
    }
}

static void eventServerWriteCallback(int fd) {
    class server sock;
    int x;

    if( Listen && Listen->Descriptor == fd ) {
	sock = Listen;
    }
    else if( Sockets[fd] ) {
	sock = Sockets[fd];
    }
    else {
	return;
    }
    sock->Blocking = 0;
    if( !sock->Buffer && sock->Closing ) {
	eventClose(sock);
	return;
    }
    x = EESUCCESS;
    while( sock->Buffer && x == EESUCCESS ) {
	switch( x = socket_write(sock->Descriptor, sock->Buffer[0]) ) {
            case EESUCCESS:
	        break;
	    case EECALLBACK:
		sock->Blocking = 1;
		break;
	    case EEWOULDBLOCK: 
		call_out( (: eventServerWriteCallback :), 0, fd);
		return;
	    case EEALREADY:
		sock->Blocking = 1;
		return;
	    default:
		eventClose(sock);
		eventSocketError("Error in socket_write().", x);
		return;
	}
	if( sizeof(sock->Buffer) == 1 ) {
	    sock->Buffer = 0;
	    if( sock->Closing && !sock->Blocking ) {
		eventClose(sock);
	    }
	}
	else {
	    sock->Buffer = sock->Buffer[1..];
	}
    }
}

static void eventSocketError(string msg, int code) {
    log_file("servers", "Error code: " + code + "\n" + msg + "\n");
}

varargs int eventWrite(object owner, mixed val, int close) {
    class server sock;
    int fd = owner->GetDescriptor();

    if( Listen && Listen->Descriptor == fd ) {
	sock = Listen;
    }
    else if( Sockets[fd] ) {
	sock = Sockets[fd];
    }
    else {
	return 0;
    }
    if( owner != sock->Owner ) {
	return 0;
    }
    if( SocketType != STREAM ) {
	if( sock->Buffer ) {
	    sock->Buffer += ({ val });
	}
	else {
	    sock->Buffer = ({ val });
	}
    }
    else {
	buffer data = val;
	int size = sizeof(data);
	int count = (size/MaxBytes) + 1;
	
	if( !sock->Buffer ) {
	    sock->Buffer = ({});
	}
	for(int i=0; i<count; i++) {
	    int length, ptr;
	    buffer b;
	    
	    ptr = count * MaxBytes;
	    if( size - ptr > MaxBytes ) {
		length = MaxBytes;
	    }
	    else {
		length = size - ptr;
	    }
	    b = read_buffer(data, ptr, length);
	    sock->Buffer = ({ sock->Buffer..., b });
	}
    }
    sock->Closing = close;
    if( !sock->Blocking ) {
	eventServerWriteCallback(sock->Descriptor);
    }
    return 1;
}

/* ******************** server.c driver applies ******************* */
varargs static void create(int port, int type, string socket_obj) {
    daemon::create();
    SetNoClean(1);
    if( socket_obj ) {
	SocketObject = socket_obj;
    }
    if( !undefinedp(type) ) {
	SocketType = type;
    }
    if( !undefinedp(port) ) {
	Port = port;
	call_out((: eventCreateSocket :), 1, Port);
    }
}