⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmwhod.c

📁 C实现的MUD,对大家基本入门网络游戏很有帮助!
💻 C
字号:
// mudlib: Basis// date:   10/10/92// author: Truilkan@Basis// desc:   mudwho client written for MudOS 0.9 (uses DATAGRAM sockets)// interface:////   call boot() when the mud starts up (in preload() with the code that//     preloads other socket efun code is a good place to call it from)//   call halt() when the mud shuts down (e.g. in a shutdown() simul_efun)//   call add_user(user_object) for each player that logs in (don't forget//     reconnects by netdeaders).//   call remove_user(user_object) for each player that logs out.//     (user::remove() may be a good place to add this call.  Also, you//     wish to call this when netdeath occurs).// DAEMON_IP won't need to change unless you want to point to a different// mudwho dameon.  If you wish to use Portals as your mudwho daemon,// you'll have to get your mud and mud password added to the Portals// mudwho daemon database.//// Here is information on how to contact existing mudwho daemon admins:// ---------------------------------------------------------------------// riemann.math.okstate.edu// - 139.78.1.15  6889, jds@math.okstate.edu// amber.ecst.csuchico.edu// - 132.241.1.43 6889, warlock@ecst.csuchico.edu// nova.tat.physik.uni-nuebingen.de// - 134.2.62.161 6889, gamesmgr@taurus.tat.physik.uni-tuebingen.de#include <net/socket.h> /* includes socket_errors.h indirectly */#define DAEMON_IP "202.160.20.18" /* actlab */#define PASSWORD "spin,ach"// undefine this if you don't want logging of errors#define LOG_INFO// only has an effect if LOG_INFO is defined#undef LOG_CALL_OUTS// change QUERY_NAME and QUERY_LOGIN_TIME to mesh with your mudlib#ifdef BASIS /* for Basis-derived mudlibs */#define QUERY_NAME query(a_cap_name)#define QUERY_LOGIN_TIME query(a_create_time)#else#define QUERY_NAME query("cap_name")#define QUERY_LOGIN_TIME query("last_on")#endif// these values should be okay as long as you are talking to the Portals daemon#define REFRESH_INTERVAL 390#define KEEPALIVE_INTERVAL 120#define PARTITIONS 10// you shouldn't need to change anything below this comment#define TAB "\t"#define GENERATION "0"#define	UDP_PORT	8892#define CLIENT_VERSION "mwp 1.2"nosave string mwhod_addr;nosave string mudname;nosave string keepalive_message;nosave string boot_message;nosave string comments;nosave int socket;voidlog_info(string error){#ifdef LOG_INFO	log_file("cmwhod", ctime(time()) + "\n" + error);#endif}protected voidsend_data(string datagram){#ifdef INTERMUD	int rc;	rc = socket_write(socket, datagram, mwhod_addr);	if (rc != EESUCCESS) {		log_info("socket_write: " + socket_error(rc) + "\n");	}#endif /* INTERMUD */}protected stringheader(string op){	return op + TAB + mudname + TAB + PASSWORD;}protected voidset_keepalive_message(){	/* uptime() is an efun that returns # of seconds the driver has been up */	keepalive_message = header("M") + TAB + mudname + TAB +		(time() - uptime()) + TAB + GENERATION + TAB + comments;}protected voidset_boot_message(){	boot_message = header("U") + TAB + mudname + TAB +		(time() - uptime()) + TAB + GENERATION + TAB + comments;}protected voidset_comments(){	comments = __VERSION__ + "/" + CLIENT_VERSION;}voidadd_user(object user, int which){	string name, datagram;	int userid, login_time;	if (!user) {		return;	}	userid = getoid(user);	/* refresh approx. 1/PARTITIONS of list each time */	if ((userid % PARTITIONS) != which) {		return;	}	login_time = (int)user->QUERY_LOGIN_TIME;	name = (string)user->QUERY_NAME;	datagram =		header("A")+TAB+mudname+TAB+userid+"@"+MUD_NAME+TAB+login_time+TAB+GENERATION			+TAB+name;	send_data(datagram);}voidadd_all_users(int which){	object *all;	int j;	all = users();	for (j = 0; j < sizeof(all); j++) {		add_user(all[j], which);	}}voidrefresh(int which){	string err;#ifdef LOG_CALL_OUTS	log_info("call_out: refresh " + which + "\n");#endif	// do the catch() so the call_out won't be lost in case of runtime error.	err = catch(add_all_users(which));	if (err) {		log_info("refresh: " + err + "\n");	}	call_out("refresh", REFRESH_INTERVAL / PARTITIONS,		(which + 1) % PARTITIONS);}/* * Initialize CMWHOD */voidcreate(){	int error;	rm(LOG_DIR + "/cmwhod");	log_info(CLIENT_VERSION + ": cmwhod log for '" + MUD_NAME + "'\n");	socket = socket_create(DATAGRAM, "read", "close");	if (socket < 0) {		log_info("socket_create: " + socket_error(socket) + "\n");		return;	}	error = socket_bind(socket, 0);	if (error != EESUCCESS) {		log_info("socket_bind: " + socket_error(error) + "\n");		return;    }	set_comments();	mwhod_addr = DAEMON_IP + " " + UDP_PORT;	mudname = MUD_NAME;	set_keepalive_message();	set_boot_message();	call_out("keepalive", KEEPALIVE_INTERVAL);	call_out("refresh", REFRESH_INTERVAL / PARTITIONS, 0);}voidkeepalive(){#ifdef LOG_CALL_OUTS	log_info("call_out: keepalive:\n" + keepalive_message + "\n");#endif	/* shouldn't be able to get a runtime error here */	send_data(keepalive_message);	call_out("keepalive", KEEPALIVE_INTERVAL);}voidboot(){	log_info("booting");	send_data(boot_message);}voidhalt(){	log_info("halting");	send_data(header("D"));}voidremove_user(object user){	string datagram;	int userid;	userid = getoid(user); /* get number following # in file_name(user) */	datagram = header("Z") + TAB + mudname + TAB + userid+"@"+MUD_NAME;	send_data(datagram);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -