DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
src/dos/dos_network.h
00001 
00002 /*
00003 DOS     Network Routines.
00004 
00005 
00006 Log:
00007 July 11 2005            cyberwalker             Created.
00008 July 23 2005            cyberwalker
00009         Removed code from dos.cpp to this file.
00010 August 8 2005           cyberwalker
00011                 Make all functions  to avoid modifying makefile.
00012 
00013 */
00014 
00015 #if defined(WIN32) && !defined(HX_DOS)
00016 
00017 #include <windows.h>
00018 
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 #include <string.h>
00022 #include <time.h>
00023 #include <errno.h>
00024 //#include <dirent.h>
00025 
00026 #include "config.h"
00027 #include "dosbox.h"
00028 #include "dos_inc.h"
00029 
00030 #include <io.h>
00031 #include <fcntl.h>
00032 #include <share.h>
00033 
00034 extern Bit16u   NetworkHandleList[127]; /*in dos_files.cpp*/
00035 
00036 inline Bit16u   Network_ToDosError(DWORD dwLastError)
00037 {
00038         Bit16u  retCode;
00039 
00040         switch(dwLastError)
00041         {
00042         case    ERROR_BAD_PIPE:
00043         case    ERROR_PIPE_BUSY:
00044         case    ERROR_NO_DATA:
00045         case    ERROR_PIPE_NOT_CONNECTED:
00046         case    ERROR_MORE_DATA:
00047                 retCode=(Bit16u)dwLastError;
00048                 break;
00049         default:
00050                 retCode=0x1;
00051                 break;
00052         }
00053 
00054         return  retCode;
00055 }//Bit16u       Network_ToDosError(DWORD dwLastError)
00056 
00057  bool   Network_SetNamedPipeState(Bit16u entry,
00058                                                                   Bit16u mask,
00059                                                                   Bit16u& errorcode)
00060 {
00061         int             ifd=NetworkHandleList[entry];
00062         HANDLE  hNamedPipe=(HANDLE)_get_osfhandle(ifd);
00063 
00064         DWORD   dwMode=0;
00065 
00066         //nonblocking/blocking
00067         if(mask & 0x8000)
00068                         dwMode|=PIPE_NOWAIT;
00069         else    dwMode|=PIPE_WAIT;
00070         
00071         //Message/byte
00072         if(mask & 0x0100)
00073                         dwMode|=PIPE_READMODE_MESSAGE;
00074         else    dwMode|=PIPE_READMODE_BYTE;
00075 
00076         if(SetNamedPipeHandleState(hNamedPipe,&dwMode,NULL,NULL))
00077                         errorcode=0;
00078         else    errorcode=Network_ToDosError(GetLastError());
00079 
00080         return  (errorcode==0);
00081 }//bool Network_SetNamedPipeState(Bit16u entry,Bit16u mask,Bit16u& errorcode)
00082 
00083 
00084  bool   Network_PeekNamedPipe(Bit16u entry,
00085                                                           unsigned char* lpBuffer,Bit16u nBufferSize,
00086                                                           Bit16u& cxRead,Bit16u& siLeft,Bit16u& dxLeftCurrent,
00087                                                           Bit16u& diStatus,
00088                                                           Bit16u& errorcode)
00089 {
00090         int             ifd=NetworkHandleList[entry];
00091         HANDLE  hNamedPipe=(HANDLE)_get_osfhandle(ifd);
00092 
00093         DWORD   BytesRead,TotalBytesAvail,BytesLeftThisMessage;
00094         if(PeekNamedPipe(hNamedPipe,lpBuffer,nBufferSize,
00095                                                 &BytesRead,&TotalBytesAvail,&BytesLeftThisMessage))
00096         {
00097                 cxRead=(Bit16u)BytesRead;
00098                 siLeft=(Bit16u)TotalBytesAvail;
00099                 dxLeftCurrent=(Bit16u)BytesLeftThisMessage;
00100                 diStatus=0x3;   //Connected
00101                 errorcode=0;
00102         }
00103         else    errorcode=Network_ToDosError(GetLastError());
00104 
00105         return  (errorcode==0);
00106 }//bool Network_PeekNamedPipe
00107 
00108 
00109  bool   Network_TranscateNamedPipe(Bit16u entry,
00110                                                                    unsigned char* lpInBuffer,Bit16u nInBufferSize,
00111                                                                    unsigned char* lpOutBuffer,Bit16u nOutBufferSize,
00112                                                                    Bit16u& cxRead,Bit16u& errorcode)
00113 {
00114         int             ifd=NetworkHandleList[entry];
00115         HANDLE  hNamedPipe=(HANDLE)_get_osfhandle(ifd);
00116 
00117         DWORD   BytesRead;
00118 
00119         if(TransactNamedPipe(hNamedPipe,
00120                                         lpInBuffer,nInBufferSize,                               
00121                                         lpOutBuffer,nOutBufferSize,
00122                                         &BytesRead,NULL))
00123         {
00124                 cxRead=(Bit16u)BytesRead;
00125                 errorcode=0;
00126         }
00127         else    errorcode=Network_ToDosError(GetLastError());
00128 
00129         return  (errorcode==0);
00130 }//bool Network_TranscateNamedPipe
00131 
00132 #endif  //WIN32
00133