Changeset 1206

Show
Ignore:
Timestamp:
01/09/08 09:40:39 (1 year ago)
Author:
mikey
Message:

added setOption()/getOption()
allowed 0 as valid protocol value

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/util/net/stubBSDSocket.php

    r1205 r1206  
    4545    protected $protocol       = SOL_TCP; 
    4646    /** 
     47     * list of options for the socket 
     48     * 
     49     * @var  array<int,array<int,mixed>> 
     50     */ 
     51    protected $options        = array(); 
     52    /** 
    4753     * list of available domains 
    4854     * 
     
    134140    public function setProtocol($protocol) 
    135141    { 
    136         if (in_array($protocol, array(SOL_TCP, SOL_UDP)) === false) { 
     142        if (0 !== $protocol && in_array($protocol, array(SOL_TCP, SOL_UDP)) === false) { 
    137143            throw new stubIllegalArgumentException('Protocol must be one of SOL_TCP or SOL_UDP.'); 
    138144        } 
     
    153159    { 
    154160        return $this->protocol; 
     161    } 
     162 
     163    /** 
     164     * sets an option 
     165     * 
     166     * @param   int    $level  protocol level of option 
     167     * @param   int    $name   option name 
     168     * @param   mixed  $value  option value 
     169     * @throws  stubConnectionException 
     170     */ 
     171    public function setOption($level, $name, $value) 
     172    { 
     173        if (isset($this->options[$level]) === false) { 
     174            $this->options[$level] = array(); 
     175        } 
     176         
     177        $this->options[$level][$name] = $value; 
     178        if ($this->isConnected() === true) { 
     179            if (socket_set_option($this->fp, $level, $name, $value) === false) { 
     180                throw new stubConnectionException('Failed to set option ' . $name . ' on level ' . $level . ' to value ' . $value); 
     181            } 
     182        } 
     183    } 
     184 
     185    /** 
     186     * returns an option 
     187     * 
     188     * @param   int    $level  protocol level of option 
     189     * @param   int    $name   option name 
     190     * @return  mixed 
     191     * @throws  stubConnectionException 
     192     */ 
     193    public function getOption($level, $name) 
     194    { 
     195        if ($this->isConnected() === true) { 
     196            $option = socket_get_option($this->fp, $level, $name); 
     197            if (false === $option) { 
     198                throw new stubConnectionException('Failed to retrieve option ' . $name . ' on level ' . $level); 
     199            } 
     200             
     201            if (isset($this->options[$level]) === false) { 
     202                $this->options[$level] = array(); 
     203            } 
     204             
     205            $this->options[$level][$name] = $option; 
     206        } 
     207         
     208        if (isset($this->options[$level]) === true && isset($this->options[$level][$name]) === true) { 
     209            return $this->options[$level][$name]; 
     210        } 
     211         
     212        return null; 
    155213    } 
    156214 
     
    180238        } 
    181239         
     240        foreach ($this->options as $level => $pairs) { 
     241            foreach ($pairs as $name => $value) { 
     242                socket_set_option($this->fp, $level, $name, $value); 
     243            } 
     244        } 
     245       
    182246        switch ($this->domain) { 
    183247            case AF_INET: