-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.c
185 lines (165 loc) · 4.71 KB
/
pipe.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "world.h"
ai_connection_t *create_connectionlist()
{
/* single instance */
if (connection_list != NULL)
return connection_list;
connection_list = malloc(sizeof(ai_connection_t));
if (connection_list == NULL)
return NULL;
return connection_list;
}
void add_connection_details(ai_connection_t * connection, character_t * character)
{
connection->id = character->id;
connection->character = character;
connection->stdin_buffer = malloc(sizeof(buffer_t));
connection->stdout_buffer = malloc(sizeof(buffer_t));
connection->next = NULL;
if (pipe(connection->pipe_to_ai) < 0 || pipe(connection->pipe_from_ai) < 0) {
debug_msg("pipe error");
return;
}
}
void create_pipe(ai_connection_t *connection, pid_t pid) {
if (pid < 0) {
debug_msg("fork error");
return;
} else if (pid > 0) { // parent
connection->pid = pid;
close(connection->pipe_to_ai[0]); /* close read end */
close(connection->pipe_from_ai[1]); /* close write end */
fcntl(connection->pipe_to_ai[1], F_SETFL, O_NONBLOCK); /* non-blocking write to ai */
fcntl(connection->pipe_from_ai[0], F_SETFL, O_NONBLOCK);
} else { // child
close(connection->pipe_to_ai[1]); /* close write end */
close(connection->pipe_from_ai[0]); /* close read end */
fcntl(connection->pipe_from_ai[1], F_SETFL, O_NONBLOCK); /* non-blocking write from ai */
fflush(stdout);
/* set write end of to_ai pipe fd to STDIN_FILENO */
if (connection->pipe_to_ai[0] != STDIN_FILENO) {
if (dup2(connection->pipe_to_ai[0], STDIN_FILENO) != STDIN_FILENO) {
debug_msg("dup2 error to stdin");
return;
}
close(connection->pipe_to_ai[0]);
}
/* set read end of from_ai pipe fd to STDOUT_FILENO */
if (connection->pipe_from_ai[1] != STDOUT_FILENO) {
if (dup2(connection->pipe_from_ai[1], STDOUT_FILENO) !=
STDOUT_FILENO) {
debug_msg("dup2 error to stdout");
return;
}
close(connection->pipe_from_ai[1]);
}
if (execl("./client", "client", (char *)0) < 0) {
debug_msg("execl error");
return;
}
}
}
ai_connection_t *add_connection(character_t * character)
{
if (connection_list == NULL) {
connection_list = create_connectionlist();
add_connection_details(connection_list, character);
pid_t pid = fork();
create_pipe(connection_list, pid);
int fd = find_free_fd();
connection_list->fd = fd;
fds[fd].fd = connection_list->pipe_from_ai[0];
fds[fd].events = POLLIN;
return connection_list;
}
ai_connection_t *current = get_connection_by_character(character);
if (current != NULL) return current;
current = connection_list;
/*fast-forward to the end of list */
while (current->next != NULL) {
current = current->next;
}
/* now we can add a new variable */
current->next = malloc(sizeof(ai_connection_t));
if (!current->next)
return NULL;
add_connection_details(current->next, character);
pid_t pid = fork();
create_pipe(current->next, pid);
int fd = find_free_fd();
current->next->fd = fd;
fds[fd].fd = current->next->pipe_from_ai[0];
fds[fd].events = POLLIN;
return current->next;
}
void remove_connection(ai_connection_t * connection)
{
if (connection == NULL)
return;
free(connection->stdin_buffer);
free(connection->stdout_buffer);
close(connection->pipe_to_ai[1]);
close(connection->pipe_from_ai[0]);
fds[connection->fd].fd = -1;
for (ai_connection_t **current = &connection_list; *current; current = &(*current)->next) {
if (*current == connection) {
ai_connection_t *next = (*current)->next;
free(*current);
*current = next;
break;
}
}
}
int send(ai_connection_t *connection, char *command, int size) {
if (command[strlen(command) - 1] != '\n') {
command[strlen(command)] = '\n';
size++;
}
int bytes_sent = write(connection->pipe_to_ai[1],
command, size);
return bytes_sent;
}
ai_connection_t *get_connection_by_character(character_t *character) {
if (connection_list == NULL) return NULL;
ai_connection_t *current = connection_list;
while (current != NULL ) {
if (current->character == character) return current;
current = current->next;
}
return NULL;
}
int check_connection(ai_connection_t *connection) {
char line;
if (connection == NULL) return 0;
send(connection, "ping", 4);
if (read(connection->pipe_from_ai[0], &line, 1) <= 0 || line == EOF) {
remove_connection(connection);
wait(0);
return 0;
}
else {
printf("%c\n", line);
return 1;
}
}
int find_free_fd() {
int i = 1;
while (i < MAXFDS) {
if (fds[i].fd <= 0) return i;
i++;
}
return -1;
}
void print_connections()
{
if (connection_list == NULL) {
printf("Connection list empty.\n");
return;
}
printf("Active connections:\n");
ai_connection_t *current = connection_list;
while (current != NULL) {
printf("%i: %s\n", current->fd, current->character->name);
current = current->next;
}
}