PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/marsyas/Network/Socket.h

https://github.com/crmne/marsyas
C Header | 110 lines | 33 code | 23 blank | 54 comment | 0 complexity | 379cb1df70658d3f5e61c0311cf790fa MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. /***************************************************/
  2. /*! \class Socket
  3. \brief STK TCP socket client/server class.
  4. This class provides a uniform cross-platform
  5. TCP socket client or socket server interface.
  6. Methods are provided for reading or writing
  7. data buffers to/from connections. This class
  8. also provides a number of static functions for
  9. use with external socket descriptors.
  10. The user is responsible for checking the values
  11. returned by the read/write methods. Values
  12. less than or equal to zero indicate a closed
  13. or lost connection or the occurence of an error.
  14. by Perry R. Cook and Gary P. Scavone, 1995 - 2002.
  15. */
  16. /***************************************************/
  17. #if !defined(__SOCKET_H)
  18. #define __SOCKET_H
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <marsyas/common_source.h>
  22. namespace Marsyas
  23. {
  24. class Socket
  25. {
  26. public:
  27. //! Default constructor which creates a local socket server on port 2006 (or the specified port number).
  28. /*!
  29. An StkError will be thrown if a socket error occurs during instantiation.
  30. */
  31. Socket( int port = 2006 );
  32. //! Class constructor which creates a socket client connection to the specified host and port.
  33. /*!
  34. An StkError will be thrown if a socket error occurs during instantiation.
  35. */
  36. Socket( int port, const char *hostname );
  37. //! The class destructor closes the socket instance, breaking any existing connections.
  38. ~Socket();
  39. //! Connect a socket client to the specified host and port and returns the resulting socket descriptor.
  40. /*!
  41. This method is valid for socket clients only. If it is called for
  42. a socket server, -1 is returned. If the socket client is already
  43. connected, that connection is terminated and a new connection is
  44. attempted. Server connections are made using the accept() method.
  45. An StkError will be thrown if a socket error occurs during
  46. instantiation. \sa accept
  47. */
  48. int connect( int port, const char *hostname = "localhost" );
  49. //! Close this socket.
  50. void close( void );
  51. //! Return the server/client socket descriptor.
  52. int socket( void ) const;
  53. //! Return the server/client port number.
  54. int port( void ) const;
  55. //! If this is a socket server, extract the first pending connection request from the queue and create a new connection, returning the descriptor for the accepted socket.
  56. /*!
  57. If no connection requests are pending and the socket has not
  58. been set non-blocking, this function will block until a connection
  59. is present. If an error occurs or this is a socket client, -1 is
  60. returned.
  61. */
  62. int accept( void );
  63. //! If enable = false, the socket is set to non-blocking mode. When first created, sockets are by default in blocking mode.
  64. static void setBlocking( int socket, bool enable );
  65. //! Close the socket with the given descriptor.
  66. static void close( int socket );
  67. //! Returns TRUE is the socket descriptor is valid.
  68. static bool isValid( int socket );
  69. //! Write a buffer over the socket connection. Returns the number of bytes written or -1 if an error occurs.
  70. int writeBuffer(const void *buffer, long bufferSize, int flags = 0);
  71. //! Write a buffer via the specified socket. Returns the number of bytes written or -1 if an error occurs.
  72. static int writeBuffer(int socket, const void *buffer, long bufferSize, int flags );
  73. //! Read a buffer from the socket connection, up to length \e bufferSize. Returns the number of bytes read or -1 if an error occurs.
  74. int readBuffer(void *buffer, long bufferSize, int flags = 0);
  75. //! Read a buffer via the specified socket. Returns the number of bytes read or -1 if an error occurs.
  76. static int readBuffer(int socket, void *buffer, long bufferSize, int flags );
  77. protected:
  78. char msg[256];
  79. int soket;
  80. int poort;
  81. bool server;
  82. };
  83. }//namespace Marsyas
  84. #endif // defined(__SOCKET_H)