Jack2  1.9.8
JackNetWinSocket.h
00001 /*
00002  Copyright (C) 2004-2008 Grame
00003 
00004  This program is free software; you can redistribute it and/or modify
00005  it under the terms of the GNU Lesser General Public License as published by
00006  the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
00013 
00014  You should have received a copy of the GNU Lesser General Public License
00015  along with this program; if not, write to the Free Software
00016  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 
00018  */
00019 
00020 #ifndef __JackNetWinSocket__
00021 #define __JackNetWinSocket__
00022 
00023 #include "JackNetSocket.h"
00024 #ifdef __MINGW32__
00025 #include <winsock2.h>
00026 #include <ws2tcpip.h>
00027 #include <stdint.h>
00028 #endif
00029 
00030 namespace Jack
00031 {
00032 
00033 #define E(code, s) { code, s }
00034 #define NET_ERROR_CODE WSAGetLastError()
00035 #define StrError PrintError
00036 
00037     typedef uint32_t uint;
00038     typedef int SOCKLEN;
00039     typedef struct _win_net_error win_net_error_t;
00040 
00041     struct _win_net_error
00042     {
00043         int code;
00044         const char* msg;
00045     };
00046 
00047     SERVER_EXPORT const char* PrintError(int error);
00048 
00049     //JeckNetWinSocket***************************************************************************
00050     class SERVER_EXPORT JackNetWinSocket
00051     {
00052         private:
00053             int fSockfd;
00054             int fPort;
00055             SOCKADDR_IN fSendAddr;
00056             SOCKADDR_IN fRecvAddr;
00057         public:
00058             JackNetWinSocket();
00059             JackNetWinSocket(const char* ip, int port);
00060             JackNetWinSocket(const JackNetWinSocket&);
00061             ~JackNetWinSocket();
00062 
00063             JackNetWinSocket& operator=(const JackNetWinSocket&);
00064 
00065             //socket management
00066             int NewSocket();
00067             int Bind();
00068             int BindWith(const char* ip);
00069             int BindWith(int port);
00070             int Connect();
00071             int ConnectTo(const char* ip);
00072             void Close();
00073             void Reset();
00074             bool IsSocket();
00075 
00076             //IP/PORT management
00077             void SetPort(int port);
00078             int GetPort();
00079 
00080             //address management
00081             int SetAddress(const char* ip, int port);
00082             char* GetSendIP();
00083             char* GetRecvIP();
00084 
00085             //utility
00086             int GetName(char* name);
00087             int JoinMCastGroup(const char* mcast_ip);
00088 
00089             //options management
00090             int SetOption(int level, int optname, const void* optval, SOCKLEN optlen);
00091             int GetOption(int level, int optname, void* optval, SOCKLEN* optlen);
00092 
00093             //timeout
00094             int SetTimeOut(int usec);
00095 
00096             //disable local loop
00097             int SetLocalLoop();
00098 
00099             bool IsLocal(char* ip);
00100 
00101             //network operations
00102             int SendTo(const void* buffer, size_t nbytes, int flags);
00103             int SendTo(const void* buffer, size_t nbytes, int flags, const char* ip);
00104             int Send(const void* buffer, size_t nbytes, int flags);
00105             int RecvFrom(void* buffer, size_t nbytes, int flags);
00106             int Recv(void* buffer, size_t nbytes, int flags);
00107             int CatchHost(void* buffer, size_t nbytes, int flags);
00108 
00109             //error management
00110             net_error_t GetError();
00111     };
00112 }
00113 
00114 #endif
00115