1 /****************************************************************************
2  *
3  *  Copyright (C) 2000-2001 RealNetworks, Inc. All rights reserved.
4  *
5  *  This program is free software.  It may be distributed under the terms
6  *  in the file LICENSE, found in the top level of the source distribution.
7  *
8  */
9 
10 #ifndef _STREAM_H
11 #define _STREAM_H
12 
13 #include "types.h"
14 #include "str.h"
15 #include "tlist.h"
16 
17 #ifdef _UNIX
18     // some custom bits (we rely on pollfd events being a 16-bit type)
19     // note that these are combined with POLLIN/POLLOUT
20     #define XPOLLACC    0x00010000  /* accept */
21     #define XPOLLCNX    0x00020000  /* connect */
22 
23     #include <sys/poll.h>
24     typedef pollfd              waitobj_t;
25     typedef INT32               waitevents_t;
26    #define WAITOBJ_MAX          0xffff
27 
28     #define WAITOBJ_IS_VALID(obj)       (obj.fd != -1)
29     #define WAIT_EVENT_ACCEPT(wevt)     (wevt & XPOLLACC)
30     #define WAIT_EVENT_CONNECT(wevt)    (wevt & XPOLLCNX)
31     #define WAIT_EVENT_READ(wevt)       (wevt & POLLIN)
32     #define WAIT_EVENT_WRITE(wevt)      (wevt & POLLOUT)
33     #define WAIT_EVENT_EXCEPT(wevt)     (wevt & POLLPRI)
34 #endif
35 #ifdef _WIN32
36     typedef HANDLE              waitobj_t;
37     typedef WSANETWORKEVENTS    waitevents_t;
38     #define WAITOBJ_MAX         MAXIMUM_WAIT_OBJECTS
39 
40     #define WAITOBJ_IS_VALID(obj)       (obj != NULL)
41     #define WAIT_EVENT_ACCEPT(wevt)     (wevt.lNetworkEvents & FD_ACCEPT)
42     #define WAIT_EVENT_CONNECT(wevt)    (wevt.lNetworkEvents & FD_CONNECT)
43     #define WAIT_EVENT_READ(wevt)       (wevt.lNetworkEvents & FD_READ)
44     #define WAIT_EVENT_WRITE(wevt)      (wevt.lNetworkEvents & FD_WRITE)
45     #define WAIT_EVENT_EXCEPT(wevt)     (wevt.lNetworkEvents & FD_OOB)
46 #endif
47 
48 class CStreamResponse
49 {
50 public:
51     virtual void OnConnectDone( int err ) = 0;
52     virtual void OnReadReady  ( void ) = 0;
53     virtual void OnWriteReady ( void ) = 0;
54     virtual void OnExceptReady( void ) = 0;
55     virtual void OnClosed     ( void ) = 0;
56 };
57 
58 class CStream
59 {
60 //    friend class CEventThread;
61 
62 private: // Unimplemented
63     CStream( const CStream& );
64     CStream& operator=( const CStream& );
65 
66 public:
67     CStream( void );
68     CStream( CStreamResponse* pResponse );
69     virtual ~CStream( void );
70 
71     virtual bool    IsOpen( void ) = 0;
72     virtual void    Close( void ) = 0;
73     virtual size_t  Read( PVOID pbuf, size_t nLen ) = 0;
74     virtual size_t  Write( CPVOID pbuf, size_t nLen ) = 0;
75 
76     void    SetResponse( CStreamResponse* pResponse );
77     bool    Read( CBuffer* pbuf );
78     bool    Write( const CBuffer& buf );
79 
80 protected:
GetResponse(void)81     inline CStreamResponse* GetResponse( void ) { return m_pResponse; }
82 
83 protected:
84     CStreamResponse* m_pResponse;
85 };
86 typedef TDoubleList<CStream*> CStreamList;
87 
88 #endif //ndef _STREAM_H
89