mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 16:22:43 +00:00 
			
		
		
		
	LibCore: Add CLocalServer, a server-only local socket class.
Instead of trying to support both client and server in CLocalSocket, let's
have a specialized server class.
The basic usage is:
    CLocalServer server;
    server.listen("/tmp/name-of-portal");
    server.on_ready_to_accept = [&] {
        CLocalSocket* client = server.accept();
        ...
    };
This will make things a lot simpler, since an accepting socket doesn't need
half of the stuff that a regular CIODevice provides. :^)
			
			
This commit is contained in:
		
							parent
							
								
									9b7e1eb287
								
							
						
					
					
						commit
						b3fe9cde52
					
				
					 5 changed files with 93 additions and 0 deletions
				
			
		
							
								
								
									
										54
									
								
								Libraries/LibCore/CLocalServer.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								Libraries/LibCore/CLocalServer.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,54 @@ | |||
| #include <LibCore/CLocalServer.h> | ||||
| #include <LibCore/CLocalSocket.h> | ||||
| #include <LibCore/CNotifier.h> | ||||
| #include <stdio.h> | ||||
| #include <sys/socket.h> | ||||
| 
 | ||||
| CLocalServer::CLocalServer(CObject* parent) | ||||
|     : CObject(parent) | ||||
| { | ||||
|     m_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0); | ||||
|     ASSERT(m_fd >= 0); | ||||
| } | ||||
| 
 | ||||
| CLocalServer::~CLocalServer() | ||||
| { | ||||
| } | ||||
| 
 | ||||
| bool CLocalServer::listen(const String& address) | ||||
| { | ||||
|     if (m_listening) | ||||
|         return false; | ||||
| 
 | ||||
|     int rc; | ||||
| 
 | ||||
|     auto socket_address = CSocketAddress::local(address); | ||||
|     auto un = socket_address.to_sockaddr_un(); | ||||
|     rc = ::bind(m_fd, (const sockaddr*)&un, sizeof(un)); | ||||
|     ASSERT(rc == 0); | ||||
| 
 | ||||
|     rc = ::listen(m_fd, 5); | ||||
|     ASSERT(rc == 0); | ||||
|     m_listening = true; | ||||
| 
 | ||||
|     m_notifier = make<CNotifier>(m_fd, CNotifier::Event::Read); | ||||
|     m_notifier->on_ready_to_read = [this] { | ||||
|         if (on_ready_to_accept) | ||||
|             on_ready_to_accept(); | ||||
|     }; | ||||
|     return true; | ||||
| } | ||||
| 
 | ||||
| CLocalSocket* CLocalServer::accept() | ||||
| { | ||||
|     ASSERT(m_listening); | ||||
|     sockaddr_un un; | ||||
|     socklen_t un_size = sizeof(un); | ||||
|     int accepted_fd = ::accept(m_fd, (sockaddr*)&un, &un_size); | ||||
|     if (accepted_fd < 0) { | ||||
|         perror("accept"); | ||||
|         return nullptr; | ||||
|     } | ||||
| 
 | ||||
|     return new CLocalSocket({}, accepted_fd); | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling