function create_response($req) { $pkt = pack("N", PACKET_TYPE_RESPONSE); $method_tlv = packet_get_tlv($req, TLV_TYPE_METHOD); my_print("method is {$method_tlv['value']}"); packet_add_tlv($pkt, $method_tlv); $reqid_tlv = packet_get_tlv($req, TLV_TYPE_REQUEST_ID); packet_add_tlv($pkt, $reqid_tlv); if (is_callable($method_tlv['value'])) { $result = $method_tlv['value']($req, $pkt); } else { my_print("Got a request for something I don't know how to handle (" . $method_tlv['value'] . "), returning failure"); $result = ERROR_FAILURE; } packet_add_tlv($pkt, create_tlv(TLV_TYPE_RESULT, $result)); # Add the length to the beginning of the packet $pkt = pack("N", strlen($pkt) + 4) . $pkt; return $pkt; }
function channel_create_stdapi_net_udp_client($req, &$pkt) { my_print("creating udp client"); $peer_host_tlv = packet_get_tlv($req, TLV_TYPE_PEER_HOST); $peer_port_tlv = packet_get_tlv($req, TLV_TYPE_PEER_PORT); # We can't actually do anything with local_host and local_port because PHP # doesn't let us specify these values in any of the exposed socket API # functions. #$local_host_tlv = packet_get_tlv($req, TLV_TYPE_LOCAL_HOST); #$local_port_tlv = packet_get_tlv($req, TLV_TYPE_LOCAL_PORT); $sock = connect($peer_host_tlv['value'], $peer_port_tlv['value'], 'udp'); my_print("UDP channel on {$sock}"); if (!$sock) { return ERROR_CONNECTION_ERROR; } # # If we got here, the connection worked, respond with the new channel ID # $id = register_channel($sock); packet_add_tlv($pkt, create_tlv(TLV_TYPE_CHANNEL_ID, $id)); add_reader($sock); return ERROR_SUCCESS; }