00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef NEWNET_FACTORYSOCKET_H
00021 #define NEWNET_FACTORYSOCKET_H
00022
00023 #include "nnreactor.h"
00024
00025 namespace NewNet
00026 {
00028
00032 template<class ServerType, class ClientType>
00033 class FactorySocket : public Object
00034 {
00035 public:
00037
00040 FactorySocket()
00041 {
00042 m_ServerSocket = new ServerType;
00043 m_ServerSocket->acceptedEvent.connect(this, &FactorySocket::buildClient);
00044 }
00045
00047
00049 FactorySocket(ServerType * serverSocket)
00050 {
00051 m_ServerSocket = serverSocket;
00052 m_ServerSocket->acceptedEvent.connect(this, &FactorySocket::buildClient);
00053 }
00054
00056
00058 ServerType * serverSocket()
00059 {
00060 return m_ServerSocket;
00061 }
00062
00064
00067 Event<ClientType *> clientAcceptedEvent;
00068
00069 private:
00070 void buildClient(int descriptor)
00071 {
00072 ClientType * client = new ClientType();
00073 client->setDescriptor(descriptor);
00074 client->setSocketState(Socket::SocketConnected);
00075 if(m_ServerSocket->reactor())
00076 m_ServerSocket->reactor()->add(client);
00077 clientAcceptedEvent(client);
00078 }
00079
00080 NewNet::RefPtr<ServerType> m_ServerSocket;
00081 };
00082 }
00083
00084 #endif // NEWNET_FACTORYSOCKET_H