[903] | 1 | <?php |
---|
| 2 | /* |
---|
| 3 | +-------------------------------------------------------------------------+ |
---|
| 4 | | | |
---|
| 5 | | @FILE_NAME: Socket.abstract.php | |
---|
| 6 | | @CLASS_NAME: (abstract) Jabber | |
---|
| 7 | | | |
---|
| 8 | | @DATE: Wed May,16 2007 07:58:59 | |
---|
| 9 | | @LAST_CHANGE: Wed May,16 2007 08:03:28 | |
---|
| 10 | | | |
---|
| 11 | | @AUTHOR: [NUTS] Rodrigo Souza - rodsouza@celepar.pr.gov.br | |
---|
| 12 | | | |
---|
| 13 | | @BRIEF: Abstract class for connect with a socket | |
---|
| 14 | | | |
---|
| 15 | +-------------------------------------------------------------------------+ |
---|
| 16 | */ |
---|
| 17 | |
---|
| 18 | abstract class Socket |
---|
| 19 | { |
---|
| 20 | protected final function open( $pSocket = false, $pTimeOut = 1, $pBlockingMode = 0 ) |
---|
| 21 | { |
---|
| 22 | if ( !$pSocket ) |
---|
| 23 | return false; |
---|
| 24 | |
---|
| 25 | if ( $socket = @stream_socket_client($pSocket, $errno, $errstr) ) |
---|
| 26 | { |
---|
| 27 | stream_set_blocking($socket, $pBlockingMode); |
---|
| 28 | stream_set_timeout($socket, $pTimeOut); |
---|
| 29 | } |
---|
| 30 | return $socket; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | protected final function blocking($pSocket = false, $pBlockingMode = 1) |
---|
| 34 | { |
---|
| 35 | if ( !$pSocket || !is_resource($pSocket) ) |
---|
| 36 | return false; |
---|
| 37 | |
---|
| 38 | stream_set_blocking($pSocket, $pBlockingMode); |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | protected function read( $pSocket = false, $pLength = 4096 ) |
---|
| 43 | { |
---|
| 44 | ini_set( 'track_errors', '1' ); |
---|
| 45 | $php_errormsg = ''; |
---|
| 46 | |
---|
| 47 | if ( !$pSocket || !is_resource($pSocket) ) |
---|
| 48 | return false; |
---|
| 49 | |
---|
| 50 | set_magic_quotes_runtime(0); |
---|
| 51 | $return = @fread($pSocket, $pLength); |
---|
| 52 | set_magic_quotes_runtime(get_magic_quotes_gpc()); |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | if ( $php_errormsg ) |
---|
| 56 | return false; |
---|
| 57 | |
---|
| 58 | return $return; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | protected function write( $pSocket = false, $pData = false ) |
---|
| 62 | { |
---|
| 63 | ini_set( 'track_errors', '1' ); |
---|
| 64 | $php_errormsg = ''; |
---|
| 65 | |
---|
| 66 | if ( (!$pSocket || !is_resource($pSocket)) || !$pData ) |
---|
| 67 | return false; |
---|
| 68 | |
---|
| 69 | $return = @fwrite($pSocket, $pData); |
---|
| 70 | |
---|
| 71 | if ( $php_errormsg ) |
---|
| 72 | return false; |
---|
| 73 | |
---|
| 74 | return $return; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | protected final function close( $pSocket = false ) |
---|
| 78 | { |
---|
| 79 | if ( !$pSocket || !is_resource($pSocket) ) |
---|
| 80 | return false; |
---|
| 81 | |
---|
| 82 | return fclose($pSocket); |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | ?> |
---|