Skip to content

Commit 96b0333

Browse files
committed
who even knows if this is better or worse than it was yesterday but it sure is different because i changed a lot of the code and for that reason i believe i should make a commit but i'm kind of tired so i think this commit message might end up a little bit rambly but that's ok because i say so
1 parent 112ddd9 commit 96b0333

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

server

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ function subscribe() {
8585
}
8686

8787
function unsubscribe() {
88-
debug "PIPE CLOSING: $SUBSCRIPTION"
88+
[[ ! -z "$UNSUBBED" ]] && return
89+
UNSUBBED=true
8990
rm -f "$SUBSCRIPTION"
9091
debug "PIPE CLOSED: $SUBSCRIPTION"
9192
}
@@ -109,6 +110,20 @@ debug() {
109110
printf "%s\n" "$@" 1>&2
110111
}
111112

113+
PONG_COUNT=0
114+
PING=0
115+
heartbeat() {
116+
while [[ -z "$UNSUBBED" ]]; do
117+
if [[ $PONG_COUNT -lt $PING ]]; then
118+
debug "NO PONG? GOODBYE"
119+
exit 1
120+
fi
121+
reply PING ":$PING"
122+
((PING++))
123+
sleep 60
124+
done
125+
}
126+
112127
if [[ -z "$IRC_CONN_HANDLER" ]]; then
113128

114129
rm -rf pubsub
@@ -126,10 +141,12 @@ if [[ -z "$IRC_CONN_HANDLER" ]]; then
126141

127142
export IRC_CONN_HANDLER=true
128143
export IRC_SERVER_CREATED="$(date)"
129-
tcpserver -1 -o -l 0 -H -R -c 1000 0 $PORT $0
144+
tcpserver -1 -o -l 0 -H -D -R -c 1000 0 $PORT $0
130145
else
131146
# echo "connection opened with $TCPREMOTEIP" 1>&2
132147
subscribe
148+
heartbeat &
149+
trap unsubscribe EXIT
133150
while IFS= read -r line; do
134151
split "${line//$'\r'}" ' '
135152
if [[ "${arr[0]}" =~ :(.+) ]]; then
@@ -190,12 +207,15 @@ else
190207
PING)
191208
reply PONG "$NICK" "${arr[0]}"
192209
;;
210+
PONG)
211+
((PONG_COUNT++))
212+
;;
193213
*)
194214
debug "UNRECOGNIZED COMMAND"
195215
debug "${arr[@]}"
196216
;;
197217
esac
198218
done
199-
# echo "connection closed with $TCPREMOTEIP" 1>&2
200219
unsubscribe
220+
# echo "connection closed with $TCPREMOTEIP" 1>&2
201221
fi

0 commit comments

Comments
 (0)