00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef __UT_NetStream__
00028 #define __UT_NetStream__
00029
00030 #include "UT_API.h"
00031 #include "UT_Lock.h"
00032 #include "UT_String.h"
00033 #include "UT_NTStreamUtil.h"
00034
00035 class UT_String;
00036 class UT_Thread;
00037 class UT_NetSocket;
00038 class UT_NetPacket;
00039
00040 #define NET_STREAM_IN 0
00041 #define NET_STREAM_OUT 1
00042
00043 #define NET_STREAM_SOCKET 0
00044 #define NET_STREAM_PLUG 1
00045
00046 class UT_API UT_NetStream
00047 {
00048 public:
00049 UT_NetStream();
00050 virtual ~UT_NetStream();
00051
00052
00053
00054 int startIOThread();
00055 int stopIOThread();
00056
00057
00058 void openConnection(const char *address, int port,
00059 int mode = NET_STREAM_IN,
00060 int connect_type = NET_STREAM_PLUG);
00061 void closeConnection();
00062
00063
00064 int isThreadStarted() const { return myIOThread ? 1 : 0; }
00065
00066
00067 int isValidStream() const { return myValidAddress; }
00068
00069
00070 int isConnected() const { return myIsConnected; }
00071
00072
00073
00074 void setDataReady();
00075
00076
00077
00078 int isDataReady();
00079
00080 protected:
00081
00082
00083 virtual void receiveData(istream &is) = 0;
00084 virtual void transmitData(ostream &os) = 0;
00085
00086
00087 virtual void justConnected() { ; }
00088 virtual void dataReceived() { ; }
00089
00090 static void *ioLoopEntry(void *data);
00091 void ioLoop();
00092
00093 private:
00094
00095 void breakConnection();
00096
00097 UT_NetSocket *myServer;
00098 UT_NetSocket *myConnection;
00099 UT_NetPacket *myPacket;
00100 UT_Thread *myIOThread;
00101
00102
00103 UT_String myAddress;
00104 int myPort;
00105 int myMode;
00106 int myConnectType;
00107
00108
00109 int myIsConnected;
00110 int myValidAddress;
00111 UT_Lock myThreadLock;
00112
00113
00114 int myDataReadyFlag;
00115 int mySocketInfoSetFlag;
00116 int myCloseConnectionFlag;
00117 int myThreadStopFlag;
00118
00119 UT_OStrStream *myOStream;
00120 istrstream *myIStream;
00121
00122 UT_NetStream *myThis;
00123 };
00124
00125 #endif