CLocalSocket.cpp 953 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include <LibCore/CLocalSocket.h>
  2. #include <sys/socket.h>
  3. #include <errno.h>
  4. #ifndef SOCK_NONBLOCK
  5. #include <sys/ioctl.h>
  6. #endif
  7. CLocalSocket::CLocalSocket(int fd, CObject* parent)
  8. : CSocket(CSocket::Type::Local, parent)
  9. {
  10. // NOTE: This constructor is used by CLocalServer::accept(), so the socket is already connected.
  11. m_connected = true;
  12. set_fd(fd);
  13. set_mode(CIODevice::ReadWrite);
  14. set_error(0);
  15. }
  16. CLocalSocket::CLocalSocket(CObject* parent)
  17. : CSocket(CSocket::Type::Local, parent)
  18. {
  19. #ifdef SOCK_NONBLOCK
  20. int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  21. #else
  22. int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  23. int option = 1;
  24. ioctl(fd, FIONBIO, &option);
  25. fcntl(fd, F_SETFD, FD_CLOEXEC);
  26. #endif
  27. if (fd < 0) {
  28. set_error(errno);
  29. } else {
  30. set_fd(fd);
  31. set_mode(CIODevice::ReadWrite);
  32. set_error(0);
  33. }
  34. }
  35. CLocalSocket::~CLocalSocket()
  36. {
  37. }