mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 17:02:45 +00:00 
			
		
		
		
	CSocket: Don't create the read notifier until after we've connected
This makes it so that "on_connected" always gets called first. Since accepted sockets are connected before construction, they have to manually set CSocket::m_connected.
This commit is contained in:
		
							parent
							
								
									65409e8f04
								
							
						
					
					
						commit
						a1907011b2
					
				
					 4 changed files with 19 additions and 1 deletions
				
			
		|  | @ -5,6 +5,8 @@ | ||||||
| CLocalSocket::CLocalSocket(int fd, CObject* parent) | CLocalSocket::CLocalSocket(int fd, CObject* parent) | ||||||
|     : CSocket(CSocket::Type::Local, parent) |     : CSocket(CSocket::Type::Local, parent) | ||||||
| { | { | ||||||
|  |     // NOTE: This constructor is used by CLocalServer::accept(), so the socket is already connected.
 | ||||||
|  |     m_connected = true; | ||||||
|     set_fd(fd); |     set_fd(fd); | ||||||
|     set_mode(CIODevice::ReadWrite); |     set_mode(CIODevice::ReadWrite); | ||||||
|     set_error(0); |     set_error(0); | ||||||
|  |  | ||||||
|  | @ -89,6 +89,7 @@ bool CSocket::common_connect(const struct sockaddr* addr, socklen_t addrlen) | ||||||
|             m_notifier->on_ready_to_write = [this] { |             m_notifier->on_ready_to_write = [this] { | ||||||
|                 dbg() << *this << " connected!"; |                 dbg() << *this << " connected!"; | ||||||
|                 m_connected = true; |                 m_connected = true; | ||||||
|  |                 ensure_read_notifier(); | ||||||
|                 m_notifier->set_event_mask(CNotifier::Event::None); |                 m_notifier->set_event_mask(CNotifier::Event::None); | ||||||
|                 if (on_connected) |                 if (on_connected) | ||||||
|                     on_connected(); |                     on_connected(); | ||||||
|  | @ -100,6 +101,7 @@ bool CSocket::common_connect(const struct sockaddr* addr, socklen_t addrlen) | ||||||
|     } |     } | ||||||
|     dbg() << *this << " connected ok!"; |     dbg() << *this << " connected ok!"; | ||||||
|     m_connected = true; |     m_connected = true; | ||||||
|  |     ensure_read_notifier(); | ||||||
|     if (on_connected) |     if (on_connected) | ||||||
|         on_connected(); |         on_connected(); | ||||||
|     return true; |     return true; | ||||||
|  | @ -132,7 +134,18 @@ void CSocket::did_update_fd(int fd) | ||||||
|         m_read_notifier = nullptr; |         m_read_notifier = nullptr; | ||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
|     m_read_notifier = CNotifier::construct(fd, CNotifier::Event::Read, this); |     if (m_connected) { | ||||||
|  |         ensure_read_notifier(); | ||||||
|  |     } else { | ||||||
|  |         // I don't think it would be right if we updated the fd while not connected *but* while having a notifier..
 | ||||||
|  |         ASSERT(!m_read_notifier); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void CSocket::ensure_read_notifier() | ||||||
|  | { | ||||||
|  |     ASSERT(m_connected); | ||||||
|  |     m_read_notifier = CNotifier::construct(fd(), CNotifier::Event::Read, this); | ||||||
|     m_read_notifier->on_ready_to_read = [this] { |     m_read_notifier->on_ready_to_read = [this] { | ||||||
|         if (on_ready_to_read) |         if (on_ready_to_read) | ||||||
|             on_ready_to_read(); |             on_ready_to_read(); | ||||||
|  |  | ||||||
|  | @ -51,6 +51,7 @@ protected: | ||||||
| private: | private: | ||||||
|     virtual bool open(CIODevice::OpenMode) override { ASSERT_NOT_REACHED(); } |     virtual bool open(CIODevice::OpenMode) override { ASSERT_NOT_REACHED(); } | ||||||
|     bool common_connect(const struct sockaddr*, socklen_t); |     bool common_connect(const struct sockaddr*, socklen_t); | ||||||
|  |     void ensure_read_notifier(); | ||||||
| 
 | 
 | ||||||
|     Type m_type { Type::Invalid }; |     Type m_type { Type::Invalid }; | ||||||
|     RefPtr<CNotifier> m_notifier; |     RefPtr<CNotifier> m_notifier; | ||||||
|  |  | ||||||
|  | @ -5,6 +5,8 @@ | ||||||
| CTCPSocket::CTCPSocket(int fd, CObject* parent) | CTCPSocket::CTCPSocket(int fd, CObject* parent) | ||||||
|     : CSocket(CSocket::Type::TCP, parent) |     : CSocket(CSocket::Type::TCP, parent) | ||||||
| { | { | ||||||
|  |     // NOTE: This constructor is used by CTCPServer::accept(), so the socket is already connected.
 | ||||||
|  |     m_connected = true; | ||||||
|     set_fd(fd); |     set_fd(fd); | ||||||
|     set_mode(CIODevice::ReadWrite); |     set_mode(CIODevice::ReadWrite); | ||||||
|     set_error(0); |     set_error(0); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling