Exemple #1
0
 /**
  * Sends a magic packet to the specified IP/MAC address using the specified
  * address protocol.
  * @param string $ip The broadcast IP address to use
  * @param string $mac The MAC address of the device to wake
  * @param int $protocol The type of IP to use. Deafult AF_INET
  * @return int|bool The result of sending the packet
  */
 public static function wake($ip, $mac, $protocol = AF_INET)
 {
     if (empty($ip) || empty($mac)) {
         throw new \Exception('Broadcast IP address and MAC address required.');
     }
     if (!in_array($protocol, array(AF_INET, AF_INET6, AF_UNIX))) {
         throw new \Exception('Invalid protocol.');
     }
     $socket = socket_create($protocol, SOCK_DGRAM, SOL_UDP);
     if ($socket && is_resource($socket)) {
         if (!socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0))) {
             throw new \Exception('Unable to set options on socket: ' . socket_strerror(socket_last_error()) . PHP_EOL);
         }
         $packet = SELF::generateMagicPacket();
         $result = socket_sendto($socket, $packet, strlen($packet), 0, $ip, 7);
         socket_close($socket);
         return $result;
     } else {
         throw new \Exception('Connection failed: ' . socket_strerror(socket_last_error()) . PHP_EOL);
     }
 }