jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
4.0KB

  1. /*
  2. Copyright (C) 2008-2011 Romain Moret at Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #ifndef __JackNetUnixSocket__
  16. #define __JackNetUnixSocket__
  17. #include "JackNetSocket.h"
  18. #include <sys/types.h>
  19. #include <sys/socket.h>
  20. #include <netdb.h>
  21. #include <netinet/in.h>
  22. #include <arpa/inet.h>
  23. namespace Jack
  24. {
  25. #define NET_ERROR_CODE errno
  26. #define SOCKET_ERROR -1
  27. #define StrError strerror
  28. //JackNetUnixSocket********************************************
  29. class SERVER_EXPORT JackNetUnixSocket
  30. {
  31. protected:
  32. int fFamily;
  33. int fSockfd;
  34. int fState;
  35. int fPort;
  36. int fTimeOut;
  37. struct sockaddr_storage fSendAddr;
  38. struct sockaddr_storage fRecvAddr;
  39. void Clone(const JackNetUnixSocket& socket);
  40. int ProbeAF(const char* ip, struct sockaddr_storage *addr, int (*call)(int, const struct sockaddr*, socklen_t));
  41. int BindMCastIface(const char *if_name, const int option, struct in_addr *addr);
  42. int BindMCast6Iface(const char *if_name, struct in6_addr *addr);
  43. private:
  44. char f_addr_buff[INET6_ADDRSTRLEN];
  45. #if defined(__sun__) || defined(sun)
  46. int WaitRead();
  47. int WaitWrite();
  48. #endif
  49. public:
  50. JackNetUnixSocket();
  51. JackNetUnixSocket(const char* ip, int port);
  52. JackNetUnixSocket(const JackNetUnixSocket&);
  53. ~JackNetUnixSocket();
  54. JackNetUnixSocket& operator=(const JackNetUnixSocket& socket);
  55. //socket management
  56. int NewSocket(const char *ip);
  57. int NewSocket();
  58. int Bind();
  59. int Bind(const char *if_name);
  60. int BindWith(const char* ip);
  61. int BindWith(int port);
  62. int Connect();
  63. int ConnectTo(const char* ip);
  64. void Close();
  65. void Reset();
  66. bool IsSocket();
  67. //IP/PORT management
  68. void SetPort(int port);
  69. int GetPort();
  70. //address management
  71. int SetAddress(const char* ip, int port);
  72. char* GetSendIP();
  73. int SetSendIP(const char *ip);
  74. char* GetRecvIP();
  75. int SetRecvIP(const char *ip);
  76. //utility
  77. int GetName(char* name);
  78. int JoinMCastGroup(const char* mcast_ip);
  79. int JoinMCastGroup(const char* mcast_ip, const char* if_name);
  80. //options management
  81. int SetOption(int level, int optname, const void* optval, socklen_t optlen);
  82. int GetOption(int level, int optname, void* optval, socklen_t* optlen);
  83. //timeout
  84. int SetTimeOut(int us);
  85. //disable local loop
  86. int SetLocalLoop();
  87. bool IsLocal(char* ip);
  88. //network operations
  89. int SendTo(const void* buffer, size_t nbytes, int flags);
  90. int SendTo(const void* buffer, size_t nbytes, int flags, const char* ip);
  91. int Send(const void* buffer, size_t nbytes, int flags);
  92. int RecvFrom(void* buffer, size_t nbytes, int flags);
  93. int Recv(void* buffer, size_t nbytes, int flags);
  94. int CatchHost(void* buffer, size_t nbytes, int flags);
  95. //error management
  96. net_error_t GetError();
  97. void PrintError();
  98. };
  99. }
  100. #endif