Audio plugin host https://kx.studio/carla
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.

90 lines
2.4KB

  1. #ifndef STK_SOCKET_H
  2. #define STK_SOCKET_H
  3. #include "Stk.h"
  4. #if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
  5. #include <sys/socket.h>
  6. #include <sys/types.h>
  7. #include <arpa/inet.h>
  8. #include <netdb.h>
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11. #include <netinet/in.h>
  12. #include <netinet/tcp.h>
  13. #elif defined(__OS_WINDOWS__)
  14. #include <winsock.h>
  15. #endif
  16. namespace stk {
  17. /***************************************************/
  18. /*! \class Socket
  19. \brief STK internet socket abstract base class.
  20. This class provides common functionality for TCP and UDP internet
  21. socket server and client subclasses. This class also provides a
  22. number of static functions for use with external socket
  23. descriptors.
  24. by Perry R. Cook and Gary P. Scavone, 1995-2011.
  25. */
  26. /***************************************************/
  27. class Socket : public Stk
  28. {
  29. public:
  30. enum ProtocolType {
  31. PROTO_TCP,
  32. PROTO_UDP
  33. };
  34. //! Class constructor
  35. Socket();
  36. //! Class destructor.
  37. virtual ~Socket();
  38. //! Close the socket.
  39. static void close( int socket );
  40. //! Return the socket descriptor.
  41. int id( void ) const { return soket_; };
  42. //! Return the socket port number.
  43. int port( void ) const { return port_; };
  44. //! Returns true if the socket descriptor is valid.
  45. static bool isValid( int socket ) { return socket != -1; };
  46. //! If enable = false, the socket is set to non-blocking mode. When first created, sockets are by default in blocking mode.
  47. static void setBlocking( int socket, bool enable );
  48. //! Write a buffer over the socket connection. Returns the number of bytes written or -1 if an error occurs.
  49. virtual int writeBuffer(const void *buffer, long bufferSize, int flags = 0) = 0;
  50. //! Read an input buffer, up to length \e bufferSize. Returns the number of bytes read or -1 if an error occurs.
  51. virtual int readBuffer(void *buffer, long bufferSize, int flags = 0) = 0;
  52. //! Write a buffer via the specified socket. Returns the number of bytes written or -1 if an error occurs.
  53. static int writeBuffer(int socket, const void *buffer, long bufferSize, int flags );
  54. //! Read a buffer via the specified socket. Returns the number of bytes read or -1 if an error occurs.
  55. static int readBuffer(int socket, void *buffer, long bufferSize, int flags );
  56. protected:
  57. int soket_;
  58. int port_;
  59. };
  60. } // stk namespace
  61. #endif