summaryrefslogtreecommitdiffstats
path: root/xbmc/network/UdpClient.h
blob: 060cb711333ad6380f772a05da3b311d56989c51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 *  Copyright (c) 2002 Frodo
 *      Portions Copyright (c) by the authors of ffmpeg and xvid
 *
 *  Copyright (C) 2002-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#pragma once

#include "threads/CriticalSection.h"
#include "threads/Thread.h"

#include <string>
#include <vector>

#include <netinet/in.h>
#include <sys/socket.h>

#include "PlatformDefs.h"

class CUdpClient : CThread
{
public:
  CUdpClient();
  ~CUdpClient(void) override;

protected:

  bool Create();
  void Destroy();

  void OnStartup() override;
  void Process() override;

  bool Broadcast(int aPort, const std::string& aMessage);
  bool Send(const std::string& aIpAddress, int aPort, const std::string& aMessage);
  bool Send(struct sockaddr_in aAddress, const std::string& aMessage);
  bool Send(struct sockaddr_in aAddress, unsigned char* pMessage, DWORD dwSize);

  virtual void OnMessage(struct sockaddr_in& aRemoteAddress,
                         const std::string& aMessage,
                         unsigned char* pMessage,
                         DWORD dwMessageLength)
  {
  }

protected:

  struct UdpCommand
  {
    struct sockaddr_in address;
    std::string message;
    unsigned char* binary;
    DWORD binarySize;
  };

  bool DispatchNextCommand();

  SOCKET client_socket;

  std::vector<UdpCommand> commands;
  typedef std::vector<UdpCommand> ::iterator COMMANDITERATOR;

  CCriticalSection critical_section;
};