|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 /* |
|
7 ** Subclass implementation for streamed network I/O (ref: prio.h) |
|
8 */ |
|
9 |
|
10 #include "rcnetio.h" |
|
11 |
|
12 #include <private/pprio.h> |
|
13 |
|
14 RCNetStreamIO::~RCNetStreamIO() |
|
15 { PRStatus rv = (fd->methods->close)(fd); fd = NULL; } |
|
16 |
|
17 RCNetStreamIO::RCNetStreamIO(): RCIO(RCIO::tcp) |
|
18 { fd = PR_NewTCPSocket(); } |
|
19 |
|
20 RCNetStreamIO::RCNetStreamIO(PRIntn protocol): RCIO(RCIO::tcp) |
|
21 { fd = PR_Socket(PR_AF_INET, PR_SOCK_STREAM, protocol); } |
|
22 |
|
23 RCIO* RCNetStreamIO::Accept(RCNetAddr* addr, const RCInterval& timeout) |
|
24 { |
|
25 PRNetAddr peer; |
|
26 RCNetStreamIO* rcio = NULL; |
|
27 PRFileDesc* newfd = fd->methods->accept(fd, &peer, timeout); |
|
28 if (NULL != newfd) |
|
29 { |
|
30 rcio = new RCNetStreamIO(); |
|
31 if (NULL != rcio) |
|
32 { |
|
33 *addr = &peer; |
|
34 rcio->fd = newfd; |
|
35 } |
|
36 else |
|
37 (void)(newfd->methods->close)(newfd); |
|
38 } |
|
39 return rcio; |
|
40 } /* RCNetStreamIO::Accept */ |
|
41 |
|
42 PRInt32 RCNetStreamIO::AcceptRead( |
|
43 RCIO **nd, RCNetAddr **raddr, void *buf, |
|
44 PRSize amount, const RCInterval& timeout) |
|
45 { |
|
46 PRNetAddr *from; |
|
47 PRFileDesc *accepted; |
|
48 PRInt32 rv = (fd->methods->acceptread)( |
|
49 fd, &accepted, &from, buf, amount, timeout); |
|
50 if (rv >= 0) |
|
51 { |
|
52 RCNetStreamIO *ns = new RCNetStreamIO(); |
|
53 if (NULL != *nd) ns->fd = accepted; |
|
54 else {PR_Close(accepted); rv = -1; } |
|
55 *nd = ns; |
|
56 } |
|
57 return rv; |
|
58 } /* RCNetStreamIO::AcceptRead */ |
|
59 |
|
60 PRInt64 RCNetStreamIO::Available() |
|
61 { return (fd->methods->available64)(fd); } |
|
62 |
|
63 PRStatus RCNetStreamIO::Bind(const RCNetAddr& addr) |
|
64 { return (fd->methods->bind)(fd, addr); } |
|
65 |
|
66 PRStatus RCNetStreamIO::Connect(const RCNetAddr& addr, const RCInterval& timeout) |
|
67 { return (fd->methods->connect)(fd, addr, timeout); } |
|
68 |
|
69 PRStatus RCNetStreamIO::GetLocalName(RCNetAddr *addr) const |
|
70 { |
|
71 PRNetAddr local; |
|
72 PRStatus rv = (fd->methods->getsockname)(fd, &local); |
|
73 if (PR_SUCCESS == rv) *addr = &local; |
|
74 return rv; |
|
75 } /* RCNetStreamIO::GetLocalName */ |
|
76 |
|
77 PRStatus RCNetStreamIO::GetPeerName(RCNetAddr *addr) const |
|
78 { |
|
79 PRNetAddr peer; |
|
80 PRStatus rv = (fd->methods->getpeername)(fd, &peer); |
|
81 if (PR_SUCCESS == rv) *addr = &peer; |
|
82 return rv; |
|
83 } /* RCNetStreamIO::GetPeerName */ |
|
84 |
|
85 PRStatus RCNetStreamIO::GetSocketOption(PRSocketOptionData *data) const |
|
86 { return (fd->methods->getsocketoption)(fd, data); } |
|
87 |
|
88 PRStatus RCNetStreamIO::Listen(PRIntn backlog) |
|
89 { return (fd->methods->listen)(fd, backlog); } |
|
90 |
|
91 PRInt16 RCNetStreamIO::Poll(PRInt16 in_flags, PRInt16 *out_flags) |
|
92 { return (fd->methods->poll)(fd, in_flags, out_flags); } |
|
93 |
|
94 PRInt32 RCNetStreamIO::Read(void *buf, PRSize amount) |
|
95 { return (fd->methods->read)(fd, buf, amount); } |
|
96 |
|
97 PRInt32 RCNetStreamIO::Recv( |
|
98 void *buf, PRSize amount, PRIntn flags, const RCInterval& timeout) |
|
99 { return (fd->methods->recv)(fd, buf, amount, flags, timeout); } |
|
100 |
|
101 PRInt32 RCNetStreamIO::Recvfrom( |
|
102 void *buf, PRSize amount, PRIntn flags, |
|
103 RCNetAddr* addr, const RCInterval& timeout) |
|
104 { |
|
105 PRNetAddr peer; |
|
106 PRInt32 rv = (fd->methods->recvfrom)( |
|
107 fd, buf, amount, flags, &peer, timeout); |
|
108 if (-1 != rv) *addr = &peer; |
|
109 return rv; |
|
110 } /* RCNetStreamIO::Recvfrom */ |
|
111 |
|
112 PRInt32 RCNetStreamIO::Send( |
|
113 const void *buf, PRSize amount, PRIntn flags, const RCInterval& timeout) |
|
114 { return (fd->methods->send)(fd, buf, amount, flags, timeout); } |
|
115 |
|
116 PRInt32 RCNetStreamIO::Sendto( |
|
117 const void *buf, PRSize amount, PRIntn flags, |
|
118 const RCNetAddr& addr, const RCInterval& timeout) |
|
119 { return (fd->methods->sendto)(fd, buf, amount, flags, addr, timeout); } |
|
120 |
|
121 PRStatus RCNetStreamIO::SetSocketOption(const PRSocketOptionData *data) |
|
122 { return (fd->methods->setsocketoption)(fd, data); } |
|
123 |
|
124 PRStatus RCNetStreamIO::Shutdown(RCIO::ShutdownHow how) |
|
125 { return (fd->methods->shutdown)(fd, (PRIntn)how); } |
|
126 |
|
127 PRInt32 RCNetStreamIO::TransmitFile( |
|
128 RCIO *source, const void *headers, PRSize hlen, |
|
129 RCIO::FileDisposition flags, const RCInterval& timeout) |
|
130 { |
|
131 RCNetStreamIO *src = (RCNetStreamIO*)source; |
|
132 return (fd->methods->transmitfile)( |
|
133 fd, src->fd, headers, hlen, (PRTransmitFileFlags)flags, timeout); } |
|
134 |
|
135 PRInt32 RCNetStreamIO::Write(const void *buf, PRSize amount) |
|
136 { return (fd->methods->write)(fd, buf, amount); } |
|
137 |
|
138 PRInt32 RCNetStreamIO::Writev( |
|
139 const PRIOVec *iov, PRSize size, const RCInterval& timeout) |
|
140 { return (fd->methods->writev)(fd, iov, size, timeout); } |
|
141 |
|
142 /* |
|
143 ** Invalid functions |
|
144 */ |
|
145 |
|
146 PRStatus RCNetStreamIO::Close() |
|
147 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } |
|
148 |
|
149 PRStatus RCNetStreamIO::FileInfo(RCFileInfo*) const |
|
150 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } |
|
151 |
|
152 PRStatus RCNetStreamIO::Fsync() |
|
153 { return (fd->methods->fsync)(fd); } |
|
154 |
|
155 PRStatus RCNetStreamIO::Open(const char*, PRIntn, PRIntn) |
|
156 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } |
|
157 |
|
158 PRInt64 RCNetStreamIO::Seek(PRInt64, RCIO::Whence) |
|
159 { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } |
|
160 |
|
161 /* RCNetStreamIO.cpp */ |
|
162 |
|
163 |