source: trunk/instant_messenger/inc/Socket.abstract.php @ 55

Revision 55, 2.8 KB checked in by niltonneto, 17 years ago (diff)

Nova versão do Instant Messenger.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
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
18abstract class Socket
19{
20        private $log_file = false;
21       
22        protected final function open( $pSocket = false, $pTimeOut = 1, $pBlockingMode = 0 )
23        {
24      if ( !$pSocket )
25         return false;
26
27      if ( $socket = @stream_socket_client($pSocket, $errno, $errstr) )
28      {
29         stream_set_blocking($socket, $pBlockingMode);
30         stream_set_timeout($socket, $pTimeOut);
31      }
32      return $socket;
33        }
34
35   protected final function blocking($pSocket = false, $pBlockingMode = 1)
36   {
37      if ( !$pSocket || !is_resource($pSocket) )
38         return false;
39
40      stream_set_blocking($pSocket, $pBlockingMode);
41   }
42
43
44        protected function read( $pSocket = false, $pLength = 4096 )
45        {
46      ini_set( 'track_errors', '1' );
47      $php_errormsg = '';
48
49      if ( !$pSocket || !is_resource($pSocket) )
50         return false;
51
52      set_magic_quotes_runtime(0);
53      $return = @fread($pSocket, $pLength);
54      set_magic_quotes_runtime(get_magic_quotes_gpc());
55
56                if( $this->log_file )
57                {
58                        if ( !empty($return) && $fp = fopen ('read.log', "a+") )
59              {
60                 fwrite($fp, $return . "\n");
61                 fclose($fp);
62              }
63                }
64
65      if ( $php_errormsg )
66         return false;
67
68      return $return;
69        }
70
71        protected function write( $pSocket = false, $pData = false )
72        {
73      ini_set( 'track_errors', '1' );
74      $php_errormsg = '';
75     
76      if ( (!$pSocket || !is_resource($pSocket)) || !$pData )
77         return false;
78
79                if( $this->log_file)
80                {
81                        if ( $fp = fopen ('write.log', "a+") )
82              {
83                 fwrite($fp, $pData . "\n");
84                 fclose($fp);
85              }
86                }
87
88                $return = @fwrite($pSocket, $pData);
89     
90      if ( $php_errormsg )
91         return false;
92
93      return $return;
94        }
95
96        protected final function close( $pSocket = false )
97        {
98      if ( !$pSocket || !is_resource($pSocket) )
99         return false;
100
101                return fclose($pSocket);
102        }
103}
104?>
Note: See TracBrowser for help on using the repository browser.