Subversion Repositories Mokou

Rev

Rev 7 | Rev 9 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7 Rev 8
Line 1... Line 1...
1
/* $Id: server.c 7 2024-09-07 09:14:35Z nishi $ */
1
/* $Id: server.c 8 2024-09-07 09:22:48Z nishi $ */
2
 
2
 
3
#include "mk_server.h"
3
#include "mk_server.h"
4
 
4
 
-
 
5
#include "mk_service.h"
5
#include "mk_version.h"
6
#include "mk_version.h"
6
#include "mk_util.h"
7
#include "mk_util.h"
7
#include "mk_log.h"
8
#include "mk_log.h"
8
 
9
 
9
#include <stdio.h>
10
#include <stdio.h>
Line 14... Line 15...
14
#include <sys/socket.h>
15
#include <sys/socket.h>
15
#include <sys/un.h>
16
#include <sys/un.h>
16
 
17
 
17
struct sockaddr_un sun;
18
struct sockaddr_un sun;
18
int server;
19
int server;
-
 
20
extern const char* mk_errors[];
19
 
21
 
20
int mk_server_init(void){
22
int mk_server_init(void){
21
	remove("/tmp/mokou.sock");
23
	remove("/tmp/mokou.sock");
22
	memset(&sun, 0, sizeof(sun));
24
	memset(&sun, 0, sizeof(sun));
23
	server = socket(AF_LOCAL, SOCK_STREAM, 0);
25
	server = socket(AF_LOCAL, SOCK_STREAM, 0);
Line 38... Line 40...
38
		return 1;
40
		return 1;
39
	}
41
	}
40
	return 0;
42
	return 0;
41
}
43
}
42
 
44
 
-
 
45
#define PROTOCOL_ERROR "EProtocol Error\n"
-
 
46
 
43
void mk_server_loop(void){
47
void mk_server_loop(void){
44
	struct sockaddr_un cun;
48
	struct sockaddr_un cun;
45
	socklen_t socklen = sizeof(cun);
49
	socklen_t socklen = sizeof(cun);
46
	char* ver = mk_strcat3("V", mk_get_version(), "\n");
50
	char* ver = mk_strcat3("R", mk_get_version(), "\n");
-
 
51
	char cbuf[2];
-
 
52
	cbuf[1] = 0;
-
 
53
	char* str = malloc(1);
-
 
54
	str[0] = 0;
47
	while(1){
55
	while(1){
48
		mk_log("Waiting for the connection");
56
		mk_log("Waiting for the connection");
49
		int cli = accept(server, (struct sockaddr*)&cun, &socklen);
57
		int cli = accept(server, (struct sockaddr*)&cun, &socklen);
50
		send(cli, ver, strlen(ver), 0);
58
		send(cli, ver, strlen(ver), 0);
-
 
59
		while(1){
-
 
60
			if(recv(cli, cbuf, 1, 0) <= 0) break;
-
 
61
			if(cbuf[0] == '\n'){
-
 
62
				if(str[0] == 'U'){
-
 
63
					int err = mk_start_service(str + 1);
-
 
64
					if(err != 0){
-
 
65
						send(cli, "E", 1, 0);
-
 
66
						send(cli, mk_errors[err], strlen(mk_errors[err]), 0);
-
 
67
						send(cli, "\n", 1, 0);
-
 
68
					}else{
-
 
69
						send(cli, "Mok\n", 4, 0);
-
 
70
					}
-
 
71
				}else if(str[0] == 'D'){
-
 
72
					int err = mk_stop_service(str + 1);
-
 
73
					if(err != 0){
-
 
74
						send(cli, "E", 1, 0);
-
 
75
						send(cli, mk_errors[err], strlen(mk_errors[err]), 0);
-
 
76
						send(cli, "\n", 1, 0);
-
 
77
					}else{
-
 
78
						send(cli, "Mok\n", 4, 0);
-
 
79
					}
-
 
80
				}else{
-
 
81
					send(cli, PROTOCOL_ERROR, strlen(PROTOCOL_ERROR), 0);
-
 
82
				}
-
 
83
				free(str);
-
 
84
				str = malloc(1);
-
 
85
				str[0] = 0;
-
 
86
				break;
-
 
87
			}else if(cbuf[0] != '\r'){
-
 
88
				char* tmp = str;
-
 
89
				str = mk_strcat(tmp, cbuf);
-
 
90
				free(tmp);
-
 
91
			}
-
 
92
		}
51
		close(cli);
93
		close(cli);
52
	}
94
	}
53
	free(ver);
95
	free(ver);
-
 
96
	free(ver);
54
}
97
}