|
| 1 | +#include "mrubyc.h" |
| 2 | + |
| 3 | +static void |
| 4 | +c_net_dns_resolve(mrbc_vm *vm, mrbc_value *v, int argc) |
| 5 | +{ |
| 6 | + if (argc != 1) { |
| 7 | + mrbc_raise(vm, MRBC_CLASS(ArgumentError), "wrong number of arguments"); |
| 8 | + return; |
| 9 | + } |
| 10 | + mrbc_value input = GET_ARG(1); |
| 11 | + if (input.tt != MRBC_TT_STRING) { |
| 12 | + mrbc_raise(vm, MRBC_CLASS(TypeError), "wrong type of argument"); |
| 13 | + return; |
| 14 | + } |
| 15 | + |
| 16 | + mrbc_value ret = DNS_resolve(vm, (const char *)GET_STRING_ARG(1), (GET_ARG(2).tt == MRBC_TT_TRUE)); |
| 17 | + SET_RETURN(ret); |
| 18 | +} |
| 19 | + |
| 20 | +static void |
| 21 | +c_net_tcpclient__request_impl(mrbc_vm *vm, mrbc_value *v, int argc) |
| 22 | +{ |
| 23 | + if (argc != 4) { |
| 24 | + mrbc_raise(vm, MRBC_CLASS(ArgumentError), "wrong number of arguments"); |
| 25 | + return; |
| 26 | + } |
| 27 | + mrbc_value host = GET_ARG(1); |
| 28 | + if (host.tt != MRBC_TT_STRING) { |
| 29 | + mrbc_raise(vm, MRBC_CLASS(TypeError), "wrong type of argument"); |
| 30 | + return; |
| 31 | + } |
| 32 | + mrbc_value port = GET_ARG(2); |
| 33 | + if (port.tt != MRBC_TT_INTEGER) { |
| 34 | + mrbc_raise(vm, MRBC_CLASS(TypeError), "wrong type of argument"); |
| 35 | + return; |
| 36 | + } |
| 37 | + mrbc_value input = GET_ARG(3); |
| 38 | + if (input.tt != MRBC_TT_STRING) { |
| 39 | + mrbc_raise(vm, MRBC_CLASS(TypeError), "wrong type of argument"); |
| 40 | + return; |
| 41 | + } |
| 42 | + mrbc_value is_tls = GET_ARG(4); |
| 43 | + if (!((is_tls.tt == MRBC_TT_TRUE) || (is_tls.tt == MRBC_TT_FALSE))) { |
| 44 | + mrbc_raise(vm, MRBC_CLASS(TypeError), "wrong type of argument"); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + mrbc_value ret = TCPClient_send((const char *)GET_STRING_ARG(1), port.i, vm, &input, (is_tls.tt == MRBC_TT_TRUE)); |
| 49 | + SET_RETURN(ret); |
| 50 | +} |
| 51 | + |
| 52 | +static void |
| 53 | +c_net_udpclient__send_impl(mrbc_vm *vm, mrbc_value *v, int argc) |
| 54 | +{ |
| 55 | + if (argc != 4) { |
| 56 | + mrbc_raise(vm, MRBC_CLASS(ArgumentError), "wrong number of arguments"); |
| 57 | + return; |
| 58 | + } |
| 59 | + mrbc_value host = GET_ARG(1); |
| 60 | + if (host.tt != MRBC_TT_STRING) { |
| 61 | + mrbc_raise(vm, MRBC_CLASS(TypeError), "wrong type of argument for host"); |
| 62 | + return; |
| 63 | + } |
| 64 | + mrbc_value port = GET_ARG(2); |
| 65 | + if (port.tt != MRBC_TT_INTEGER) { |
| 66 | + mrbc_raise(vm, MRBC_CLASS(TypeError), "wrong type of argument for port"); |
| 67 | + return; |
| 68 | + } |
| 69 | + mrbc_value data = GET_ARG(3); |
| 70 | + if (data.tt != MRBC_TT_STRING) { |
| 71 | + mrbc_raise(vm, MRBC_CLASS(TypeError), "wrong type of argument for data"); |
| 72 | + return; |
| 73 | + } |
| 74 | + mrbc_value use_dtls = GET_ARG(4); |
| 75 | + if (!((use_dtls.tt == MRBC_TT_TRUE) || (use_dtls.tt == MRBC_TT_FALSE))) { |
| 76 | + mrbc_raise(vm, MRBC_CLASS(TypeError), "wrong type of argument for use_dtls"); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + mrbc_value ret = UDPClient_send((const char *)GET_STRING_ARG(1), port.i, vm, &data, (use_dtls.tt == MRBC_TT_TRUE)); |
| 81 | + SET_RETURN(ret); |
| 82 | +} |
| 83 | + |
| 84 | +void |
| 85 | +mrbc_net_init(mrbc_vm *vm) |
| 86 | +{ |
| 87 | + mrbc_class *class_Net = mrbc_define_class(vm, "Net", mrbc_class_object); |
| 88 | + |
| 89 | + mrbc_class *class_Net_DNS = mrbc_define_class_under(vm, class_Net, "DNS", mrbc_class_object); |
| 90 | + mrbc_define_method(vm, class_Net_DNS, "resolve", c_net_dns_resolve); |
| 91 | + |
| 92 | + mrbc_class *class_Net_TCPClient = mrbc_define_class_under(vm, class_Net, "TCPClient", mrbc_class_object); |
| 93 | + mrbc_define_method(vm, class_Net_TCPClient, "_request_impl", c_net_tcpclient__request_impl); |
| 94 | + |
| 95 | + mrbc_class *class_Net_UDPClient = mrbc_define_class_under(vm, class_Net, "UDPClient", mrbc_class_object); |
| 96 | + mrbc_define_method(vm, class_Net_UDPClient, "_send_impl", c_net_udpclient__send_impl); |
| 97 | +} |
0 commit comments