Darin Fisher wrote:
Charles Melhorn wrote:Hello, I was wondering if it's possible to create a (scriptable) UDP/IP (datagram) socket with NetLib? Upon a cursory inspection of the code, particularly the nsSocketTransportService::CreateTransportOfTypes() method and related classes, it would appear that it's only possible to create TCP/IP sockets, or variants: tcp+SSL, tcp+TLS, tcp+SOCKS, tcp+SOCKS4. Is that the case? And if it is, should I file an RFE? I believe there is a portable runtime function for creating a UDP socket, PR_NewUDPSocket(), but I'm not sure how much more infrastructure would be needed to support the use of datagrams in a Mozilla extension. Any help would be appreciated. Thanks, Charles
yup, this is a legitimate feature request. you can work around the limitations of necko by registering your own nsISocketProvider. check out the nsISocketProviderService for more details.
if you register a socket provider under the string "my-socket-type", and if you pass this string to the CreateTransportOfType method on nsISocketTransport, the socket transport code will call your socket provider's NewSocket instead of the default PR_NewTCPSocket(). iow, you can just make your socket provider call PR_NewUDPSocket() and return that.
darin
This is code from about a year ago when I was playing around with UDP applications and Mozilla. I would be surprised if this complied out of the box.
one you get it compiling, you need to register this component with the contract id that is specified in nsISocketProvider.
If you have question, please ask.
Doug Turner
[EMAIL PROTECTED]
/* ***** BEGIN LICENSE BLOCK ***** * Version: NPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Netscape Public License * Version 1.1 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.mozilla.org/NPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is mozilla.org code. * * The Initial Developer of the Original Code is * Netscape Communications Corporation. * Portions created by the Initial Developer are Copyright (C) 1998 * the Initial Developer. All Rights Reserved. * * Contributor(s): * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the NPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the NPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ #include "nsUDPSocketProvider.h" #include "nspr.h" #include "nsIComponentManager.h" #include "nsIServiceManager.h" #include "nsISocketProvider.h" NS_IMPL_THREADSAFE_ISUPPORTS1(nsUDPSocketProvider, nsISocketProvider) nsUDPSocketProvider::nsUDPSocketProvider() { NS_INIT_ISUPPORTS(); } nsUDPSocketProvider::~nsUDPSocketProvider() { } NS_IMETHODIMP nsUDPSocketProvider::NewSocket(const char *host, PRInt32 port, const char *proxyHost, PRInt32 proxyPort, PRFileDesc * *fileDesc, nsISupports **securityInfo) { PRFileDesc* udpFD = PR_OpenUDPSocket(PR_AF_INET6); if (!udpFD) return NS_ERROR_FAILURE; *fileDesc = udpFD; return NS_OK; } NS_IMETHODIMP nsUDPSocketProvider::AddToSocket(const char *host, PRInt32 port, const char *proxyHost, PRInt32 proxyPort, PRFileDesc * fileDesc, nsISupports **securityInfo) { // do nothing. return NS_OK; }
/* ***** BEGIN LICENSE BLOCK ***** * Version: NPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Netscape Public License * Version 1.1 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.mozilla.org/NPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is mozilla.org code. * * The Initial Developer of the Original Code is * Netscape Communications Corporation. * Portions created by the Initial Developer are Copyright (C) 1998 * the Initial Developer. All Rights Reserved. * * Contributor(s): * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the NPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the NPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ #define NS_UDP_SOCKETPROVIDER_CID \ { 0x75f99884, 0x7336, 0x433b, { 0x85, 0xf8, 0xaf, 0xa7, 0x46, 0xa7, 0xc7, 0xad } } #include "nsISocketProvider.h" class nsUDPSocketProvider : public nsISocketProvider { public: NS_DECL_ISUPPORTS NS_DECL_NSISOCKETPROVIDER nsUDPSocketProvider(); virtual ~nsUDPSocketProvider(); };
