Answers.Body: <p>Simple. Inter-thread communication. Specific implementations could be:</p>
  
  <ul>
  <li>Internal UCI queues</li>
  <li>Find the UCI loop function then hook it up (e.g. UCI::loop in Stockfish).</li>
  <li>Redirect the standard input/output into pipes</li>
  </ul>
  
  <p>Few points:</p>
  
  <ul>
  <li>It's not a good idea to do your works with Swift, because Swift has poor support for C++. I prefer Objective-C as it works with C++ at the source level.</li>
  <li>Redirecting is <strong>recommended</strong> if you're confident. All my iOS apps use that. You just need to send your UCI commands to a redirected pipe, while the engine receives them from the standard inputs (redirected). This is not a place for discussing unix programming, please ask Stackoverflow. It works.</li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-31T04:01:40.947Z
Answers.Text: do you use GCD to handle thread communication? What do you mean by UCI queues? is that like buffering commands? And lastly, how do you achieve that standard input/output via pipes? do you call some c function to do that?
Answers.UserDisplayName: swigganicks
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-31T04:17:03.547Z
Answers.Text: I used std::thread for inter-thread. Queue for buffering UCI commands. Standard input/output redirection is possible with the pipe() command. @swigganicks
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-28T16:36:49.683Z
Answers.Text: @SmallChess this post inspired me to make my own chess app integrating Stockfish. Thank you for all those advices, it helped me good. I would like to say though, I disagree with the "It's not a good idea to do your works with Swift". I started building an interface in C++ to talk with stockfish, and I refactored it later in Swift and I must say it works just fine (`dup` and `Pipe` act just the same if not better when it comes to handling stdout). The code is smaller and easier to understand for Swift only team members. once again thank you a lot for your post
Answers.UserDisplayName: Crocobag
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-31T03:02:27.513Z
Answers.LastActivityDate: 2018-01-31T15:32:57.630Z
Answers.LastEditDate: 2018-01-31T15:32:57.630Z
Answers.UserDisplayName: SmallChess
Body: <p>So I'm not actually making a Chess app, but I am trying to hook up an engine that uses a protocol almost exactly the same as UCI. I can get it working quite easily on a macOS app. Now, the hard part is integrating an engine like Stockfish into an iOS app. </p>
  
  <p>You can't use std input/output nor can you fork a process, so how should one manage the protocol for communicating moves to a UCI interface? I've managed to include the engine C++ library and can communicate via UCI commands in the debug console but I have no idea how to simulate std input into the engine from iOS. Should I look for hooks within the engine to call directly and simulate the game loop in Swift?</p>
  
  <p>I apologize if this is the wrong community for this question, but UCI specific questions go over the head of most SO users so I thought I might try here. There are a plethora of iOS chess apps so I'm genuinely curious how people are integrating their engines into their apps. </p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-04-22T00:20:18.693Z
Comments.Text: FWIW, I did some digging around and found [this forum discussion](http://support.stockfishchess.org/discussions/problems/321-how-to-integrate-stockfish-in-ios-app) which includes [a link to the source for v2.2 of the iOS StockFish](http://support.stockfishchess.org/discussions/problems/321/r?go=aHR0cDovL2NsLmx5LzFPMHYzODBSM1AyYTJBMDQwNjBT) app. One can reverse engineer what they've done there...
Comments.UserDisplayName: Rob
Tags: <engines><uci>
Title: Connecting UCI engine with iOS?
ThreadText: # Connecting UCI engine with iOS?
  
  (Asked by: username_0 on 2018-01-31)
  
  So I'm not actually making a Chess app, but I am trying to hook up an engine that uses a protocol almost exactly the same as UCI. I can get it working quite easily on a macOS app. Now, the hard part is integrating an engine like Stockfish into an iOS app.
  
  You can't use std input/output nor can you fork a process, so how should one manage the protocol for communicating moves to a UCI interface? I've managed to include the engine C++ library and can communicate via UCI commands in the debug console but I have no idea how to simulate std input into the engine from iOS. Should I look for hooks within the engine to call directly and simulate the game loop in Swift?
  
  I apologize if this is the wrong community for this question, but UCI specific questions go over the head of most SO users so I thought I might try here. There are a plethora of iOS chess apps so I'm genuinely curious how people are integrating their engines into their apps.
  
  **Tags:** engines, uci
  
  > **Comments:**
  > **username_1**: FWIW, I did some digging around and found [this forum discussion](http://support.stockfishchess.org/discussions/problems/321-how-to-integrate-stockfish-in-ios-app) which includes [a link to the source for v2.2 of the iOS StockFish](http://support.stockfishchess.org/discussions/problems/321/r?go=aHR0cDovL2NsLmx5LzFPMHYzODBSM1AyYTJBMDQwNjBT) app. One can reverse engineer what they've done there...  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2018-01-31. Score: 2 | Accepted answer
  
  Simple. Inter-thread communication. Specific implementations could be:
  
  * Internal UCI queues
  * Find the UCI loop function then hook it up (e.g. UCI::loop in Stockfish).
  * Redirect the standard input/output into pipes
  
  Few points:
  
  * It's not a good idea to do your works with Swift, because Swift has poor support for C++. I prefer Objective-C as it works with C++ at the source level.
  * Redirecting is **recommended** if you're confident. All my iOS apps use that. You just need to send your UCI commands to a redirected pipe, while the engine receives them from the standard inputs (redirected). This is not a place for discussing unix programming, please ask Stackoverflow. It works.
  
  > **Comments:**
  > **username_0**: do you use GCD to handle thread communication? What do you mean by UCI queues? is that like buffering commands? And lastly, how do you achieve that standard input/output via pipes? do you call some c function to do that?  
  > **username_2**: I used std::thread for inter-thread. Queue for buffering UCI commands. Standard input/output redirection is possible with the pipe() command. @username_0  
  > **username_3**: @username_2 this post inspired me to make my own chess app integrating Stockfish. Thank you for all those advices, it helped me good. I would like to say though, I disagree with the "It's not a good idea to do your works with Swift". I started building an interface in C++ to talk with stockfish, and I refactored it later in Swift and I must say it works just fine (`dup` and `Pipe` act just the same if not better when it comes to handling stdout). The code is smaller and easier to understand for Swift only team members. once again thank you a lot for your post  
  
  ---
  
  