Back to index

lightning-sunbird  0.9+nobinonly
rcnetio.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039 ** Subclass implementation for streamed network I/O (ref: prio.h)
00040 */
00041 
00042 #include "rcnetio.h"
00043 
00044 #include <private/pprio.h>
00045 
00046 RCNetStreamIO::~RCNetStreamIO()
00047     { PRStatus rv = (fd->methods->close)(fd); fd = NULL; }
00048 
00049 RCNetStreamIO::RCNetStreamIO(): RCIO(RCIO::tcp)
00050     { fd = PR_NewTCPSocket(); }
00051 
00052 RCNetStreamIO::RCNetStreamIO(PRIntn protocol): RCIO(RCIO::tcp)
00053     { fd = PR_Socket(PR_AF_INET, PR_SOCK_STREAM, protocol); }
00054 
00055 RCIO* RCNetStreamIO::Accept(RCNetAddr* addr, const RCInterval& timeout)
00056 {
00057     PRNetAddr peer;
00058     RCNetStreamIO* rcio = NULL;
00059     PRFileDesc* newfd = fd->methods->accept(fd, &peer, timeout);
00060     if (NULL != newfd)
00061     {
00062         rcio = new RCNetStreamIO();
00063         if (NULL != rcio)
00064         {
00065             *addr = &peer;
00066             rcio->fd = newfd;
00067         }
00068         else
00069             (void)(newfd->methods->close)(newfd);
00070     }
00071     return rcio;
00072 }  /* RCNetStreamIO::Accept */
00073 
00074 PRInt32 RCNetStreamIO::AcceptRead(
00075     RCIO **nd, RCNetAddr **raddr, void *buf,
00076     PRSize amount, const RCInterval& timeout)
00077 {   
00078     PRNetAddr *from;
00079     PRFileDesc *accepted;
00080     PRInt32 rv = (fd->methods->acceptread)(
00081         fd, &accepted, &from, buf, amount, timeout);
00082     if (rv >= 0)
00083     {
00084         RCNetStreamIO *ns = new RCNetStreamIO();
00085         if (NULL != *nd) ns->fd = accepted;
00086         else {PR_Close(accepted); rv = -1; }
00087         *nd = ns;
00088     }
00089     return rv;
00090 }  /* RCNetStreamIO::AcceptRead */
00091 
00092 PRInt64 RCNetStreamIO::Available()
00093     { return (fd->methods->available64)(fd); }
00094 
00095 PRStatus RCNetStreamIO::Bind(const RCNetAddr& addr)
00096     { return (fd->methods->bind)(fd, addr); }
00097 
00098 PRStatus RCNetStreamIO::Connect(const RCNetAddr& addr, const RCInterval& timeout)
00099     { return (fd->methods->connect)(fd, addr, timeout); }
00100 
00101 PRStatus RCNetStreamIO::GetLocalName(RCNetAddr *addr) const
00102 {
00103     PRNetAddr local;
00104     PRStatus rv = (fd->methods->getsockname)(fd, &local);
00105     if (PR_SUCCESS == rv) *addr = &local;
00106     return rv;
00107 }  /* RCNetStreamIO::GetLocalName */
00108 
00109 PRStatus RCNetStreamIO::GetPeerName(RCNetAddr *addr) const
00110 {
00111     PRNetAddr peer;
00112     PRStatus rv = (fd->methods->getpeername)(fd, &peer);
00113     if (PR_SUCCESS == rv) *addr = &peer;
00114     return rv;
00115 }  /* RCNetStreamIO::GetPeerName */
00116 
00117 PRStatus RCNetStreamIO::GetSocketOption(PRSocketOptionData *data) const
00118     { return (fd->methods->getsocketoption)(fd, data); }
00119 
00120 PRStatus RCNetStreamIO::Listen(PRIntn backlog)
00121     { return (fd->methods->listen)(fd, backlog); }
00122 
00123 PRInt16 RCNetStreamIO::Poll(PRInt16 in_flags, PRInt16 *out_flags)
00124     { return (fd->methods->poll)(fd, in_flags, out_flags); }
00125 
00126 PRInt32 RCNetStreamIO::Read(void *buf, PRSize amount)
00127     { return (fd->methods->read)(fd, buf, amount); }
00128 
00129 PRInt32 RCNetStreamIO::Recv(
00130     void *buf, PRSize amount, PRIntn flags, const RCInterval& timeout)
00131     { return (fd->methods->recv)(fd, buf, amount, flags, timeout); }
00132 
00133 PRInt32 RCNetStreamIO::Recvfrom(
00134     void *buf, PRSize amount, PRIntn flags,
00135     RCNetAddr* addr, const RCInterval& timeout)
00136 {
00137     PRNetAddr peer;
00138     PRInt32 rv = (fd->methods->recvfrom)(
00139         fd, buf, amount, flags, &peer, timeout);
00140     if (-1 != rv) *addr = &peer;
00141     return rv;
00142 }  /* RCNetStreamIO::Recvfrom */
00143 
00144 PRInt32 RCNetStreamIO::Send(
00145     const void *buf, PRSize amount, PRIntn flags, const RCInterval& timeout)
00146     { return (fd->methods->send)(fd, buf, amount, flags, timeout); }
00147 
00148 PRInt32 RCNetStreamIO::Sendto(
00149     const void *buf, PRSize amount, PRIntn flags,
00150     const RCNetAddr& addr, const RCInterval& timeout)
00151     { return (fd->methods->sendto)(fd, buf, amount, flags, addr, timeout); }
00152 
00153 PRStatus RCNetStreamIO::SetSocketOption(const PRSocketOptionData *data)
00154     { return (fd->methods->setsocketoption)(fd, data); }
00155 
00156 PRStatus RCNetStreamIO::Shutdown(RCIO::ShutdownHow how)
00157     { return (fd->methods->shutdown)(fd, (PRIntn)how); }
00158 
00159 PRInt32 RCNetStreamIO::TransmitFile(
00160     RCIO *source, const void *headers, PRSize hlen,
00161     RCIO::FileDisposition flags, const RCInterval& timeout)
00162 {
00163     RCNetStreamIO *src = (RCNetStreamIO*)source;
00164     return (fd->methods->transmitfile)(
00165         fd, src->fd, headers, hlen, (PRTransmitFileFlags)flags, timeout); }
00166 
00167 PRInt32 RCNetStreamIO::Write(const void *buf, PRSize amount)
00168     { return (fd->methods->write)(fd, buf, amount); }
00169 
00170 PRInt32 RCNetStreamIO::Writev(
00171     const PRIOVec *iov, PRSize size, const RCInterval& timeout)
00172     { return (fd->methods->writev)(fd, iov, size, timeout); }
00173     
00174 /*
00175 ** Invalid functions
00176 */
00177 
00178 PRStatus RCNetStreamIO::Close()
00179     { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
00180 
00181 PRStatus RCNetStreamIO::FileInfo(RCFileInfo*) const
00182     { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
00183 
00184 PRStatus RCNetStreamIO::Fsync()
00185     { return (fd->methods->fsync)(fd); }
00186 
00187 PRStatus RCNetStreamIO::Open(const char*, PRIntn, PRIntn)
00188     { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
00189 
00190 PRInt64 RCNetStreamIO::Seek(PRInt64, RCIO::Whence)
00191     { PR_SetError(PR_INVALID_METHOD_ERROR, 0); return PR_FAILURE; }
00192 
00193 /* RCNetStreamIO.cpp */
00194 
00195