| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 15:04:03 +01:00
										 |  |  | #include <LibCore/TCPSocket.h>
 | 
					
						
							| 
									
										
										
										
											2019-08-05 20:47:30 +10:00
										 |  |  | #include <errno.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | #include <sys/socket.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 15:31:30 +02:00
										 |  |  | #ifndef SOCK_NONBLOCK
 | 
					
						
							|  |  |  | #    include <sys/ioctl.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | namespace Core { | 
					
						
							| 
									
										
										
										
											2019-08-05 20:47:30 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | TCPSocket::TCPSocket(int fd, Object* parent) | 
					
						
							|  |  |  |     : Socket(Socket::Type::TCP, parent) | 
					
						
							| 
									
										
										
										
											2019-08-05 20:47:30 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-07 11:37:51 +13:00
										 |  |  |     // NOTE: This constructor is used by TCPServer::accept(), so the socket is already connected.
 | 
					
						
							| 
									
										
										
										
											2019-09-22 21:46:46 +02:00
										 |  |  |     m_connected = true; | 
					
						
							| 
									
										
										
										
											2019-08-05 20:47:30 +10:00
										 |  |  |     set_fd(fd); | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     set_mode(IODevice::ReadWrite); | 
					
						
							| 
									
										
										
										
											2019-08-05 20:47:30 +10:00
										 |  |  |     set_error(0); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-03-18 14:09:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | TCPSocket::TCPSocket(Object* parent) | 
					
						
							|  |  |  |     : Socket(Socket::Type::TCP, parent) | 
					
						
							| 
									
										
										
										
											2019-03-18 14:09:58 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-05-23 15:31:30 +02:00
										 |  |  | #ifdef SOCK_NONBLOCK
 | 
					
						
							| 
									
										
										
										
											2019-04-08 04:53:45 +02:00
										 |  |  |     int fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); | 
					
						
							| 
									
										
										
										
											2020-05-23 15:31:30 +02:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     int fd = socket(AF_INET, SOCK_STREAM, 0); | 
					
						
							|  |  |  |     int option = 1; | 
					
						
							|  |  |  |     ioctl(fd, FIONBIO, &option); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-03-18 14:09:58 +01:00
										 |  |  |     if (fd < 0) { | 
					
						
							| 
									
										
										
										
											2019-08-17 11:07:15 +02:00
										 |  |  |         set_error(errno); | 
					
						
							| 
									
										
										
										
											2019-03-18 14:09:58 +01:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         set_fd(fd); | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |         set_mode(IODevice::ReadWrite); | 
					
						
							| 
									
										
										
										
											2019-03-18 14:09:58 +01:00
										 |  |  |         set_error(0); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | TCPSocket::~TCPSocket() | 
					
						
							| 
									
										
										
										
											2019-03-18 14:09:58 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |