Jack2 1.9.6

JackNetUnixSocket.h

00001 /*
00002 Copyright (C) 2008 Romain Moret at Grame
00003 
00004 This program is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation; either version 2 of the License, or
00007 (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 
00018 */
00019 
00020 #ifndef __JackNetUnixSocket__
00021 #define __JackNetUnixSocket__
00022 
00023 #include "JackNetSocket.h"
00024 #include <sys/types.h>
00025 #include <sys/socket.h>
00026 #include <netdb.h>
00027 #include <netinet/in.h>
00028 #include <arpa/inet.h>
00029 
00030 namespace Jack
00031 {
00032 #define NET_ERROR_CODE errno
00033 #define SOCKET_ERROR -1
00034 #define StrError strerror
00035 
00036     typedef struct sockaddr socket_address_t;
00037     typedef struct in_addr address_t;
00038 
00039     //JackNetUnixSocket********************************************
00040     class SERVER_EXPORT JackNetUnixSocket
00041     {
00042         private:
00043             int fSockfd;
00044             int fPort;
00045             int fTimeOut;
00046 
00047             struct sockaddr_in fSendAddr;
00048             struct sockaddr_in fRecvAddr;
00049         #if defined(__sun__) || defined(sun)
00050             int WaitRead();
00051             int WaitWrite();
00052         #endif
00053         public:
00054             JackNetUnixSocket();
00055             JackNetUnixSocket ( const char* ip, int port );
00056             JackNetUnixSocket ( const JackNetUnixSocket& );
00057             ~JackNetUnixSocket();
00058 
00059             JackNetUnixSocket& operator= ( const JackNetUnixSocket& socket );
00060 
00061             //socket management
00062             int NewSocket();
00063             int Bind();
00064             int BindWith ( const char* ip );
00065             int BindWith ( int port );
00066             int Connect();
00067             int ConnectTo ( const char* ip );
00068             void Close();
00069             void Reset();
00070             bool IsSocket();
00071 
00072             //IP/PORT management
00073             void SetPort ( int port );
00074             int GetPort();
00075 
00076             //address management
00077             int SetAddress ( const char* ip, int port );
00078             char* GetSendIP();
00079             char* GetRecvIP();
00080 
00081             //utility
00082             int GetName ( char* name );
00083             int JoinMCastGroup ( const char* mcast_ip );
00084 
00085             //options management
00086             int SetOption ( int level, int optname, const void* optval, socklen_t optlen );
00087             int GetOption ( int level, int optname, void* optval, socklen_t* optlen );
00088 
00089             //timeout
00090             int SetTimeOut ( int us );
00091 
00092             //disable local loop
00093             int SetLocalLoop();
00094 
00095             //network operations
00096             int SendTo ( const void* buffer, size_t nbytes, int flags );
00097             int SendTo ( const void* buffer, size_t nbytes, int flags, const char* ip );
00098             int Send ( const void* buffer, size_t nbytes, int flags );
00099             int RecvFrom ( void* buffer, size_t nbytes, int flags );
00100             int Recv ( void* buffer, size_t nbytes, int flags );
00101             int CatchHost ( void* buffer, size_t nbytes, int flags );
00102 
00103             //error management
00104             net_error_t GetError();
00105     };
00106 }
00107 
00108 #endif