HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_NetStream.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_NetPacket.h ( UT Library, C++)
7  *
8  * COMMENTS:
9  * This class is a special network stream that asynchronously reads data
10  * into houdini. It is also responsible for listening for connections or
11  * attempting to connect to sockets. Endian issues are handled.
12  * The I/O is handled in a separate thread.
13  *
14  * CAVEATS: - This is built partially upon UT_NetPacket.
15  * - This is an abstract base class - you must define the
16  * processData method.
17  */
18 
19 #ifndef __UT_NetStream__
20 #define __UT_NetStream__
21 
22 #include "UT_API.h"
23 #include "UT_Lock.h"
24 #include "UT_String.h"
25 
26 #include <sstream>
27 #include <iosfwd>
28 
29 class UT_String;
30 class UT_Thread;
31 class UT_NetSocket;
32 class UT_NetPacket;
33 
34 #define NET_STREAM_IN 0
35 #define NET_STREAM_OUT 1
36 
37 #define NET_STREAM_SOCKET 0
38 #define NET_STREAM_PLUG 1
39 
41 {
42 public:
43  UT_NetStream();
44  virtual ~UT_NetStream();
45 
46  // call first before attempting to open a connection. Returns 0 if already
47  // open (or for stop, if it's not open).
48  int startIOThread();
49  int stopIOThread();
50 
51  // call to open and close client/server connections.
52  void openConnection(const char *address, int port,
53  int mode = NET_STREAM_IN,
54  int connect_type = NET_STREAM_PLUG);
55  void closeConnection();
56 
57  // returns 1 if startIOThread successfully started a thread.
58  int isThreadStarted() const { return myIOThread ? 1 : 0; }
59 
60  // returns 1 if the socket is valid.
61  int isValidStream() const { return myValidAddress; }
62 
63  // returns 1 if a connection has been established.
64  int isConnected() const { return myIsConnected; }
65 
66  // when writing, this is used to tell the thread that data is ready to
67  // be sent. The thread will then call transmitData().
68  void setDataReady();
69 
70  // when ready, check this flag to tell you if data is ready (if so, it
71  // will clear the flag).
72  int isDataReady();
73 
74 protected:
75  // Fill these methods in to send data, or to receive a packet of data. Both
76  // methods operate inside the thread.
77  virtual void receiveData(std::istream &is) = 0;
78  virtual void transmitData(std::ostream &os) = 0;
79 
80  // called when a connection is made.
81  virtual void justConnected() { ; }
82  virtual void dataReceived() { ; }
83 
84  static void *ioLoopEntry(void *data);
85  void ioLoop();
86 
87 private:
88 
89  void breakConnection();
90 
91  UT_NetSocket *myServer;
92  UT_NetSocket *myConnection;
93  UT_NetPacket *myPacket;
94  UT_Thread *myIOThread;
95 
96  // address to create socket at or connect to.
97  UT_String myAddress;
98  int myPort;
99  int myMode;
100  int myConnectType;
101 
102  // query flags.
103  int myIsConnected;
104  int myValidAddress;
105  UT_Lock myThreadLock;
106 
107  // flags to control the thread.
108  int myDataReadyFlag;
109  int mySocketInfoSetFlag;
110  int myCloseConnectionFlag;
111  int myThreadStopFlag;
112 
113  UT_NetStream *myThis;
114 };
115 
116 #endif
int isThreadStarted() const
Definition: UT_NetStream.h:58
#define UT_API
Definition: UT_API.h:14
GLenum mode
Definition: glcorearb.h:99
int isConnected() const
Definition: UT_NetStream.h:64
virtual void justConnected()
Definition: UT_NetStream.h:81
#define NET_STREAM_PLUG
Definition: UT_NetStream.h:38
int isValidStream() const
Definition: UT_NetStream.h:61
virtual void dataReceived()
Definition: UT_NetStream.h:82
#define NET_STREAM_IN
Definition: UT_NetStream.h:34
Definition: format.h:895