omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / protocol / omnet / cmd / tkad / socket.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | text ds_sock "tkad.sock" |
| 3 | fn dsio fd int b edit uchar 65536 n ulong output ulong to int |
| 4 | p = ulong 0 |
| 5 | deadline = add monons 3000000000 |
| 6 | r = long 0 |
| 7 | while lt p n bound 65537 |
| 8 | left = sub deadline monons |
| 9 | if le left 0 |
| 10 | ret 1 |
| 11 | end |
| 12 | if eq output 0 |
| 13 | r = waitio fd 1 left |
| 14 | else |
| 15 | r = waitio fd 4 left |
| 16 | end |
| 17 | if lt r 0 |
| 18 | if eq r -4 |
| 19 | continue |
| 20 | end |
| 21 | ret 1 |
| 22 | end |
| 23 | if eq r 0 |
| 24 | ret 1 |
| 25 | end |
| 26 | if eq output 0 |
| 27 | r = recv fd b p sub n p int 0 |
| 28 | else |
| 29 | r = send fd b p sub n p int 0 |
| 30 | end |
| 31 | if or eq r -11 eq r -4 |
| 32 | continue |
| 33 | end |
| 34 | if le r 0 |
| 35 | ret 1 |
| 36 | end |
| 37 | p = add p ulong r |
| 38 | end |
| 39 | ret 0 |
| 40 | end |
| 41 | fn dsclient fd int to void |
| 42 | h = new uchar 65536 0 |
| 43 | b = new uchar 65536 0 |
| 44 | o = new uchar 65536 0 |
| 45 | if ne dsio fd h 20 0 0 |
| 46 | ret |
| 47 | end |
| 48 | if or ne get h 0 84 ne get h 1 75 |
| 49 | ret |
| 50 | end |
| 51 | if or ne get h 2 65 ne get h 3 1 |
| 52 | ret |
| 53 | end |
| 54 | if gt get h 5 1 |
| 55 | ret |
| 56 | end |
| 57 | if or ne get h 6 0 ne get h 7 0 |
| 58 | ret |
| 59 | end |
| 60 | n = ulong 0 |
| 61 | for i ulong 4 |
| 62 | n = or n shl ulong get h add i 16 mul i 8 |
| 63 | end |
| 64 | if gt n 65536 |
| 65 | ret |
| 66 | end |
| 67 | put ds_ctl 4 ulong get h 4 |
| 68 | put ds_ctl 3 ulong get h 5 |
| 69 | put ds_ctl 2 dsleget h 8 |
| 70 | if ne dsio fd b n 0 0 |
| 71 | ret |
| 72 | end |
| 73 | z = dsrequest b n o |
| 74 | put h 4 uchar and z 255 |
| 75 | put h 5 uchar shr z 8 |
| 76 | put h 6 0 |
| 77 | put h 7 0 |
| 78 | n = get ds_ctl 6 |
| 79 | for i ulong 4 |
| 80 | put h add i 8 uchar and shr n mul i 8 255 |
| 81 | end |
| 82 | if ne dsio fd h 12 1 0 |
| 83 | ret |
| 84 | end |
| 85 | z = dsio fd o n 1 |
| 86 | end |
| 87 | fn dslisten to int |
| 88 | z = unlink ds_sock 0 len ds_sock |
| 89 | if and ne z 0 ne z -2 |
| 90 | ret -1 |
| 91 | end |
| 92 | addr = new uchar 128 0 |
| 93 | put addr 0 1 |
| 94 | for i ulong len ds_sock |
| 95 | put addr add i 2 get ds_sock i |
| 96 | end |
| 97 | fd = socknew int 1 int 526337 int 0 |
| 98 | if lt fd 0 |
| 99 | ret -1 |
| 100 | end |
| 101 | if ne bind int fd addr 0 add len ds_sock 3 0 |
| 102 | z = fclose int fd |
| 103 | ret -1 |
| 104 | end |
| 105 | if ne chmod ds_sock 0 len ds_sock int 384 0 |
| 106 | z = fclose int fd |
| 107 | ret -1 |
| 108 | end |
| 109 | if ne listen int fd int 32 0 |
| 110 | z = fclose int fd |
| 111 | ret -1 |
| 112 | end |
| 113 | ret int fd |
| 114 | end |
| 115 | fn dsbatch fd int to void |
| 116 | for i ulong 100000 |
| 117 | r = waitio fd int 1 long -1 |
| 118 | if lt r 0 |
| 119 | if eq r -4 |
| 120 | continue |
| 121 | end |
| 122 | exit 75 |
| 123 | end |
| 124 | client = accept fd int 526336 |
| 125 | if lt client 0 |
| 126 | if or eq client -11 eq client -4 |
| 127 | continue |
| 128 | end |
| 129 | exit 75 |
| 130 | end |
| 131 | dsclient int client |
| 132 | z = fclose int client |
| 133 | end |
| 134 | end |