ASClientConnection.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "ASClientConnection.h"
  2. #include "ASMixer.h"
  3. #include <LibCore/CEventLoop.h>
  4. #include <LibAudio/ASAPI.h>
  5. #include <LibAudio/ABuffer.h>
  6. #include <SharedBuffer.h>
  7. #include <errno.h>
  8. #include <unistd.h>
  9. #include <sys/uio.h>
  10. #include <sys/types.h>
  11. #include <sys/socket.h>
  12. #include <stdio.h>
  13. ASClientConnection::ASClientConnection(CLocalSocket& client_socket, int client_id, ASMixer& mixer)
  14. : Connection(client_socket, client_id)
  15. , m_mixer(mixer)
  16. {
  17. }
  18. ASClientConnection::~ASClientConnection()
  19. {
  20. }
  21. void ASClientConnection::send_greeting()
  22. {
  23. ASAPI_ServerMessage message;
  24. message.type = ASAPI_ServerMessage::Type::Greeting;
  25. message.greeting.server_pid = getpid();
  26. message.greeting.your_client_id = client_id();
  27. post_message(message);
  28. }
  29. bool ASClientConnection::handle_message(const ASAPI_ClientMessage& message, const ByteBuffer&&)
  30. {
  31. switch (message.type) {
  32. case ASAPI_ClientMessage::Type::Greeting:
  33. set_client_pid(message.greeting.client_pid);
  34. break;
  35. case ASAPI_ClientMessage::Type::PlayBuffer: {
  36. // ### ensure that the size is that of a Vector<ASample>
  37. Vector<ASample> samples;
  38. {
  39. const auto& shared_buf = SharedBuffer::create_from_shared_buffer_id(message.play_buffer.buffer_id);
  40. if (!shared_buf) {
  41. did_misbehave();
  42. return false;
  43. }
  44. if (shared_buf->size() / sizeof(ASample) > 441000) {
  45. did_misbehave();
  46. return false;
  47. }
  48. samples.resize(shared_buf->size() / sizeof(ASample));
  49. memcpy(samples.data(), shared_buf->data(), shared_buf->size());
  50. }
  51. // we no longer need the buffer, so acknowledge that it's playing
  52. // TODO: rate limit playback here somehow
  53. ASAPI_ServerMessage reply;
  54. reply.type = ASAPI_ServerMessage::Type::PlayingBuffer;
  55. reply.playing_buffer.buffer_id = message.play_buffer.buffer_id;
  56. post_message(reply);
  57. m_mixer.queue(*this, adopt(*new ABuffer(move(samples))));
  58. break;
  59. }
  60. case ASAPI_ClientMessage::Type::Invalid:
  61. default:
  62. dbgprintf("ASClientConnection: Unexpected message ID %d\n", int(message.type));
  63. did_misbehave();
  64. }
  65. return true;
  66. }