Buchempfehlung
MySQL kurz & gut
MySQL kurz & gut
Das preiswerte Taschen- buch stellt MySQL-rele- vante Inhalte systematisch und knapp dar, sodass es sich optimal zum Nach- schlagen beim Pro- grammieren eignet. [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

fb:porticula NoPaste

Info
Info / Hilfe
Liste
Übersicht / Liste
Neu
Datei hochladen
Suche
Quellcode suchen
Download
Dateidownload

connect_engines.bi

Uploader:Mitgliedgrindstone
Datum/Zeit:16.11.2013 09:10:54

#Include Once "windows.bi"

Declare Function GetEngineWhiteResponse() As String
Declare Function ReadEngineWhiteInfo() As String
Declare Sub WriteEngineWhiteInfo(s As String)
Declare Function GetEngineBlackResponse() As String
Declare Function ReadEngineBlackInfo() As String
Declare Sub WriteEngineBlackInfo(s As String)

Dim As STARTUPINFO siWhite, siBlack
Dim As PROCESS_INFORMATION pi
Dim As SECURITY_ATTRIBUTES sa
Dim shared As HANDLE hReadChildPipeWhite, hWriteChildPipeWhite, hReadChildPipeBlack, hWriteChildPipeBlack
Dim Shared As HANDLE hReadPipeWhite, hWritePipeWhite, hReadPipeBlack, hWritePipeBlack
Dim Shared As String engineWhite, engineBlack

sa.nLength = SizeOf(SECURITY_ATTRIBUTES)
sa.lpSecurityDescriptor = NULL
sa.bInheritHandle = TRUE

engineWhite = "stockfish_4_32bit.exe"
engineBlack = "Fruit-2-3-1.exe"

CreatePipe(@hReadChildPipeWhite,@hWritePipeWhite,@sa,0)
SetHandleInformation(hWritePipeWhite,HANDLE_FLAG_INHERIT,0)

CreatePipe(@hReadPipeWhite,@hWriteChildPipeWhite,@sa,0)
SetHandleInformation(hReadPipeWhite,HANDLE_FLAG_INHERIT,0)

GetStartupInfo(@siWhite)

siWhite.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
siWhite.wShowWindow = SW_SHOW
siWhite.hStdOutput  = hWriteChildPipeWhite
siWhite.hStdError   = hWriteChildPipeWhite
siWhite.hStdInput   = hReadChildPipeWhite

CreateProcess(0,engineWhite,0,0,TRUE,0,0,0,@siWhite,@pi)

CreatePipe(@hReadChildPipeBlack,@hWritePipeBlack,@sa,0)
SetHandleInformation(hWritePipeBlack,HANDLE_FLAG_INHERIT,0)

CreatePipe(@hReadPipeBlack,@hWriteChildPipeBlack,@sa,0)
SetHandleInformation(hReadPipeBlack,HANDLE_FLAG_INHERIT,0)

GetStartupInfo(@siBlack)

siBlack.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
siBlack.wShowWindow = SW_SHOW
siBlack.hStdOutput  = hWriteChildPipeBlack
siBlack.hStdError   = hWriteChildPipeBlack
siBlack.hStdInput   = hReadChildPipeBlack

CreateProcess(0,engineBlack,0,0,TRUE,0,0,0,@siBlack,@pi)

CloseHandle(hWriteChildPipeWhite)
CloseHandle(hReadChildPipeWhite)
CloseHandle(hWriteChildPipeBlack)
CloseHandle(hReadChildPipeBlack)

Function ReadEngineWhiteInfo() As String
    Dim As String g, sRet

    g = GetEngineWhiteResponse() 'request a message from the engine
    If InStrRev(g,"bestmove") Then 'the engine has finished the calculating for this move
        sRet = Mid(g,InStrRev(g,"bestmove") + 9,5) 'isolate the data of the move
        Return sRet 'send the move to the gui
    Else
        Return "err" 'in any other case send "err" to the gui
    EndIf

End Function

Function GetEngineWhiteResponse() As String
    Dim As Integer iTotalBytesAvail, iNumberOfBytesWritten, iBytesToRead
    Dim As String sRet = "", sBuf

    Const As Integer MaxBytesToRead = 4096 'maximum number of bytes to be returned at one 'ReadFile'-operation

    Do
        sBuf = "" 'clear buffer
        PeekNamedPipe(hReadPipeWhite,NULL,NULL,NULL,@iTotalBytesAvail,NULL) 'find out if there's any data in the pipe

        If iTotalBytesAvail Then 'pipe is not empty
            If iTotalBytesAvail < MaxBytesToRead Then
                iBytesToRead = iTotalBytesAvail 'set all available bytes to be read
            Else
                iBytesToRead = MaxBytesToRead 'set the first 4096 bytes to be read
            EndIf
            sBuf = String(iBytesToRead,Chr(0)) 'set the length of the buffer string to the necessary value
            ReadFile(hReadPipeWhite,StrPtr(sBuf),iBytesToRead,@iNumberOfBytesWritten,NULL) 'read the specified amount of bytes from the pipe
            sRet += sBuf 'add the buffer to the return string
            Sleep 1
        Else 'pipe is empty
            Exit Do 'return
        EndIf
    Loop

    Return sRet
End Function

Sub WriteEngineWhiteInfo(s As String)
    Dim As Integer iNumberOfBytesWritten, x
    Dim As String sBuf
    sBuf = s + Chr(10)
    WriteFile(hWritePipeWhite,StrPtr(sBuf),Len(sBuf),@iNumberOfBytesWritten,NULL)
    Sleep 1500
End Sub

Function ReadEngineBlackInfo() As String
    Dim As String g, sRet

    g = GetEngineBlackResponse() 'request a message from the engine
    If InStrRev(g,"bestmove") Then 'the engine has finished the calculating for this move
        sRet = Mid(g,InStrRev(g,"bestmove") + 9,5) 'isolate the data of the move
        Return sRet 'send the move to the gui
    Else
        Return "err" 'in any other case send "err" to the gui
    EndIf

End Function

Function GetEngineBlackResponse() As String
    Dim As Integer iTotalBytesAvail, iNumberOfBytesWritten, iBytesToRead
    Dim As String sRet = "", sBuf

    Const As Integer MaxBytesToRead = 4096 'maximum number of bytes to be returned at one 'ReadFile'-operation

    Do
        sBuf = "" 'clear buffer
        PeekNamedPipe(hReadPipeBlack,NULL,NULL,NULL,@iTotalBytesAvail,NULL) 'find out if there's any data in the pipe

        If iTotalBytesAvail Then 'pipe is not empty
            If iTotalBytesAvail < MaxBytesToRead Then
                iBytesToRead = iTotalBytesAvail 'set all available bytes to be read
            Else
                iBytesToRead = MaxBytesToRead 'set the first 4096 bytes to be read
            EndIf
            sBuf = String(iBytesToRead,Chr(0)) 'set the length of the buffer string to the necessary value
            ReadFile(hReadPipeBlack,StrPtr(sBuf),iBytesToRead,@iNumberOfBytesWritten,NULL) 'read the specified amount of bytes from the pipe
            sRet += sBuf 'add the buffer to the return string
            Sleep 1
        Else 'pipe is empty
            Exit Do 'return
        EndIf
    Loop

    Return sRet
End Function

Sub WriteEngineBlackInfo(s As String)
    Dim As Integer iNumberOfBytesWritten, x
    Dim As String sBuf
    sBuf = s + Chr(10)
    WriteFile(hWritePipeBlack,StrPtr(sBuf),Len(sBuf),@iNumberOfBytesWritten,NULL)
    Sleep 1500
End Sub