DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
src/hardware/serialport/misc_util.cpp
00001 /*
00002  *  Copyright (C) 2002-2013  The DOSBox Team
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 /* $Id $ */
00020 
00021 
00022 #include "config.h"
00023 
00024 #if C_MODEM
00025 
00026 /*****************************************************************************/
00027 // C++ SDLnet wrapper
00028 
00029 #include "misc_util.h"
00030 
00031 struct _TCPsocketX {
00032         int ready;
00033 #ifdef NATIVESOCKETS
00034         SOCKET channel;
00035 #endif
00036         IPaddress remoteAddress;
00037         IPaddress localAddress;
00038         int sflag;
00039 };
00040 
00041 Bit32u Netwrapper_GetCapabilities()
00042 {
00043         Bit32u retval=0;
00044         retval = CAPWORD;
00045         return retval;
00046 }
00047 
00048 #ifdef NATIVESOCKETS
00049 TCPClientSocket::TCPClientSocket(int platformsocket) {
00050         sendbuffer=0;
00051         nativetcpstruct = new Bit8u[sizeof(struct _TCPsocketX)];
00052         
00053         mysock = (TCPsocket)nativetcpstruct;
00054         isopen = false;
00055         if(!SDLNetInited) {
00056         if(SDLNet_Init()==-1) {
00057                         LOG_MSG("SDLNet_Init failed: %s\n", SDLNet_GetError());
00058                         return;
00059                 }
00060                 SDLNetInited = true;
00061         }
00062         // fill the SDL socket manually
00063         ((struct _TCPsocketX*)nativetcpstruct)->ready=0;
00064         ((struct _TCPsocketX*)nativetcpstruct)->sflag=0;
00065         ((struct _TCPsocketX*)nativetcpstruct)->channel=(SOCKET) platformsocket;
00066         sockaddr_in             sa;
00067         socklen_t               sz;
00068         sz=sizeof(sa);
00069         if(getpeername(platformsocket, (sockaddr *)(&sa), &sz)==0) {
00070                 ((struct _TCPsocketX*)nativetcpstruct)->
00071                         remoteAddress.host=/*ntohl(*/sa.sin_addr.s_addr;//);
00072                 ((struct _TCPsocketX*)nativetcpstruct)->
00073                         remoteAddress.port=/*ntohs(*/sa.sin_port;//);
00074         }
00075         else {
00076                 mysock=0;
00077                 return;
00078         }
00079         sz=sizeof(sa);
00080         if(getsockname(platformsocket, (sockaddr *)(&sa), &sz)==0) {
00081                 ((struct _TCPsocketX*)nativetcpstruct)->
00082                         localAddress.host=/*ntohl(*/sa.sin_addr.s_addr;//);
00083                 ((struct _TCPsocketX*)nativetcpstruct)->
00084                         localAddress.port=/*ntohs(*/sa.sin_port;//);
00085         }
00086         else {
00087                 mysock=0;
00088                 return;
00089         }
00090         if(mysock!=0) {
00091                 listensocketset = SDLNet_AllocSocketSet(1);
00092                 if(!listensocketset) return;
00093                 SDLNet_TCP_AddSocket(listensocketset, mysock);
00094                 isopen=true;
00095                 return;
00096         }
00097         mysock=0;
00098         return;
00099 }
00100 #endif // NATIVESOCKETS
00101 
00102 TCPClientSocket::TCPClientSocket(TCPsocket source) {
00103 #ifdef NATIVESOCKETS
00104         nativetcpstruct=0;
00105 #endif
00106         sendbuffer=0;
00107         isopen = false;
00108         if(!SDLNetInited) {
00109         if(SDLNet_Init()==-1) {
00110                         LOG_MSG("SDLNet_Init failed: %s\n", SDLNet_GetError());
00111                         return;
00112                 }
00113                 SDLNetInited = true;
00114         }       
00115         
00116         mysock=0;
00117         listensocketset=0;
00118         if(source!=0) {
00119                 mysock = source;
00120                 listensocketset = SDLNet_AllocSocketSet(1);
00121                 if(!listensocketset) return;
00122                 SDLNet_TCP_AddSocket(listensocketset, source);
00123 
00124                 isopen=true;
00125         }
00126 }
00127 TCPClientSocket::TCPClientSocket(const char* destination, Bit16u port) {
00128 #ifdef NATIVESOCKETS
00129         nativetcpstruct=0;
00130 #endif
00131         sendbuffer=0;
00132         isopen = false;
00133         if(!SDLNetInited) {
00134         if(SDLNet_Init()==-1) {
00135                         LOG_MSG("SDLNet_Init failed: %s\n", SDLNet_GetError());
00136                         return;
00137                 }
00138                 SDLNetInited = true;
00139         }       
00140         mysock=0;
00141         listensocketset=0;
00142         
00143         IPaddress openip;
00144         //Ancient versions of SDL_net had this as char*. People still appear to be using this one.
00145         if (!SDLNet_ResolveHost(&openip,const_cast<char*>(destination),port)) {
00146                 listensocketset = SDLNet_AllocSocketSet(1);
00147                 if(!listensocketset) return;
00148                 mysock = SDLNet_TCP_Open(&openip);
00149                 if(!mysock) return;
00150                 SDLNet_TCP_AddSocket(listensocketset, mysock);
00151                 isopen=true;
00152         }
00153 }
00154 
00155 TCPClientSocket::~TCPClientSocket() {
00156         
00157         if(sendbuffer) delete [] sendbuffer;
00158 #ifdef NATIVESOCKETS
00159         if(nativetcpstruct) delete [] nativetcpstruct;
00160         else
00161 #endif
00162         if(mysock) {
00163                 if(listensocketset) SDLNet_TCP_DelSocket(listensocketset,mysock);
00164                 SDLNet_TCP_Close(mysock);
00165         }
00166 
00167         if(listensocketset) SDLNet_FreeSocketSet(listensocketset);
00168 }
00169 bool TCPClientSocket::GetRemoteAddressString(Bit8u* buffer) {
00170         IPaddress* remote_ip;
00171         Bit8u b1, b2, b3, b4;
00172         remote_ip=SDLNet_TCP_GetPeerAddress(mysock);
00173         if(!remote_ip) return false;
00174         b4=remote_ip->host>>24;
00175         b3=(remote_ip->host>>16)&0xff;
00176         b2=(remote_ip->host>>8)&0xff;
00177         b1=remote_ip->host&0xff;
00178         sprintf((char*)buffer,"%u.%u.%u.%u",b1,b2,b3,b4);
00179         return true;
00180 }
00181 
00182 bool TCPClientSocket::ReceiveArray(Bit8u* data, Bitu* size) {
00183         if(SDLNet_CheckSockets(listensocketset,0))
00184         {
00185                 Bits retval = SDLNet_TCP_Recv(mysock, data, *size);
00186                 if(retval<1) {
00187                         isopen=false;
00188                         *size=0;
00189                         return false;
00190                 } else {
00191                         *size=retval;
00192                         return true;
00193                 }
00194         }
00195         else {
00196                 *size=0;
00197                 return true;
00198         }
00199 }
00200 
00201 
00202 Bits TCPClientSocket::GetcharNonBlock() {
00203 // return:
00204 // -1: no data
00205 // -2: socket closed
00206 // 0..255: data
00207         if(SDLNet_CheckSockets(listensocketset,0))
00208         {
00209                 Bitu retval =0;
00210                 if(SDLNet_TCP_Recv(mysock, &retval, 1)!=1) {
00211                         isopen=false;
00212                         return -2;
00213                 } else return retval;
00214         }
00215         else return -1;
00216 }
00217 bool TCPClientSocket::Putchar(Bit8u data) {
00218         if(SDLNet_TCP_Send(mysock, &data, 1)!=1) {
00219                 isopen=false;
00220                 return false;
00221         }
00222         return true;
00223 }
00224 
00225 bool TCPClientSocket::SendArray(Bit8u* data, Bitu bufsize) {
00226         if((Bitu)SDLNet_TCP_Send(mysock, data, bufsize) != bufsize) {
00227                 isopen=false;
00228                 return false;
00229         }
00230         return true;
00231 }
00232 
00233 bool TCPClientSocket::SendByteBuffered(Bit8u data) {
00234         
00235         if(sendbufferindex==(sendbuffersize-1)) {
00236                 // buffer is full, get rid of it
00237                 sendbuffer[sendbufferindex]=data;
00238                 sendbufferindex=0;
00239                 
00240                 if((Bitu)SDLNet_TCP_Send(mysock, sendbuffer, sendbuffersize) != sendbuffersize) {
00241                         isopen=false;
00242                         return false;
00243                 }
00244         } else {
00245                 sendbuffer[sendbufferindex]=data;
00246                 sendbufferindex++;
00247         }
00248         return true;
00249 }
00250 /*
00251 bool TCPClientSocket::SendArrayBuffered(Bit8u* data, Bitu bufsize) {
00252         
00253         Bitu bytes
00254         while(
00255         
00256         // first case, buffer already full
00257         if(sendbufferindex==(sendbuffersize-1)) {
00258                 // buffer is full, get rid of it
00259                 sendbuffer[sendbufferindex]=data;
00260                 sendbufferindex=0;
00261                 
00262                 if(SDLNet_TCP_Send(mysock, sendbuffer, sendbuffersize)!=sendbuffersize) {
00263                         isopen=false;
00264                         return false;
00265                 }
00266         }
00267 }
00268 */
00269 void TCPClientSocket::FlushBuffer() {
00270         if(sendbufferindex) {
00271                 if((Bitu)SDLNet_TCP_Send(mysock, sendbuffer,
00272                         sendbufferindex) != sendbufferindex) {
00273                         isopen=false;
00274                         return;
00275                 }
00276                 sendbufferindex=0;
00277         }
00278 }
00279 
00280 void TCPClientSocket::SetSendBufferSize(Bitu bufsize) {
00281         if(sendbuffer) delete [] sendbuffer;
00282         sendbuffer = new Bit8u[bufsize];
00283         sendbuffersize=bufsize;
00284         sendbufferindex=0;
00285 }
00286 
00287 
00288 TCPServerSocket::TCPServerSocket(Bit16u port)
00289 {
00290         isopen = false;
00291         mysock = 0;
00292         if(!SDLNetInited) {
00293         if(SDLNet_Init()==-1) {
00294                         LOG_MSG("SDLNet_Init failed: %s\n", SDLNet_GetError());
00295                         return;
00296                 }
00297                 SDLNetInited = true;
00298         }
00299         if (port) {
00300                 IPaddress listen_ip;
00301                 SDLNet_ResolveHost(&listen_ip, NULL, port);
00302                 mysock=SDLNet_TCP_Open(&listen_ip);
00303                 if(!mysock) return;
00304         }
00305         else return;
00306         isopen = true;
00307 }
00308 
00309 TCPServerSocket::~TCPServerSocket() {
00310         if(mysock) SDLNet_TCP_Close(mysock);
00311 }
00312 
00313 TCPClientSocket* TCPServerSocket::Accept() {
00314 
00315         TCPsocket new_tcpsock;
00316 
00317         new_tcpsock=SDLNet_TCP_Accept(mysock);
00318         if(!new_tcpsock) {
00319                 //printf("SDLNet_TCP_Accept: %s\n", SDLNet_GetError());
00320                 return 0;
00321         }
00322         
00323         return new TCPClientSocket(new_tcpsock);
00324 }
00325 #endif // #if C_MODEM