//****************************************************************************** // RCF - Remote Call Framework // Copyright (c) 2005 - 2007. All rights reserved. // Consult your license for conditions of use. // Developed by Jarl Lindrud. // Contact: jlindrud@hotmail.com . //****************************************************************************** #ifndef INCLUDE_RCF_TCPSERVERTRANSPORT_HPP #define INCLUDE_RCF_TCPSERVERTRANSPORT_HPP #include #include #include #include #include #include #include #include namespace RCF { class TcpServerTransport; typedef boost::shared_ptr TcpServerTransportPtr; class TcpServerTransport : public I_ServerTransport, public I_Service { public: TcpServerTransport(int port = 0); ServerTransportPtr clone() { return ServerTransportPtr(new TcpServerTransport(port)); } void setSessionManager(I_SessionManager &sessionManager); I_SessionManager & getSessionManager(); void setPort(int port); int getPort(); void setNetworkInterface(const std::string &networkInterface); std::string getNetworkInterface(); void setMaxPendingConnectionCount(unsigned int maxPendingConnectionCount); unsigned int getMaxPendingConnectionCount(); bool isClientIpAllowed(sockaddr_in &addr); private: typedef int Fd; class SessionState { public: SessionState(Fd fd); enum State { Ready, ReadingDataCount, ReadingData, WritingData, }; State state; std::vector readBuffer; std::size_t readBufferRemaining; std::vector writeBuffer; std::size_t writeBufferRemaining; Fd fd; }; typedef boost::shared_ptr SessionStatePtr; class TcpProactor : public I_Proactor { public: TcpProactor(TcpServerTransport &transport, SessionStatePtr sessionStatePtr); void postRead(); void postWrite(); void postClose(); std::vector & getWriteBuffer(); std::size_t getWriteOffset(); std::vector & getReadBuffer(); std::size_t getReadOffset(); I_ServerTransport & getServerTransport(); const I_RemoteAddress & getRemoteAddress(); void setTransportFilters(const std::vector &filters); const std::vector &getTransportFilters(); ByteBuffer getReadByteBuffer() { return ByteBuffer(); } void postWrite(const std::vector &byteBuffers) { RCF_UNUSED_VARIABLE(byteBuffers);} private: TcpServerTransport & transport; SessionStatePtr sessionStatePtr; }; friend class TcpServerTransport::TcpProactor; private: void open(); void close(); Fd hash(Fd fd); void createSession(Fd fd); int readSession(Fd fd); int writeSession(Fd fd); bool closeSession(Fd fd); void cycleRead(int timeoutMs); void cycleWrite(); void cycleClose(); void cycleAccept(); void cycle(int timeoutMs, const volatile bool &stopFlag); void postClose(SessionStatePtr sessionStatePtr); void postWrite(SessionStatePtr sessionStatePtr); void postRead(SessionStatePtr sessionStatePtr); bool cycleTransportAndServer(RcfServer &server, int timeoutMs, const volatile bool &stopFlag); void onServiceAdded(RcfServer &server); void onServiceRemoved(RcfServer &server); void onServerOpen(RcfServer &server); void onServerClose(RcfServer &server); void onServerStart(RcfServer &server); void onServerStop(RcfServer &server); SessionState & getSessionState(Fd fd); SessionStatePtr getSessionStatePtr(Fd fd); I_Session & getSession(Fd fd); SessionPtr getSessionPtr(Fd fd); private: typedef int PortNumber; typedef int FdHash; const unsigned int FdPartitionCount; typedef std::map > SessionMap; typedef std::pair, SessionMap > SynchronizedSessionMap; typedef std::vector SessionMaps; SessionMaps sessionMaps; I_SessionManager * pSessionManager; std::string acceptorInterface; PortNumber acceptorPort; Fd acceptorFd; volatile bool mStopFlag; Mutex fdsReadyMutex; std::vector fdsReady; Mutex fdsToBeReadMutex; std::vector fdsToBeRead; Mutex fdsToBeWrittenMutex; std::vector fdsToBeWritten; Mutex fdsToBeClosedMutex; std::vector fdsToBeClosed; Mutex selectingMutex; bool selecting; ReadWriteMutex allowedIpsMutex; std::vector allowedIps; std::vector allowedIpAddrs; int port; std::string networkInterface; unsigned int maxPendingConnectionCount; // temp vectors, eventually supposed to be thread-specific std::vector fdsTemp1; std::vector fdsTemp2; std::vector fdsTemp3; }; } // namespace RCF #endif // ! INCLUDE_RCF_TCPSERVERTRANSPORT_HPP