Jack2  1.9.8
JackNetUnixSocket.h
00001 /*
00002 Copyright (C) 2008-2011 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 
00044             int fSockfd;
00045             int fPort;
00046             int fTimeOut;
00047 
00048             struct sockaddr_in fSendAddr;
00049             struct sockaddr_in fRecvAddr;
00050         #if defined(__sun__) || defined(sun)
00051             int WaitRead();
00052             int WaitWrite();
00053         #endif
00054 
00055         public:
00056 
00057             JackNetUnixSocket();
00058             JackNetUnixSocket(const char* ip, int port);
00059             JackNetUnixSocket(const JackNetUnixSocket&);
00060             ~JackNetUnixSocket();
00061 
00062             JackNetUnixSocket& operator=(const JackNetUnixSocket& socket);
00063 
00064             //socket management
00065             int NewSocket();
00066             int Bind();
00067             int BindWith(const char* ip);
00068             int BindWith(int port);
00069             int Connect();
00070             int ConnectTo(const char* ip);
00071             void Close();
00072             void Reset();
00073             bool IsSocket();
00074 
00075             //IP/PORT management
00076             void SetPort(int port);
00077             int GetPort();
00078 
00079             //address management
00080             int SetAddress(const char* ip, int port);
00081             char* GetSendIP();
00082             char* GetRecvIP();
00083 
00084             //utility
00085             int GetName(char* name);
00086             int JoinMCastGroup(const char* mcast_ip);
00087 
00088             //options management
00089             int SetOption(int level, int optname, const void* optval, socklen_t optlen);
00090             int GetOption(int level, int optname, void* optval, socklen_t* optlen);
00091 
00092             //timeout
00093             int SetTimeOut(int us);
00094 
00095             //disable local loop
00096             int SetLocalLoop();
00097 
00098             bool IsLocal(char* ip);
00099 
00100             //network operations
00101             int SendTo(const void* buffer, size_t nbytes, int flags);
00102             int SendTo(const void* buffer, size_t nbytes, int flags, const char* ip);
00103             int Send(const void* buffer, size_t nbytes, int flags);
00104             int RecvFrom(void* buffer, size_t nbytes, int flags);
00105             int Recv(void* buffer, size_t nbytes, int flags);
00106             int CatchHost(void* buffer, size_t nbytes, int flags);
00107 
00108             //error management
00109             net_error_t GetError();
00110     };
00111 }
00112 
00113 #endif