00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef _GLASS_PROTOCOL_TCP_HH_
00022 #define _GLASS_PROTOCOL_TCP_HH_
00023
00024 #include <list>
00025
00026 #if defined(_MSC_VER)
00027 # include <winsock2.h>
00028 # include <time.h>
00029 #else
00030 # include <sys/time.h>
00031 # include <sys/select.h>
00032 # include <unistd.h>
00033 #endif
00034 #include <sys/types.h>
00035 #include "protocol.h"
00036 #include "packet.h"
00037
00038 namespace libglass {
00039
00040 class PacketLog;
00041
00042
00043 #if defined(_MSC_VER)
00044
00045 LIBGLASS_IMP_TEMPLATE_STL_LIST(Packet*);
00046 #endif // _MSC_VER
00047
00048 class LIBGLASS_API TCP : public Protocol {
00049 private:
00050 int readPacket(Packet &p, nodeFD sock);
00051 unsigned int writePacket(nodeFD fd, chain &m, unsigned int length);
00052
00053 std::list<Packet *> queue;
00054
00055 boost::mutex mutex;
00061 const struct in_addr gethostbyname_r(const char *host) const throw(Exception);
00062
00063 protected:
00065 #if defined(_MSC_VER) && (_MSC_VER < 1300) // earlier than .NET compiler (VC 7.0)
00066
00067
00068 enum{ default_port = 9191 };
00069 #else
00070 static const unsigned int default_port = 9191;
00071 #endif
00072
00074 union {
00075 struct {
00076 int sockfd;
00077 int fdmax;
00078 fd_set clients;
00079 struct timeval default_timeout;
00080 bool no_timeout;
00081 } server;
00082 struct {
00083 int sockfd;
00084 } client;
00085 } data;
00086
00087 PacketLog *plog;
00088
00089 int _listen(unsigned int port);
00090 nodeFD _accept(void);
00091 nodeFD _connect(const char *host, unsigned int port);
00092 void _disconnect(nodeFD node);
00093 bool _send(Packet &p);
00094 int _recv(Packet &p);
00095 int _safely_close_socket(nodeFD fd);
00096
00097 public:
00098 TCP();
00099 ~TCP();
00100
00101 nodeId getUniqueId(void);
00102 };
00103
00104 }
00105
00106 #endif // _GLASS_PROTOCOL_TCP_HH_