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 
~RCNetStreamIO()14 RCNetStreamIO::~RCNetStreamIO()
15 {
16     PRStatus rv = (fd->methods->close)(fd);
17     fd = NULL;
18 }
19 
RCNetStreamIO()20 RCNetStreamIO::RCNetStreamIO(): RCIO(RCIO::tcp)
21 {
22     fd = PR_NewTCPSocket();
23 }
24 
RCNetStreamIO(PRIntn protocol)25 RCNetStreamIO::RCNetStreamIO(PRIntn protocol): RCIO(RCIO::tcp)
26 {
27     fd = PR_Socket(PR_AF_INET, PR_SOCK_STREAM, protocol);
28 }
29 
Accept(RCNetAddr * addr,const RCInterval & timeout)30 RCIO* RCNetStreamIO::Accept(RCNetAddr* addr, const RCInterval& timeout)
31 {
32     PRNetAddr peer;
33     RCNetStreamIO* rcio = NULL;
34     PRFileDesc* newfd = fd->methods->accept(fd, &peer, timeout);
35     if (NULL != newfd)
36     {
37         rcio = new RCNetStreamIO();
38         if (NULL != rcio)
39         {
40             *addr = &peer;
41             rcio->fd = newfd;
42         }
43         else {
44             (void)(newfd->methods->close)(newfd);
45         }
46     }
47     return rcio;
48 }  /* RCNetStreamIO::Accept */
49 
AcceptRead(RCIO ** nd,RCNetAddr ** raddr,void * buf,PRSize amount,const RCInterval & timeout)50 PRInt32 RCNetStreamIO::AcceptRead(
51     RCIO **nd, RCNetAddr **raddr, void *buf,
52     PRSize amount, const RCInterval& timeout)
53 {
54     PRNetAddr *from;
55     PRFileDesc *accepted;
56     PRInt32 rv = (fd->methods->acceptread)(
57                      fd, &accepted, &from, buf, amount, timeout);
58     if (rv >= 0)
59     {
60         RCNetStreamIO *ns = new RCNetStreamIO();
61         if (NULL != *nd) {
62             ns->fd = accepted;
63         }
64         else {
65             PR_Close(accepted);
66             rv = -1;
67         }
68         *nd = ns;
69     }
70     return rv;
71 }  /* RCNetStreamIO::AcceptRead */
72 
Available()73 PRInt64 RCNetStreamIO::Available()
74 {
75     return (fd->methods->available64)(fd);
76 }
77 
Bind(const RCNetAddr & addr)78 PRStatus RCNetStreamIO::Bind(const RCNetAddr& addr)
79 {
80     return (fd->methods->bind)(fd, addr);
81 }
82 
Connect(const RCNetAddr & addr,const RCInterval & timeout)83 PRStatus RCNetStreamIO::Connect(const RCNetAddr& addr, const RCInterval& timeout)
84 {
85     return (fd->methods->connect)(fd, addr, timeout);
86 }
87 
GetLocalName(RCNetAddr * addr) const88 PRStatus RCNetStreamIO::GetLocalName(RCNetAddr *addr) const
89 {
90     PRNetAddr local;
91     PRStatus rv = (fd->methods->getsockname)(fd, &local);
92     if (PR_SUCCESS == rv) {
93         *addr = &local;
94     }
95     return rv;
96 }  /* RCNetStreamIO::GetLocalName */
97 
GetPeerName(RCNetAddr * addr) const98 PRStatus RCNetStreamIO::GetPeerName(RCNetAddr *addr) const
99 {
100     PRNetAddr peer;
101     PRStatus rv = (fd->methods->getpeername)(fd, &peer);
102     if (PR_SUCCESS == rv) {
103         *addr = &peer;
104     }
105     return rv;
106 }  /* RCNetStreamIO::GetPeerName */
107 
GetSocketOption(PRSocketOptionData * data) const108 PRStatus RCNetStreamIO::GetSocketOption(PRSocketOptionData *data) const
109 {
110     return (fd->methods->getsocketoption)(fd, data);
111 }
112 
Listen(PRIntn backlog)113 PRStatus RCNetStreamIO::Listen(PRIntn backlog)
114 {
115     return (fd->methods->listen)(fd, backlog);
116 }
117 
Poll(PRInt16 in_flags,PRInt16 * out_flags)118 PRInt16 RCNetStreamIO::Poll(PRInt16 in_flags, PRInt16 *out_flags)
119 {
120     return (fd->methods->poll)(fd, in_flags, out_flags);
121 }
122 
Read(void * buf,PRSize amount)123 PRInt32 RCNetStreamIO::Read(void *buf, PRSize amount)
124 {
125     return (fd->methods->read)(fd, buf, amount);
126 }
127 
Recv(void * buf,PRSize amount,PRIntn flags,const RCInterval & timeout)128 PRInt32 RCNetStreamIO::Recv(
129     void *buf, PRSize amount, PRIntn flags, const RCInterval& timeout)
130 {
131     return (fd->methods->recv)(fd, buf, amount, flags, timeout);
132 }
133 
Recvfrom(void * buf,PRSize amount,PRIntn flags,RCNetAddr * addr,const RCInterval & timeout)134 PRInt32 RCNetStreamIO::Recvfrom(
135     void *buf, PRSize amount, PRIntn flags,
136     RCNetAddr* addr, const RCInterval& timeout)
137 {
138     PRNetAddr peer;
139     PRInt32 rv = (fd->methods->recvfrom)(
140                      fd, buf, amount, flags, &peer, timeout);
141     if (-1 != rv) {
142         *addr = &peer;
143     }
144     return rv;
145 }  /* RCNetStreamIO::Recvfrom */
146 
Send(const void * buf,PRSize amount,PRIntn flags,const RCInterval & timeout)147 PRInt32 RCNetStreamIO::Send(
148     const void *buf, PRSize amount, PRIntn flags, const RCInterval& timeout)
149 {
150     return (fd->methods->send)(fd, buf, amount, flags, timeout);
151 }
152 
Sendto(const void * buf,PRSize amount,PRIntn flags,const RCNetAddr & addr,const RCInterval & timeout)153 PRInt32 RCNetStreamIO::Sendto(
154     const void *buf, PRSize amount, PRIntn flags,
155     const RCNetAddr& addr, const RCInterval& timeout)
156 {
157     return (fd->methods->sendto)(fd, buf, amount, flags, addr, timeout);
158 }
159 
SetSocketOption(const PRSocketOptionData * data)160 PRStatus RCNetStreamIO::SetSocketOption(const PRSocketOptionData *data)
161 {
162     return (fd->methods->setsocketoption)(fd, data);
163 }
164 
Shutdown(RCIO::ShutdownHow how)165 PRStatus RCNetStreamIO::Shutdown(RCIO::ShutdownHow how)
166 {
167     return (fd->methods->shutdown)(fd, (PRIntn)how);
168 }
169 
TransmitFile(RCIO * source,const void * headers,PRSize hlen,RCIO::FileDisposition flags,const RCInterval & timeout)170 PRInt32 RCNetStreamIO::TransmitFile(
171     RCIO *source, const void *headers, PRSize hlen,
172     RCIO::FileDisposition flags, const RCInterval& timeout)
173 {
174     RCNetStreamIO *src = (RCNetStreamIO*)source;
175     return (fd->methods->transmitfile)(
176                fd, src->fd, headers, hlen, (PRTransmitFileFlags)flags, timeout);
177 }
178 
Write(const void * buf,PRSize amount)179 PRInt32 RCNetStreamIO::Write(const void *buf, PRSize amount)
180 {
181     return (fd->methods->write)(fd, buf, amount);
182 }
183 
Writev(const PRIOVec * iov,PRSize size,const RCInterval & timeout)184 PRInt32 RCNetStreamIO::Writev(
185     const PRIOVec *iov, PRSize size, const RCInterval& timeout)
186 {
187     return (fd->methods->writev)(fd, iov, size, timeout);
188 }
189 
190 /*
191 ** Invalid functions
192 */
193 
Close()194 PRStatus RCNetStreamIO::Close()
195 {
196     PR_SetError(PR_INVALID_METHOD_ERROR, 0);
197     return PR_FAILURE;
198 }
199 
FileInfo(RCFileInfo *) const200 PRStatus RCNetStreamIO::FileInfo(RCFileInfo*) const
201 {
202     PR_SetError(PR_INVALID_METHOD_ERROR, 0);
203     return PR_FAILURE;
204 }
205 
Fsync()206 PRStatus RCNetStreamIO::Fsync()
207 {
208     return (fd->methods->fsync)(fd);
209 }
210 
Open(const char *,PRIntn,PRIntn)211 PRStatus RCNetStreamIO::Open(const char*, PRIntn, PRIntn)
212 {
213     PR_SetError(PR_INVALID_METHOD_ERROR, 0);
214     return PR_FAILURE;
215 }
216 
Seek(PRInt64,RCIO::Whence)217 PRInt64 RCNetStreamIO::Seek(PRInt64, RCIO::Whence)
218 {
219     PR_SetError(PR_INVALID_METHOD_ERROR, 0);
220     return PR_FAILURE;
221 }
222 
223 /* RCNetStreamIO.cpp */
224 
225 
226