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
93682a51
Commit
93682a51
authored
Jul 13, 2021
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Refactoring: function renaming and reformatting
parent
1f4444e2
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
chat-client.c
+2
-2
2 additions, 2 deletions
chat-client.c
chat-server-iterative.c
+3
-3
3 additions, 3 deletions
chat-server-iterative.c
chat-server-processes.c
+18
-18
18 additions, 18 deletions
chat-server-processes.c
with
23 additions
and
23 deletions
chat-client.c
+
2
−
2
View file @
93682a51
...
...
@@ -144,7 +144,7 @@ void on_signal(int sig) {
send_message
(
socket_fd
,
"EXIT"
);
cleanup
();
fprintf
(
stderr
,
"Terminating due to signal %d.
\n
"
,
sig
);
exit
(
sig
);
exit
(
128
+
sig
);
}
void
cleanup
()
{
...
...
This diff is collapsed.
Click to expand it.
chat-server-iterative.c
+
3
−
3
View file @
93682a51
...
...
@@ -129,5 +129,5 @@ void on_signal(int sig) {
}
}
printf
(
"[STATUS]
\t
Terminating.
\n
"
);
exit
(
sig
);
exit
(
128
+
sig
);
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
chat-server-processes.c
+
18
−
18
View file @
93682a51
...
...
@@ -10,7 +10,7 @@ void manage_client(int socket_fd);
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_
daemon
(
int
sig
);
void
on_signal_
listener
(
int
sig
);
void
on_signal_handler
(
int
sig
);
void
on_sigusr1
(
int
sig
);
...
...
@@ -31,11 +31,11 @@ int main(int argc, const char **argv) {
unsigned
short
port
=
(
unsigned
short
)
strtol
(
argv
[
1
],
NULL
,
10
);
struct
sockaddr_in
server_socket_address
;
listening_socket_fd
=
listen_to_port
(
port
,
&
server_socket_address
);
sigset
(
SIGINT
,
on_signal_
daemon
);
sigset
(
SIGABRT
,
on_signal_
daemon
);
sigset
(
SIGKILL
,
on_signal_
daemon
);
sigset
(
SIGSEGV
,
on_signal_
daemon
);
sigset
(
SIGTERM
,
on_signal_
daemon
);
sigset
(
SIGINT
,
on_signal_
listener
);
sigset
(
SIGABRT
,
on_signal_
listener
);
sigset
(
SIGKILL
,
on_signal_
listener
);
sigset
(
SIGSEGV
,
on_signal_
listener
);
sigset
(
SIGTERM
,
on_signal_
listener
);
display_host_info
(
port
);
pipe
(
pipe_to_repeater
);
manage_server
(
listening_socket_fd
);
...
...
@@ -73,7 +73,7 @@ void manage_server(int connect_socket_fd) {
if
(
pid
)
{
// parent process, listen for the next client
add_client
(
accepted_socket_fd
,
&
client_socket_fd_list
,
pid
,
&
client_handler_pid_list
,
&
client_list_length
);
// many code samples would now close
daemon
's copy of the accepted socket, but ours needs it for the repeater
// many code samples would now close
listener
's copy of the accepted socket, but ours needs it for the repeater
}
else
{
// child process, handle the client
sigset
(
SIGINT
,
on_signal_handler
);
...
...
@@ -82,12 +82,12 @@ void manage_server(int connect_socket_fd) {
sigset
(
SIGSEGV
,
on_signal_handler
);
sigset
(
SIGTERM
,
on_signal_handler
);
sigset
(
SIGUSR1
,
on_sigusr1
);
close
(
connect_socket_fd
);
//
c
lose the listening socket file descriptor **for this process**
close
(
pipe_to_repeater
[
0
]);
//
w
hile we're at it, close the reading end of the pipe
close
(
connect_socket_fd
);
//
C
lose the listening socket file descriptor **for this process**
close
(
pipe_to_repeater
[
0
]);
//
W
hile we're at it, close the reading end of the pipe
for
(
int
i
=
0
;
i
<
client_list_length
;
i
++
)
{
close
(
client_socket_fd_list
[
i
]);
// This process certainly doesn't need file
descriptors for clients it
}
// isn't handling
free
(
client_socket_fd_list
);
//
t
he handler doesn't need these lists
close
(
client_socket_fd_list
[
i
]);
// This process certainly doesn't need file
}
//
descriptors for clients it
isn't handling
free
(
client_socket_fd_list
);
//
T
he handler doesn't need these lists
free
(
client_handler_pid_list
);
fd_flags
=
fcntl
(
accepted_socket_fd
,
F_GETFL
);
fcntl
(
accepted_socket_fd
,
F_SETFL
,
fd_flags
&
!
O_NONBLOCK
);
...
...
@@ -188,14 +188,14 @@ void manage_client(int socket_fd) {
free
(
buffer
);
}
void
on_signal_
daemon
(
int
sig
)
{
void
on_signal_
listener
(
int
sig
)
{
sigset
(
SIGINT
,
SIG_IGN
);
sigset
(
SIGABRT
,
SIG_IGN
);
sigset
(
SIGKILL
,
SIG_IGN
);
sigset
(
SIGSEGV
,
SIG_IGN
);
sigset
(
SIGTERM
,
SIG_IGN
);
running
=
false
;
printf
(
"[STATUS]
\t
Daemon
process %d received signal %d; notifying clients and client handlers.
\n
"
,
getpid
(),
sig
);
printf
(
"[STATUS]
\t
Listener
process %d received signal %d; notifying clients and client handlers.
\n
"
,
getpid
(),
sig
);
for
(
int
i
=
0
;
i
<
client_list_length
;
i
++
)
{
if
(
client_socket_fd_list
[
i
])
{
send_message
(
client_socket_fd_list
[
i
],
"[SERVER]
\t
Server is terminating. EXIT"
);
...
...
@@ -208,8 +208,8 @@ void on_signal_daemon(int sig) {
close
(
listening_socket_fd
);
free
(
client_socket_fd_list
);
free
(
client_handler_pid_list
);
printf
(
"[STATUS]
\t
Daemon
process %d is terminating with signal %d.
\n
"
,
getpid
(),
sig
);
exit
(
sig
);
printf
(
"[STATUS]
\t
Listener
process %d is terminating with signal %d.
\n
"
,
getpid
(),
sig
);
exit
(
128
+
sig
);
}
void
on_signal_handler
(
int
sig
)
{
...
...
@@ -220,7 +220,7 @@ void on_signal_handler(int sig) {
sigset
(
SIGTERM
,
SIG_IGN
);
sigset
(
SIGUSR1
,
SIG_IGN
);
running
=
false
;
printf
(
"[STATUS]
\t
Process %d received signal %d; notifying client and
daemon
.
\n
"
,
getpid
(),
sig
);
printf
(
"[STATUS]
\t
Process %d received signal %d; notifying client and
listener
.
\n
"
,
getpid
(),
sig
);
send_message
(
accepted_socket_fd
,
"[SERVER]
\t
Server experienced abnormal condition. Please try reconnecting. EXIT"
);
char
message_to_repeat
[
BUFFER_SIZE
];
sprintf
(
message_to_repeat
,
"EXIT%d[SERVER]
\t
%s has left the chat due to server issue."
,
accepted_socket_fd
,
name
);
...
...
@@ -232,6 +232,6 @@ void on_sigusr1(int sig) {
printf
(
"[STATUS]
\t
Process %d is terminating with signal %d.
\n
"
,
getpid
(),
sig
);
close
(
pipe_to_repeater
[
1
]);
close
(
accepted_socket_fd
);
exit
(
sig
);
exit
(
128
+
sig
);
}
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