Panda3D

socket_tcp_listen.h

00001 #ifndef __SOCKET_TCP_LISTEN_H__
00002 #define __SOCKET_TCP_LISTEN_H__
00003 
00004 #include "pandabase.h"
00005 #include "socket_ip.h"
00006 
00007 /////////////////////////////////////////////////////////////////////
00008 // Class : Socket_TCP_Listen
00009 // Description : Base functionality for a TCP rendezvous socket
00010 /////////////////////////////////////////////////////////////////////
00011 class EXPCL_PANDA_NATIVENET Socket_TCP_Listen : public Socket_IP
00012 {
00013 public:
00014 PUBLISHED:
00015     Socket_TCP_Listen() {};
00016     ~Socket_TCP_Listen() {};
00017     inline bool OpenForListen(const Socket_Address & Inaddess, int backlog_size = 1024);
00018     inline bool GetIncomingConnection(Socket_TCP & newsession, Socket_Address &address);    
00019 public:
00020     inline bool GetIncomingConnection(SOCKET & newsession, Socket_Address &address);
00021   
00022 public:
00023   static TypeHandle get_class_type() {
00024     return _type_handle;
00025   }
00026   static void init_type() {
00027     Socket_IP::init_type();
00028     register_type(_type_handle, "Socket_TCP_Listen",
00029                   Socket_IP::get_class_type());
00030   }
00031   virtual TypeHandle get_type() const {
00032     return get_class_type();
00033   }
00034   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00035 
00036 private:
00037   static TypeHandle _type_handle;
00038 };
00039 
00040 ////////////////////////////////////////////////////////////////////
00041 // Function name : OpenForListen
00042 // Description   : This function will initialize a listening Socket
00043 ////////////////////////////////////////////////////////////////////
00044 inline bool Socket_TCP_Listen::OpenForListen(const Socket_Address & Inaddess, int backlog_size )
00045 {
00046     ErrorClose();
00047     _socket = DO_NEWTCP();
00048     
00049     SetReuseAddress();
00050     
00051     if (DO_BIND(_socket, &Inaddess.GetAddressInfo()) != 0) {
00052       return ErrorClose();
00053     }
00054     
00055     if (DO_LISTEN(_socket, backlog_size) != 0) {
00056       return ErrorClose();
00057     }
00058 
00059     return true;
00060 }
00061 ////////////////////////////////////////////////////////////////////
00062 // Function name : GetIncomingConnection
00063 // Description   : This function is used to accept new connections
00064 ////////////////////////////////////////////////////////////////////
00065 inline bool Socket_TCP_Listen::GetIncomingConnection(SOCKET & newsession, Socket_Address &address)
00066 {
00067     newsession = DO_ACCEPT(_socket, &address.GetAddressInfo());
00068     if (newsession == BAD_SOCKET)
00069         return false;
00070     return true;
00071 }
00072 
00073 
00074 inline bool Socket_TCP_Listen::GetIncomingConnection(Socket_TCP & newsession, Socket_Address &address)
00075 {
00076     SOCKET sck;
00077     if(!GetIncomingConnection(sck,address))
00078         return false;
00079 
00080     newsession.SetSocket(sck);
00081     return true;
00082 }
00083 
00084 #endif //__SOCKET_TCP_LISTEN_H__
 All Classes Functions Variables Enumerations