summaryrefslogtreecommitdiffstats
path: root/content/browser/renderer_host/p2p_socket_host_posix.h
blob: 855d7ee0778ab4e344209bde8282d735d80c8f09 (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
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_POSIX_H_
#define CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_POSIX_H_

#include "content/common/p2p_sockets.h"

#include "base/message_loop.h"
#include "content/browser/renderer_host/p2p_socket_host.h"

class P2PSocketHostPosix : public P2PSocketHost {
 public:
  P2PSocketHostPosix(P2PSocketsHost* host, int routing_id, int id);
  virtual ~P2PSocketHostPosix();

  virtual bool Init();
  virtual void Send(const net::IPEndPoint& socket_address,
                    const std::vector<char>& data);

 private:
  enum State {
    STATE_UNINITIALIZED,
    STATE_OPEN,
    STATE_ERROR,
  };

  // MessageLoopForIO::Watcher implementation used to catch read
  // events from the socket.
  class ReadWatcher : public MessageLoopForIO::Watcher {
   public:
    explicit ReadWatcher(P2PSocketHostPosix* socket) : socket_(socket) { }

    // MessageLoopForIO::Watcher methods
    virtual void OnFileCanReadWithoutBlocking(int /* fd */) {
      socket_->DidCompleteRead();
    }
    virtual void OnFileCanWriteWithoutBlocking(int /* fd */) {}

   private:
    P2PSocketHostPosix* socket_;

    DISALLOW_COPY_AND_ASSIGN(ReadWatcher);
  };

  void DidCompleteRead();
  void OnError();

  State state_;
  int socket_;
  MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_;
  ReadWatcher read_watcher_;

  DISALLOW_COPY_AND_ASSIGN(P2PSocketHostPosix);
};

#endif  // CONTENT_BROWSER_RENDERER_HOST_P2P_SOCKET_HOST_POSIX_H_