<<
path:
root/public/blog.git/html/tests/backend_socket.jai
blob: 8bb85784082e2a1722bd39e47e285fc86604307a
[raw]
[clear marker]
3Disruption_Kind :: enum_flags {
12 kind: Disruption_Kind;
16socket_dispatch :: (args: Args, is_set: Is_Set) {
17 disruption := disruption_make(args, is_set);
19 code_connect :: #code { disruption_dispatch(disruption, .CONNECT); };
20 code_send :: #code { disruption_dispatch(disruption, .SEND); };
30test__socket_send_message :: #procedure_of_call socket_send_message("A");
44yap :: inline (msg: string, args: ..Any) {
45 #if !NO_YAP then log(msg, ..args);
48socket_send_message :: inline (message: string) {
49 socket_send_message(message, {}, #code {}, #code {});
52socket_send_message :: (
53 message: string, disruption: Disruption, $code_connect: Code, $code_send: Code
62 sock := socket(AF_INET, .STREAM, 0);
63 defer close_and_reset(*sock);
64 assert(sock > 0, "Socket");
66 server_addr: sockaddr_in;
67 server_addr.sin_family = AF_INET;
68 server_addr.sin_port = htons(PORT);
69 server_addr.sin_addr.s_addr = inet_addr(HOST);
71 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, *tv, size_of(type_of(tv)));
72 setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, *tv, size_of(type_of(tv)));
74 result := connect(sock, cast(*sockaddr, *server_addr), size_of(type_of(server_addr)));
76 log_error("No connection");
80 #insert,scope() code_connect;
82 yap("Connected to %:%", HOST, PORT);
84 bytes_sent := send(sock, message.data, xx message.count, 0);
85 assert(bytes_sent >= 0, "Failed to send message");
87 #insert,scope() code_send;
89 yap("Sent % bytes\n", bytes_sent);
92 bytes_received := recv(sock, buffer.data, buffer.count, 0);
94 if bytes_received > 0 {
95 response := string.{ xx bytes_received, buffer.data };
96 print("Server response: %\n", response);
100disruption_make :: (args: Args, is_set: Is_Set) -> Disruption {
102 d.delay = args.delay;
104 if is_set.connect then d.kind |= .CONNECT;
105 if is_set.send then d.kind |= .SEND;
106 if is_set.cancel then d.kind |= .CANCEL;
111disruption_dispatch :: (d: Disruption, $post_what: Disruption_Kind) #expand {
112 if (d.kind & post_what == 0) return;
114 yap("-- Mode: % ------------", post_what);
116 if (d.delay > 0) then yap("Sleeping for % ms", d.delay);
117 sleep_milliseconds(d.delay);
119 if d.kind & .CANCEL != 0 {