changeset 4:8fdd344cdc06 default tip

Yet another rewrite - not functional
author Josef "Jeff" Sipek <jeffpc@optonline.net>
date Mon, 29 Aug 2005 17:30:59 -0500
parents 2607e160df57
children
files main.c
diffstat 1 files changed, 96 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/main.c	Fri Aug 26 14:48:20 2005 -0500
+++ b/main.c	Mon Aug 29 17:30:59 2005 -0500
@@ -22,27 +22,116 @@
 struct proc {
 	pid_t pid;
 	int ignore;
+	int active;
 	char comm[BOT_NAME_LEN];
 };
 
+void sendcmd(int fd, char *cmd);
+void addbot(int fd, struct proc* current);
+
 inline void init_outbuf(char* b)
 {
 	/* all the commands begin with 4 0xff */
 	snprintf(b, BUF_LEN, "\xff\xff\xff\xffrcon %s ", RCON_PASSWORD);
 }
 
+void addbot(int fd, struct proc* current)
+{
+	char *buf;
+	
+	if (!(buf = (char*) malloc(sizeof(char)*BUF_LEN))) {
+		fprintf(stderr, "OOM, not adding bot\n");
+		return;
+	}
+	
+	snprintf(buf, BUF_LEN, "bot addbot %s %d %d",
+		current->comm,
+		1 /* always on this team */,
+		0 /* always a soldier */);
+	
+	printf("adding bot: %s\n", buf);
+	sendcmd(fd, buf);
+}
+
+void removebot(int fd, struct proc* current)
+{
+	char *buf;
+	
+	if (!(buf = (char*) malloc(sizeof(char)*BUF_LEN))) {
+		fprintf(stderr, "OOM, not adding bot\n");
+		return;
+	}
+	
+	snprintf(buf, BUF_LEN, "bot kickbot %s", current->comm);
+	
+	printf("removing bot: %s\n", buf);
+	sendcmd(fd, buf);
+}
+
+void sendcmd(int fd, char *cmd)
+{
+	char *buf;
+	
+	if (!(buf = (char*) malloc(sizeof(char)*BUF_LEN))) {
+		fprintf(stderr, "OOM, command ignored\n");
+		return;
+	}
+	
+	snprintf(buf, BUF_LEN, "\xff\xff\xff\xffrcon %s %s", RCON_PASSWORD, cmd);
+	write(fd, buf, strlen(buf));
+	free(buf);
+}
+
+int update(int fd, struct proc* bots, int pids)
+{
+	int pid;
+	int ret;
+	
+	int owner = CONFIG_OWNER;
+
+	for(pid=0; pid < MAX_PID; pid++) {
+		ret = kill(pid,0);
+		
+		if (owner)
+			ret = !ret;
+		else
+			ret = (!ret || (ret==-1 && errno==EPERM));
+		
+		if (ret) {
+			bots[pid].pid = pid;
+			bots[pid].ignore = (pid==getpid());
+			snprintf(bots[pid].comm, BOT_NAME_LEN, "bot%d", pid);
+			
+			if (!bots[pid].active) {
+				addbot(fd, &bots[pid]);
+				usleep(500000);
+				pids++;
+			}
+			
+			bots[pid].active = 1;
+		} else {
+			if (bots[pid].active) {
+				removebot(fd, &bots[pid]);
+				usleep(250000);
+				pids--;
+			}
+			
+			bots[pid].active = 0;
+		}
+	}
+
+	return pids;
+}
+
 int main(int argc, char** argv)
 {
 	int sockfd;
 	struct sockaddr_in dest_addr;
 	char *ibuf, *obuf;
 	struct proc *bots;
-	int pid, pids;
-	int obuf_preflen;
 	int ret;
+	int pids;
 	
-	int owner = CONFIG_OWNER;
-
 	ibuf = (char*) malloc(sizeof(char)*getpagesize());
 	if (!ibuf) {
 		fprintf(stderr,"OOM\n");
@@ -76,36 +165,14 @@
 	init_outbuf(obuf);
 	memset(bots, 0, sizeof(struct proc)*MAX_PID);
 	
-	obuf_preflen = strlen(obuf);
-	
-	for(pid=0, pids=0; pid < MAX_PID; pid++) {
-		ret = kill(pid,0);
-		
-		if (owner)
-			ret = !ret;
-		else
-			ret = (!ret || (ret==-1 && errno==EPERM));
-		
-		if (ret) {
-			bots[pid].pid = pid;
-			bots[pid].ignore = (pid==getpid());
-			snprintf(bots[pid].comm, BOT_NAME_LEN, "bot%d", pid);
-			
-			snprintf(obuf + obuf_preflen, BUF_LEN-obuf_preflen, "bot addbot %s %d %d",
-				bots[pid].comm,
-				1 /* always on this team */,
-				0 /* always a soldier */);
-			printf("bot: %s\n", obuf);
-			write(sockfd, obuf, strlen(obuf));
-			usleep(500000);
-			pids++;
-		}
-	}
+	pids = update(sockfd, bots, 0);
 	printf("Total %d pids found\n", pids);
 	
 	while(1) {
 		ret = read(sockfd, ibuf, BUF_LEN-1);
 		printf("%d bytes: %s\n",ret, ibuf);
+		pids = update(sockfd, bots, pids);
+		usleep(10000);
 	}
 	
 	close(sockfd);