-
Notifications
You must be signed in to change notification settings - Fork 8
/
tcp_socket.h
77 lines (56 loc) · 1.94 KB
/
tcp_socket.h
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
70
71
72
73
74
75
76
77
// Copyright (c) 2012 The Chromium OS 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 SOCKET_H
#define SOCKET_H
#include <vector>
#include "ppapi/cpp/completion_callback.h"
#include "ppapi/cpp/private/tcp_socket_private.h"
#include "file_system.h"
#include "pthread_helpers.h"
class TCPSocket : public FileStream {
public:
TCPSocket(int fd, int oflag);
virtual ~TCPSocket();
int fd() { return fd_; }
int oflag() { return oflag_; }
bool is_block() { return !(oflag_ & O_NONBLOCK); }
bool is_open() { return socket_ != NULL; }
bool connect(const char* host, uint16_t port);
bool accept(PP_Resource resource);
virtual void addref();
virtual void release();
virtual FileStream* dup(int fd);
virtual void close();
virtual int read(char* buf, size_t count, size_t* nread);
virtual int write(const char* buf, size_t count, size_t* nwrote);
virtual int fcntl(int cmd, va_list ap);
virtual bool is_read_ready();
virtual bool is_write_ready();
virtual bool is_exception();
private:
void PostReadTask();
void PostWriteTask(int32_t* pres, bool always_post);
void Connect(int32_t result, const char* host, uint16_t port, int32_t* pres);
void OnConnect(int32_t result, int32_t* pres);
void Read(int32_t result);
void OnRead(int32_t result);
void Write(int32_t result, int32_t* pres);
void OnWrite(int32_t result, int32_t* pres);
void Close(int32_t result, int32_t* pres);
bool Accept(int32_t result, PP_Resource resource, int32_t* pres);
static const size_t kBufSize = 64 * 1024;
int ref_;
int fd_;
int oflag_;
pp::CompletionCallbackFactory<TCPSocket> factory_;
pp::TCPSocketPrivate* socket_;
std::vector<char> in_buf_;
std::vector<char> out_buf_;
std::vector<char> read_buf_;
std::vector<char> write_buf_;
bool read_sent_;
bool write_sent_;
DISALLOW_COPY_AND_ASSIGN(TCPSocket);
};
#endif // SOCKET_H