Eddie James | 18e1e46 | 2018-12-11 13:24:36 -0600 | [diff] [blame] | 1 | #include "ikvm_manager.hpp" |
| 2 | |
| 3 | #include <thread> |
| 4 | |
| 5 | namespace ikvm |
| 6 | { |
| 7 | |
| 8 | Manager::Manager(const Args& args) : |
| 9 | continueExecuting(true), serverDone(false), videoDone(true), |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 10 | input(args.getKeyboardPath(), args.getPointerPath()), |
Eddie James | 18e1e46 | 2018-12-11 13:24:36 -0600 | [diff] [blame] | 11 | video(args.getVideoPath(), input, args.getFrameRate()), |
| 12 | server(args, input, video) |
| 13 | { |
| 14 | } |
| 15 | |
| 16 | void Manager::run() |
| 17 | { |
| 18 | std::thread run(serverThread, this); |
| 19 | |
| 20 | while (continueExecuting) |
| 21 | { |
| 22 | if (server.wantsFrame()) |
| 23 | { |
| 24 | video.getFrame(); |
| 25 | server.sendFrame(); |
| 26 | } |
| 27 | else |
| 28 | { |
| 29 | video.stop(); |
| 30 | } |
| 31 | |
| 32 | if (video.needsResize()) |
| 33 | { |
| 34 | videoDone = false; |
| 35 | waitServer(); |
| 36 | video.resize(); |
| 37 | server.resize(); |
| 38 | setVideoDone(); |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | setVideoDone(); |
| 43 | waitServer(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | run.join(); |
| 48 | } |
| 49 | |
| 50 | void Manager::serverThread(Manager* manager) |
| 51 | { |
| 52 | while (manager->continueExecuting) |
| 53 | { |
| 54 | manager->server.run(); |
| 55 | manager->setServerDone(); |
| 56 | manager->waitVideo(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void Manager::setServerDone() |
| 61 | { |
| 62 | std::unique_lock<std::mutex> ulock(lock); |
| 63 | |
| 64 | serverDone = true; |
| 65 | sync.notify_all(); |
| 66 | } |
| 67 | |
| 68 | void Manager::setVideoDone() |
| 69 | { |
| 70 | std::unique_lock<std::mutex> ulock(lock); |
| 71 | |
| 72 | videoDone = true; |
| 73 | sync.notify_all(); |
| 74 | } |
| 75 | |
| 76 | void Manager::waitServer() |
| 77 | { |
| 78 | std::unique_lock<std::mutex> ulock(lock); |
| 79 | |
| 80 | while (!serverDone) |
| 81 | { |
| 82 | sync.wait(ulock); |
| 83 | } |
| 84 | |
| 85 | serverDone = false; |
| 86 | } |
| 87 | |
| 88 | void Manager::waitVideo() |
| 89 | { |
| 90 | std::unique_lock<std::mutex> ulock(lock); |
| 91 | |
| 92 | while (!videoDone) |
| 93 | { |
| 94 | sync.wait(ulock); |
| 95 | } |
| 96 | |
| 97 | // don't reset videoDone |
| 98 | } |
| 99 | |
| 100 | } // namespace ikvm |