Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
simple chat
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Christopher Bohn
simple chat
Commits
0ad6972d
Commit
0ad6972d
authored
4 years ago
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Extracted add/remove client from process-based server to common code
parent
93682a51
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
chat-server-processes.c
+0
-41
0 additions, 41 deletions
chat-server-processes.c
chat-server.c
+36
-0
36 additions, 0 deletions
chat-server.c
chat-server.h
+2
-0
2 additions, 0 deletions
chat-server.h
with
38 additions
and
41 deletions
chat-server-processes.c
+
0
−
41
View file @
0ad6972d
#include
<stdbool.h>
#include
<signal.h>
#include
<errno.h>
#include
<errno.h>
#include
<fcntl.h>
#include
<fcntl.h>
#include
"chat.h"
#include
"chat-server.h"
#include
"chat-server.h"
void
manage_server
(
int
connect_socket_fd
);
void
manage_server
(
int
connect_socket_fd
);
void
manage_client
(
int
socket_fd
);
void
manage_client
(
int
socket_fd
);
void
repeat_messages
(
int
*
client_fd_list
,
pid_t
*
handler_pid_list
,
int
list_length
,
char
*
message
);
void
repeat_messages
(
int
*
client_fd_list
,
pid_t
*
handler_pid_list
,
int
list_length
,
char
*
message
);
void
add_client
(
int
new_client_fd
,
int
**
client_fd_list
,
pid_t
new_handler_pid
,
pid_t
**
handler_pid_list
,
int
*
list_length
);
void
remove_client
(
int
exiting_client_fd
,
int
*
client_fd_list
,
pid_t
*
handler_pid_list
);
void
on_signal_listener
(
int
sig
);
void
on_signal_listener
(
int
sig
);
void
on_signal_handler
(
int
sig
);
void
on_signal_handler
(
int
sig
);
void
on_sigusr1
(
int
sig
);
void
on_sigusr1
(
int
sig
);
...
@@ -100,42 +95,6 @@ void manage_server(int connect_socket_fd) {
...
@@ -100,42 +95,6 @@ void manage_server(int connect_socket_fd) {
}
}
}
}
void
add_client
(
int
new_client_fd
,
int
**
client_fd_list
,
pid_t
new_handler_pid
,
pid_t
**
handler_pid_list
,
int
*
list_length
)
{
int
i
=
0
;
int
*
client_list
=
*
client_fd_list
;
int
*
handler_list
=
*
handler_pid_list
;
while
(
client_list
[
i
]
&&
(
i
<
*
list_length
))
{
i
++
;
}
if
(
i
==
*
list_length
)
{
// need to grow the lists
client_list
=
calloc
(
2
*
(
*
list_length
),
sizeof
(
int
));
handler_list
=
calloc
(
2
*
(
*
list_length
),
sizeof
(
pid_t
));
for
(
int
j
=
0
;
j
<
*
list_length
;
j
++
)
{
client_list
[
j
]
=
(
*
client_fd_list
)[
j
];
handler_list
[
j
]
=
(
*
handler_pid_list
)[
j
];
}
free
(
*
client_fd_list
);
free
(
*
handler_pid_list
);
*
list_length
*=
2
;
}
client_list
[
i
]
=
new_client_fd
;
handler_list
[
i
]
=
new_handler_pid
;
*
client_fd_list
=
client_list
;
*
handler_pid_list
=
handler_list
;
}
void
remove_client
(
int
exiting_client_fd
,
int
*
client_fd_list
,
pid_t
*
handler_pid_list
)
{
if
(
exiting_client_fd
)
{
while
(
*
client_fd_list
!=
exiting_client_fd
)
{
client_fd_list
++
;
handler_pid_list
++
;
}
*
client_fd_list
=
0
;
*
handler_pid_list
=
0
;
}
}
void
repeat_messages
(
int
*
client_fd_list
,
pid_t
*
handler_pid_list
,
int
list_length
,
char
*
message
)
{
void
repeat_messages
(
int
*
client_fd_list
,
pid_t
*
handler_pid_list
,
int
list_length
,
char
*
message
)
{
long
exiting_client_fd
;
long
exiting_client_fd
;
char
*
original_start
,
*
new_start
;
char
*
original_start
,
*
new_start
;
...
...
This diff is collapsed.
Click to expand it.
chat-server.c
+
36
−
0
View file @
0ad6972d
...
@@ -52,3 +52,39 @@ int listen_to_port(unsigned short port, struct sockaddr_in *server_socket_addres
...
@@ -52,3 +52,39 @@ int listen_to_port(unsigned short port, struct sockaddr_in *server_socket_addres
}
}
return
listening_socket_fd
;
return
listening_socket_fd
;
}
}
void
add_client
(
int
new_client_fd
,
int
**
client_fd_list
,
pid_t
new_handler_pid
,
pid_t
**
handler_pid_list
,
int
*
list_length
)
{
int
i
=
0
;
int
*
client_list
=
*
client_fd_list
;
int
*
handler_list
=
*
handler_pid_list
;
while
(
client_list
[
i
]
&&
(
i
<
*
list_length
))
{
i
++
;
}
if
(
i
==
*
list_length
)
{
// need to grow the lists
client_list
=
calloc
(
2
*
(
*
list_length
),
sizeof
(
int
));
handler_list
=
calloc
(
2
*
(
*
list_length
),
sizeof
(
pid_t
));
for
(
int
j
=
0
;
j
<
*
list_length
;
j
++
)
{
client_list
[
j
]
=
(
*
client_fd_list
)[
j
];
handler_list
[
j
]
=
(
*
handler_pid_list
)[
j
];
}
free
(
*
client_fd_list
);
free
(
*
handler_pid_list
);
*
list_length
*=
2
;
}
client_list
[
i
]
=
new_client_fd
;
handler_list
[
i
]
=
new_handler_pid
;
*
client_fd_list
=
client_list
;
*
handler_pid_list
=
handler_list
;
}
void
remove_client
(
int
exiting_client_fd
,
int
*
client_fd_list
,
pid_t
*
handler_pid_list
)
{
if
(
exiting_client_fd
)
{
while
(
*
client_fd_list
!=
exiting_client_fd
)
{
client_fd_list
++
;
handler_pid_list
++
;
}
*
client_fd_list
=
0
;
*
handler_pid_list
=
0
;
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
chat-server.h
+
2
−
0
View file @
0ad6972d
...
@@ -7,5 +7,7 @@
...
@@ -7,5 +7,7 @@
void
display_host_info
(
unsigned
short
port
);
void
display_host_info
(
unsigned
short
port
);
int
listen_to_port
(
unsigned
short
port
,
struct
sockaddr_in
*
server_socket_address
);
int
listen_to_port
(
unsigned
short
port
,
struct
sockaddr_in
*
server_socket_address
);
void
add_client
(
int
new_client_fd
,
int
**
client_fd_list
,
pid_t
new_handler_pid
,
pid_t
**
handler_pid_list
,
int
*
list_length
);
void
remove_client
(
int
exiting_client_fd
,
int
*
client_fd_list
,
pid_t
*
handler_pid_list
);
#endif //CHAT_SERVER_H
#endif //CHAT_SERVER_H
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment