Back to index

lightning-sunbird  0.9+nobinonly
nsSocket.h
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 Mozilla Communicator client code, released
00016  * March 31, 1998.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Samir Gehani <sgehani@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifndef _NS_SOCKET_H_
00041 #define _NS_SOCKET_H_
00042 
00043 #ifndef _WINDOWS
00044 #include <sys/time.h>
00045 #endif
00046 
00047 class nsSocket
00048 {
00049 public:
00050     nsSocket(char *aHost, int aPort);
00051     nsSocket(char *aHost, int aPort, int (*aEventPumpCB)(void) );
00052     ~nsSocket();
00053 
00054 //----------------------------------------------------------------------
00055 //   Errors
00056 //----------------------------------------------------------------------
00057     enum
00058     {
00059         OK              = 0,
00060         E_USER_CANCEL   = -813, /* user canceled the download */
00061         E_PARAM         = -1001,
00062         E_MEM           = -1002,
00063         E_INVALID_HOST  = -1003,
00064         E_SOCK_OPEN     = -1004,
00065         E_SOCK_CLOSE    = -1005,
00066         E_TIMEOUT       = -1006,
00067         E_WRITE         = -1007,
00068         E_READ_MORE     = -1008,
00069         E_READ          = -1009,
00070         E_SMALL_BUF     = -1010,
00071         E_EOF_FOUND     = -1011,
00072         E_BIND          = -1012,
00073         E_LISTEN        = -1014,
00074         E_ACCEPT        = -1015,
00075         E_GETSOCKNAME   = -1016,
00076         E_WINSOCK       = -1017,
00077         E_INVALID_ADDR  = -1018
00078     };
00079 
00080 //----------------------------------------------------------------------
00081 //   Public interface
00082 //----------------------------------------------------------------------
00083     int Open();
00084     int SrvOpen(); // server alternate to client Open()
00085     int SrvAccept(); // must be called after SrvOpen()
00086     int Send(unsigned char *aBuf, int *aBufSize);
00087     int Recv(unsigned char *aBuf, int *aBufSize);
00088     int Recv(unsigned char *aBuf, int *aBufSize, int aTimeoutThresholdUsecs);
00089     int Close();
00090 
00091     int GetHostPortString(char **aHostPort); 
00092 
00093     static 
00094     float CalcRate(struct timeval *aPre, struct timeval *aPost, int aBytes);
00095 
00096 private:
00097     int   (*mEventPumpCB)(void);
00098     char  *mHost;
00099     int   mPort;
00100     int   mFd; // connected socket
00101     int   mListenFd; // listening socket (only if SrvOpen() was called)
00102 
00103     int   IsIPAddress(char *aAddress);
00104 };
00105 
00106 //----------------------------------------------------------------------
00107 //   Macro definitions
00108 //----------------------------------------------------------------------
00109 #ifndef TRUE
00110 #define TRUE 1
00111 #endif
00112 
00113 #ifndef FALSE 
00114 #define FALSE 0
00115 #endif
00116 
00117 #ifndef NULL
00118 #define NULL 0
00119 #endif
00120 
00121 #ifdef DUMP
00122 #undef DUMP
00123 #endif
00124 #if defined(DEBUG) || defined(DEBUG_sgehani)
00125 #define DUMP(_vargs)                       \
00126 do {                                       \
00127     printf("%s %d: ", __FILE__, __LINE__); \
00128     printf _vargs;                         \
00129 } while (0);
00130 #else
00131 #define DUMP(_vargs) 
00132 #endif
00133 
00134 #endif /* _NS_SOCKET_H_ */