mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 17:52:45 +00:00 
			
		
		
		
	LibCore: Add a way to mark a socket as blocking (or not)
If custom I/O is being done outside CIODevice, we need a way to force blocking sometimes. This also fixes the default of CLocalSocket to be non-blocking, the same as CTCPSocket.
This commit is contained in:
		
							parent
							
								
									6aa77d1999
								
							
						
					
					
						commit
						a714fc661d
					
				
					 3 changed files with 15 additions and 1 deletions
				
			
		|  | @ -4,7 +4,7 @@ | |||
| CLocalSocket::CLocalSocket(CObject* parent) | ||||
|     : CSocket(CSocket::Type::Local, parent) | ||||
| { | ||||
|     int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0); | ||||
|     int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0); | ||||
|     if (fd < 0) { | ||||
|         set_error(fd); | ||||
|     } else { | ||||
|  |  | |||
|  | @ -6,6 +6,8 @@ | |||
| #include <netinet/in.h> | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <unistd.h> | ||||
| #include <fcntl.h> | ||||
| #include <sys/socket.h> | ||||
| 
 | ||||
| CSocket::CSocket(Type type, CObject* parent) | ||||
|  | @ -31,6 +33,17 @@ bool CSocket::connect(const String& hostname, int port) | |||
|     return connect(host_address, port); | ||||
| } | ||||
| 
 | ||||
| void CSocket::set_blocking(bool blocking) | ||||
| { | ||||
|     int flags = fcntl(fd(), F_GETFL, 0); | ||||
|     ASSERT(flags >= 0); | ||||
|     if (blocking) | ||||
|         flags = fcntl(fd(), F_SETFL, flags | O_NONBLOCK); | ||||
|     else | ||||
|         flags = fcntl(fd(), F_SETFL, flags & O_NONBLOCK); | ||||
|     ASSERT(flags >= 0); | ||||
| } | ||||
| 
 | ||||
| bool CSocket::connect(const CSocketAddress& address, int port) | ||||
| { | ||||
|     ASSERT(!is_connected()); | ||||
|  |  | |||
|  | @ -23,6 +23,7 @@ public: | |||
|     bool send(const ByteBuffer&); | ||||
| 
 | ||||
|     bool is_connected() const { return m_connected; } | ||||
|     void set_blocking(bool blocking); | ||||
| 
 | ||||
|     CSocketAddress source_address() const { return m_source_address; } | ||||
|     int source_port() const { return m_source_port; } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Robin Burchell
						Robin Burchell