Commit f3d972c69c7de85209753f435d5a67cfcd19053a
1 parent
48f21c0a
Add Data_IO "object" for reading from file, network or ByteAray.
we use now read_bytes from DataIO instead of read_network_bytes located in network/tools
Showing
3 changed files
with
130 additions
and
36 deletions
Show diff stats
anubis_dev/library/network/tools.anubis
| ... | ... | @@ -35,13 +35,13 @@ public define Byte_Net_Result |
| 35 | 35 | }. |
| 36 | 36 | |
| 37 | 37 | |
| 38 | -public type Bytes_Net_Result: | |
| 38 | + public type Bytes_Net_Result: | |
| 39 | 39 | failure, |
| 40 | 40 | time_out, |
| 41 | 41 | success(ByteArray), |
| 42 | 42 | truncated(ByteArray). |
| 43 | 43 | |
| 44 | -public define Bytes_Net_Result | |
| 44 | + public define Bytes_Net_Result | |
| 45 | 45 | read_network_bytes |
| 46 | 46 | ( |
| 47 | 47 | RAddr(Int8) conn, |
| ... | ... | @@ -195,4 +195,4 @@ public define One |
| 195 | 195 | print(text+"\n"). |
| 196 | 196 | |
| 197 | 197 | |
| 198 | - | |
| 199 | 198 | \ No newline at end of file |
| 199 | + | ... | ... |
| 1 | + | |
| 2 | + | |
| 3 | +public type Data_IO:... | |
| 4 | + | |
| 5 | + | |
| 6 | +public type Data_IO_Result: | |
| 7 | + failure, | |
| 8 | + time_out, | |
| 9 | + success(ByteArray), | |
| 10 | + truncated(ByteArray). | |
| 11 | + | |
| 12 | +public define Data_IO_Result read_bytes (Data_IO data, Int32 how_many). | |
| 13 | + | |
| 14 | +public define Data_IO make_data_io(RAddr(Int8) file). | |
| 15 | +public define Data_IO make_data_io(ByteArray byte_array). | |
| 16 | + | |
| 17 | + | |
| 18 | +public type Data_IO: | |
| 19 | + data_io(Int32 -> Data_IO_Result read_bytes,// read a bytes | |
| 20 | + One -> Int32 offset). // offset | |
| 21 | + | |
| 22 | + /* File interface */ | |
| 23 | + | |
| 24 | +define Int32 -> Data_IO_Result | |
| 25 | + make_read_bytes // making the 'rb' function | |
| 26 | + ( | |
| 27 | + RAddr(Int8) file, | |
| 28 | + Var(Int32) offset, | |
| 29 | + ) = | |
| 30 | + (Int32 how_many) |-> | |
| 31 | + if read(file, how_many, 10) is //by default the time_out is 10 seconds | |
| 32 | + { | |
| 33 | + failure then failure, | |
| 34 | + success(bytes) then | |
| 35 | + with len = length(bytes), | |
| 36 | + offset <- *offset + len; //update the offset | |
| 37 | + //if the returned length size is 0, it means timeout | |
| 38 | + if len = 0 then | |
| 39 | + time_out | |
| 40 | + else if len = how_many then | |
| 41 | + success(bytes) | |
| 42 | + else | |
| 43 | + truncated(bytes) | |
| 44 | + }. | |
| 45 | + | |
| 46 | +public define Data_IO | |
| 47 | + make_data_io | |
| 48 | + ( | |
| 49 | + RAddr(Int8) file | |
| 50 | + ) = | |
| 51 | + with o = var((Int32)0), // offset | |
| 52 | + data_io( | |
| 53 | + make_read_bytes(file, o), | |
| 54 | + (One u) |-> *o). | |
| 55 | + | |
| 56 | + /* ByteArray interface */ | |
| 57 | + | |
| 58 | +define Int32 -> Data_IO_Result | |
| 59 | + make_read_bytes // making the 'rb' function | |
| 60 | + ( | |
| 61 | + ByteArray byte_array, | |
| 62 | + Var(Int32) offset, | |
| 63 | + ) = | |
| 64 | + (Int32 how_many) |-> | |
| 65 | + with result_ba = extract(byte_array, *offset, *offset + how_many), | |
| 66 | + len = length(result_ba), | |
| 67 | + offset <- *offset + len; //update the offset | |
| 68 | + //if the returned length size is 0, it means we are at end of buffer | |
| 69 | + if len = 0 then | |
| 70 | + failure | |
| 71 | + else if len = how_many then | |
| 72 | + success(result_ba) | |
| 73 | + else | |
| 74 | + truncated(result_ba). | |
| 75 | + | |
| 76 | +public define Data_IO | |
| 77 | + make_data_io | |
| 78 | + ( | |
| 79 | + ByteArray byte_array | |
| 80 | + ) = | |
| 81 | + with o = var((Int32)0), // offset | |
| 82 | + data_io( | |
| 83 | + make_read_bytes(byte_array, o), | |
| 84 | + (One u) |-> *o). | |
| 85 | + | |
| 86 | + | |
| 87 | +public define Data_IO_Result | |
| 88 | + read_bytes | |
| 89 | + ( | |
| 90 | + Data_IO io, | |
| 91 | + Int32 how_many | |
| 92 | + ) = | |
| 93 | + read_bytes(io)(how_many). | ... | ... |
anubis_dev/library/system/muscle.anubis
| ... | ... | @@ -6,6 +6,7 @@ read network/tools.anubis |
| 6 | 6 | read system/string.anubis |
| 7 | 7 | read system/types.anubis |
| 8 | 8 | read system/convert.anubis |
| 9 | +read system/data_io.anubis | |
| 9 | 10 | |
| 10 | 11 | public define Int32 _B_ANY_TYPE = 1095653716. // 'ANYT', // wild card |
| 11 | 12 | public define Int32 _B_BOOL_TYPE = 1112493900. // 'BOOL', |
| ... | ... | @@ -27,7 +28,7 @@ public define Int32 _CURRENT_PROTOCOL_VERSION = 1347235888. // 'PM00' -- our mag |
| 27 | 28 | |
| 28 | 29 | public type Message:... |
| 29 | 30 | |
| 30 | -define Maybe(Message) unflatten_message ( RAddr(Int8) stream ). | |
| 31 | +define Maybe(Message) unflatten_message ( Data_IO io ). | |
| 31 | 32 | define Maybe(ByteArray) flatten_message ( Message msg ). |
| 32 | 33 | |
| 33 | 34 | |
| ... | ... | @@ -188,23 +189,23 @@ public define Bool |
| 188 | 189 | define Maybe(ByteArray) |
| 189 | 190 | read_data |
| 190 | 191 | ( |
| 191 | - RAddr(Int8) stream, | |
| 192 | - Int32 nb_bytes | |
| 192 | + Data_IO io, | |
| 193 | + Int32 nb_bytes | |
| 193 | 194 | ) = |
| 194 | - if read_network_bytes(stream, nb_bytes, 10) is | |
| 195 | + if read_bytes(io, nb_bytes) is | |
| 195 | 196 | { |
| 196 | - failure then failure, | |
| 197 | - time_out then failure, | |
| 198 | - success(bytes) then success(bytes), | |
| 199 | - truncated(_) then failure | |
| 197 | + failure then failure, | |
| 198 | + time_out then failure, | |
| 199 | + success(bytes) then success(bytes), | |
| 200 | + truncated(_) then failure | |
| 200 | 201 | }. |
| 201 | 202 | |
| 202 | 203 | define Maybe(Int32) |
| 203 | 204 | read_Int32 |
| 204 | 205 | ( |
| 205 | - RAddr(Int8) stream, | |
| 206 | + Data_IO io, | |
| 206 | 207 | ) = |
| 207 | - if read_network_bytes(stream, 4, 10) is | |
| 208 | + if read_bytes(io, 4) is | |
| 208 | 209 | { |
| 209 | 210 | failure then failure, |
| 210 | 211 | time_out then failure, |
| ... | ... | @@ -219,9 +220,9 @@ define Maybe(Int32) |
| 219 | 220 | define Maybe(Int64) |
| 220 | 221 | read_Int64 |
| 221 | 222 | ( |
| 222 | - RAddr(Int8) stream, | |
| 223 | + Data_IO io, | |
| 223 | 224 | ) = |
| 224 | - if read_network_bytes(stream, 8, 10) is | |
| 225 | + if read_bytes(io, 8) is | |
| 225 | 226 | { |
| 226 | 227 | failure then failure, |
| 227 | 228 | time_out then failure, |
| ... | ... | @@ -236,9 +237,9 @@ define Maybe(Int64) |
| 236 | 237 | define Maybe(Float32) |
| 237 | 238 | read_float32 |
| 238 | 239 | ( |
| 239 | - RAddr(Int8) stream, | |
| 240 | + Data_IO io, | |
| 240 | 241 | ) = |
| 241 | - if read_network_bytes(stream, 4, 10) is | |
| 242 | + if read_bytes(io, 4) is | |
| 242 | 243 | { |
| 243 | 244 | failure then failure, |
| 244 | 245 | time_out then failure, |
| ... | ... | @@ -253,9 +254,9 @@ define Maybe(Float32) |
| 253 | 254 | define Maybe(Float) |
| 254 | 255 | read_float64 |
| 255 | 256 | ( |
| 256 | - RAddr(Int8) stream, | |
| 257 | + Data_IO io, | |
| 257 | 258 | ) = |
| 258 | - if read_network_bytes(stream, 8, 10) is | |
| 259 | + if read_bytes(io, 8) is | |
| 259 | 260 | { |
| 260 | 261 | failure then failure, |
| 261 | 262 | time_out then failure, |
| ... | ... | @@ -268,9 +269,9 @@ define Maybe(Float) |
| 268 | 269 | define Maybe(Int16) |
| 269 | 270 | read_Int16 |
| 270 | 271 | ( |
| 271 | - RAddr(Int8) stream, | |
| 272 | + Data_IO io, | |
| 272 | 273 | ) = |
| 273 | - if read_network_bytes(stream, 2, 10) is | |
| 274 | + if read_bytes(io, 2) is | |
| 274 | 275 | { |
| 275 | 276 | failure then failure, |
| 276 | 277 | time_out then failure, |
| ... | ... | @@ -283,8 +284,8 @@ define Maybe(Int16) |
| 283 | 284 | define Maybe(Muscle_object) |
| 284 | 285 | read_one_field_item |
| 285 | 286 | ( |
| 286 | - RAddr(Int8) source, | |
| 287 | - Muscle_type b_type | |
| 287 | + Data_IO source, | |
| 288 | + Muscle_type b_type | |
| 288 | 289 | )= |
| 289 | 290 | if b_type is |
| 290 | 291 | { |
| ... | ... | @@ -464,7 +465,7 @@ define Maybe(Muscle_object) |
| 464 | 465 | define Maybe(List(Muscle_object)) |
| 465 | 466 | read_field_items |
| 466 | 467 | ( |
| 467 | - RAddr(Int8) source, | |
| 468 | + Data_IO source, | |
| 468 | 469 | Muscle_type b_type, |
| 469 | 470 | Int32 count |
| 470 | 471 | )= |
| ... | ... | @@ -485,8 +486,8 @@ define Maybe(List(Muscle_object)) |
| 485 | 486 | define Maybe(List(Muscle_object)) |
| 486 | 487 | read_message_field_items |
| 487 | 488 | ( |
| 488 | - RAddr(Int8) source, | |
| 489 | - Int32 amount | |
| 489 | + Data_IO source, | |
| 490 | + Int32 amount | |
| 490 | 491 | )= |
| 491 | 492 | if amount =< 0 then |
| 492 | 493 | success([ ]) |
| ... | ... | @@ -511,7 +512,7 @@ define Maybe(List(Muscle_object)) |
| 511 | 512 | define Maybe(Message_field) |
| 512 | 513 | read_one_field |
| 513 | 514 | ( |
| 514 | - RAddr(Int8) source | |
| 515 | + Data_IO source | |
| 515 | 516 | )= |
| 516 | 517 | //read length of the field name (4 bytes) |
| 517 | 518 | if read_Int32(source) is |
| ... | ... | @@ -676,8 +677,8 @@ define Maybe(Message_field) |
| 676 | 677 | define Maybe(List(Message_field)) |
| 677 | 678 | read_fields |
| 678 | 679 | ( |
| 679 | - RAddr(Int8) source, | |
| 680 | - Int32 n // number of elements to read | |
| 680 | + Data_IO source, | |
| 681 | + Int32 n // number of elements to read | |
| 681 | 682 | ) = |
| 682 | 683 | if n =< 0 then |
| 683 | 684 | success([ ]) |
| ... | ... | @@ -877,28 +878,28 @@ define Maybe(ByteArray) |
| 877 | 878 | define Maybe(Message) |
| 878 | 879 | unflatten_message |
| 879 | 880 | ( |
| 880 | - RAddr(Int8) stream | |
| 881 | + Data_IO io | |
| 881 | 882 | )= |
| 882 | - if read_Int32(stream) is | |
| 883 | + if read_Int32(io) is | |
| 883 | 884 | { |
| 884 | 885 | failure then failure, |
| 885 | 886 | success(protocolVersion) then |
| 886 | 887 | if protocolVersion = _CURRENT_PROTOCOL_VERSION then |
| 887 | 888 | print("CURRENT_PROTOCOL_VERSION OK\n"); |
| 888 | 889 | //get the 'what' code |
| 889 | - if read_Int32(stream) is | |
| 890 | + if read_Int32(io) is | |
| 890 | 891 | { |
| 891 | 892 | failure then failure, |
| 892 | 893 | success(what_code) then |
| 893 | 894 | print("what code = [" + to_ascii(what_code) + "] "+ what_code + "\n"); |
| 894 | 895 | //get the number of entries at this level |
| 895 | - if read_Int32(stream) is | |
| 896 | + if read_Int32(io) is | |
| 896 | 897 | { |
| 897 | 898 | failure then failure, |
| 898 | 899 | success(nb_entries) then |
| 899 | 900 | print("Nb Entries = " + nb_entries + "\n"); |
| 900 | 901 | //get all fields of this level |
| 901 | - if read_fields(stream, nb_entries) is | |
| 902 | + if read_fields(io, nb_entries) is | |
| 902 | 903 | { |
| 903 | 904 | failure then failure, |
| 904 | 905 | success(fields) then success(message(var(what_code), var(nb_entries), var(fields))) |
| ... | ... | @@ -1534,7 +1535,7 @@ define One |
| 1534 | 1535 | RWAddr(Int8) conn |
| 1535 | 1536 | ) = |
| 1536 | 1537 | |
| 1537 | - if unflatten_message(weaken(conn)) is | |
| 1538 | + if unflatten_message(make_data_io(weaken(conn))) is | |
| 1538 | 1539 | { |
| 1539 | 1540 | failure then |
| 1540 | 1541 | print("can't unflatten any message\n"); |
| ... | ... | @@ -1592,7 +1593,7 @@ global define One |
| 1592 | 1593 | { |
| 1593 | 1594 | failure then print("Open file Error\n"), |
| 1594 | 1595 | success(source) then |
| 1595 | - if unflatten_message(source) is | |
| 1596 | + if unflatten_message(make_data_io(source)) is | |
| 1596 | 1597 | { |
| 1597 | 1598 | failure then print("File Message ERROR \n"), |
| 1598 | 1599 | success(message) then | ... | ... |