feat(prepa): add pseudo-code for v1-v4

This commit is contained in:
Paul Alnet 2024-03-01 15:36:37 +01:00
parent 48c9b9e98d
commit 0b699be672

View file

@ -0,0 +1,132 @@
Use `shift + K` in vim/nvim to open the man page of function undor cursor.
# tsock_v1 pseudo-code : UDP only
```
parse and validate arguments.
fail if requests TCP
if source:
build local socket -> socket()
build and resolve destination address -> gethostbyname()
for loop:
construire_message()
send datagrams -> sendto()
exit()
else: # puit
build local socket -> socket()
build address -> INADDR_ANY
bind socket to address -> bind()
while True: # receive unlimited messages
receive datagram -> recvfrom()
afficher_message()
```
# tsock_v2 pseudo-code : UDP/TCP
```
parse and validate arguments.
if source:
build local socket -> socket()
build and resolve destination address -> gethostbyname()
if TCP:
open connection to receiver -> connect()
for loop:
construire_message()
send message -> send()
close connection -> close()
else: # UDP
same as in v1
exit()
else: # puit
build local socket -> socket()
build address -> INADDR_ANY
bind socket to address -> bind()
if TCP :
set incoming connection queue size -> listen()
while True: # receive unlimited connections (one at a time)
accept an incoming connection -> accept()
while connection open and new message:
read incoming stream -> read()
afficher_message()
# connection closed by emitter
else :
same as in v1
```
**EDIT** : Turns out v3/v4 pseudocode isn't required. For your viewing pleasure.
# tsock_v3 pseudo-code : UDP/TCP with custom length & number of messages
Pretty much the same as tsock_v2 but buffer sizes are dynamically allocated (implies changes
in parameters to read/write functions). Also changes the number of iterations in the for loops.
# tsock_v4 pseudo-code : Standalone server
UDP implies on the client listener side sending a hello packet to server to request data. how to know last packet ?
TODO
```
parse and validate arguments.
void send_messages():
for loop:
construire_message()
send message -> send()
if client:
build local socket -> socket()
build and resolve destination address -> gethostbyname()
if TCP:
open connection to server -> connect()
if emitter:
send_messages()
close()
else: # receiver
while connection open:
read()
afficher_message()
if UDP:
if emitter:
send_messages()
else:
send datagram to request data -> send()
while True: # exit condition ??
read()
afficher_message()
else: # server
build local socket -> socket()
build address -> INADDR_ANY
bind socket to address -> bind()
if TCP:
while True:
accept() produces new connection socket
fork()
close(listening socket) on child
close(new connection socket) on parent
# within child process
if emitter:
send_messages()
close()
else: # receiver
while connection open:
read()
afficher_message()
exit() on child
else: # UDP
if emitter:
while read(): # receive request datagram
fork()
# on child
build socket if required
send_messages()
else: # receiver
while True:
read()
afficher_message()
```
When as a TCP server, we may want to reap the dead children processes using waitpid() and errno.