1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/src/cplus/rcnetio.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,163 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 +** Subclass implementation for streamed network I/O (ref: prio.h) 1.11 +*/ 1.12 + 1.13 +#include "rcnetio.h" 1.14 + 1.15 +#include <private/pprio.h> 1.16 + 1.17 +RCNetStreamIO::~RCNetStreamIO() 1.18 + { PRStatus rv = (fd->methods->close)(fd); fd = NULL; } 1.19 + 1.20 +RCNetStreamIO::RCNetStreamIO(): RCIO(RCIO::tcp) 1.21 + { fd = PR_NewTCPSocket(); } 1.22 + 1.23 +RCNetStreamIO::RCNetStreamIO(PRIntn protocol): RCIO(RCIO::tcp) 1.24 + { fd = PR_Socket(PR_AF_INET, PR_SOCK_STREAM, protocol); } 1.25 + 1.26 +RCIO* RCNetStreamIO::Accept(RCNetAddr* addr, const RCInterval& timeout) 1.27 +{ 1.28 + PRNetAddr peer; 1.29 + RCNetStreamIO* rcio = NULL; 1.30 + PRFileDesc* newfd = fd->methods->accept(fd, &peer, timeout); 1.31 + if (NULL != newfd) 1.32 + { 1.33 + rcio = new RCNetStreamIO(); 1.34 + if (NULL != rcio) 1.35 + { 1.36 + *addr = &peer; 1.37 + rcio->fd = newfd; 1.38 + } 1.39 + else 1.40 + (void)(newfd->methods->close)(newfd); 1.41 + } 1.42 + return rcio; 1.43 +} /* RCNetStreamIO::Accept */ 1.44 + 1.45 +PRInt32 RCNetStreamIO::AcceptRead( 1.46 + RCIO **nd, RCNetAddr **raddr, void *buf, 1.47 + PRSize amount, const RCInterval& timeout) 1.48 +{ 1.49 + PRNetAddr *from; 1.50 + PRFileDesc *accepted; 1.51 + PRInt32 rv = (fd->methods->acceptread)( 1.52 + fd, &accepted, &from, buf, amount, timeout); 1.53 + if (rv >= 0) 1.54 + { 1.55 + RCNetStreamIO *ns = new RCNetStreamIO(); 1.56 + if (NULL != *nd) ns->fd = accepted; 1.57 + else {PR_Close(accepted); rv = -1; } 1.58 + *nd = ns; 1.59 + } 1.60 + return rv; 1.61 +} /* RCNetStreamIO::AcceptRead */ 1.62 + 1.63 +PRInt64 RCNetStreamIO::Available() 1.64 + { return (fd->methods->available64)(fd); } 1.65 + 1.66 +PRStatus RCNetStreamIO::Bind(const RCNetAddr& addr) 1.67 + { return (fd->methods->bind)(fd, addr); } 1.68 + 1.69 +PRStatus RCNetStreamIO::Connect(const RCNetAddr& addr, const RCInterval& timeout) 1.70 + { return (fd->methods->connect)(fd, addr, timeout); } 1.71 + 1.72 +PRStatus RCNetStreamIO::GetLocalName(RCNetAddr *addr) const 1.73 +{ 1.74 + PRNetAddr local; 1.75 + PRStatus rv = (fd->methods->getsockname)(fd, &local); 1.76 + if (PR_SUCCESS == rv) *addr = &local; 1.77 + return rv; 1.78 +} /* RCNetStreamIO::GetLocalName */ 1.79 + 1.80 +PRStatus RCNetStreamIO::GetPeerName(RCNetAddr *addr) const 1.81 +{ 1.82 + PRNetAddr peer; 1.83 + PRStatus rv = (fd->methods->getpeername)(fd, &peer); 1.84 + if (PR_SUCCESS == rv) *addr = &peer; 1.85 + return rv; 1.86 +} /* RCNetStreamIO::GetPeerName */ 1.87 + 1.88 +PRStatus RCNetStreamIO::GetSocketOption(PRSocketOptionData *data) const 1.89 + { return (fd->methods->getsocketoption)(fd, data); } 1.90 + 1.91 +PRStatus RCNetStreamIO::Listen(PRIntn backlog) 1.92 + { return (fd->methods->listen)(fd, backlog); } 1.93 + 1.94 +PRInt16 RCNetStreamIO::Poll(PRInt16 in_flags, PRInt16 *out_flags) 1.95 + { return (fd->methods->poll)(fd, in_flags, out_flags); } 1.96 + 1.97 +PRInt32 RCNetStreamIO::Read(void *buf, PRSize amount) 1.98 + { return (fd->methods->read)(fd, buf, amount); } 1.99 + 1.100 +PRInt32 RCNetStreamIO::Recv( 1.101 + void *buf, PRSize amount, PRIntn flags, const RCInterval& timeout) 1.102 + { return (fd->methods->recv)(fd, buf, amount, flags, timeout); } 1.103 + 1.104 +PRInt32 RCNetStreamIO::Recvfrom( 1.105 + void *buf, PRSize amount, PRIntn flags, 1.106 + RCNetAddr* addr, const RCInterval& timeout) 1.107 +{ 1.108 + PRNetAddr peer; 1.109 + PRInt32 rv = (fd->methods->recvfrom)( 1.110 + fd, buf, amount, flags, &peer, timeout); 1.111 + if (-1 != rv) *addr = &peer; 1.112 + return rv; 1.113 +} /* RCNetStreamIO::Recvfrom */ 1.114 + 1.115 +PRInt32 RCNetStreamIO::Send( 1.116 + const void *buf, PRSize amount, PRIntn flags, const RCInterval& timeout) 1.117 + { return (fd->methods->send)(fd, buf, amount, flags, timeout); } 1.118 + 1.119 +PRInt32 RCNetStreamIO::Sendto( 1.120 + const void *buf, PRSize amount, PRIntn flags, 1.121 + const RCNetAddr& addr, const RCInterval& timeout) 1.122 + { return (fd->methods->sendto)(fd, buf, amount, flags, addr, timeout); } 1.123 + 1.124 +PRStatus RCNetStreamIO::SetSocketOption(const PRSocketOptionData *data) 1.125 + { return (fd->methods->setsocketoption)(fd, data); } 1.126 + 1.127 +PRStatus RCNetStreamIO::Shutdown(RCIO::ShutdownHow how) 1.128 + { return (fd->methods->shutdown)(fd, (PRIntn)how); } 1.129 + 1.130 +PRInt32 RCNetStreamIO::TransmitFile( 1.131 + RCIO *source, const void *headers, PRSize hlen, 1.132 + RCIO::FileDisposition flags, const RCInterval& timeout) 1.133 +{ 1.134 + RCNetStreamIO *src = (RCNetStreamIO*)source; 1.135 + return (fd->methods->transmitfile)( 1.136 + fd, src->fd, headers, hlen, (PRTransmitFileFlags)flags, timeout); } 1.137 + 1.138 +PRInt32 RCNetStreamIO::Write(const void *buf, PRSize amount) 1.139 + { return (fd->methods->write)(fd, buf, amount); } 1.140 + 1.141 +PRInt32 RCNetStreamIO::Writev( 1.142 + const PRIOVec *iov, PRSize size, const RCInterval& timeout) 1.143 + { return (fd->methods->writev)(fd, iov, size, timeout); } 1.144 + 1.145 +/* 1.146 +** Invalid functions 1.147 +*/ 1.148 + 1.149 +PRStatus RCNetStreamIO::Close() 1.150 + { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } 1.151 + 1.152 +PRStatus RCNetStreamIO::FileInfo(RCFileInfo*) const 1.153 + { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } 1.154 + 1.155 +PRStatus RCNetStreamIO::Fsync() 1.156 + { return (fd->methods->fsync)(fd); } 1.157 + 1.158 +PRStatus RCNetStreamIO::Open(const char*, PRIntn, PRIntn) 1.159 + { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } 1.160 + 1.161 +PRInt64 RCNetStreamIO::Seek(PRInt64, RCIO::Whence) 1.162 + { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; } 1.163 + 1.164 +/* RCNetStreamIO.cpp */ 1.165 + 1.166 +