Example #1
0
 int Emulator::ConnectSocket(UnitId const & endpoint, int const socket, bool const binary) {
     LOCK_SCOPE;
     size_t newIdx = NextIdx(mExclusiveHandlers);
     PacketListener::Ptr pSc(new SocketConnection(mIo, endpoint, *mpDev, socket, newIdx, *this));
     mExclusiveHandlers.emplace(newIdx, pSc);
     return newIdx;
 }
Example #2
0
    std::vector<unsigned char> Emulator::SendCmd(size_t const unit,
                                                 std::vector<unsigned char> const & packet, 
                                                 size_t const timeoutMs) {

        PacketListener::Ptr pRh(new SyncResponseHandler(UnitId(unit)));
        auto pPacketPromise = dynamic_cast<SyncResponseHandler*>(pRh.get())->GetPromise();
        auto packetFuture = pPacketPromise->get_future();
        size_t handler;
        {
            LOCK_SCOPE;
            handler = NextIdx(mExclusiveHandlers);
            mExclusiveHandlers.emplace(handler, pRh);
        }

        mpDev->SendPacket(*Packet::Down(UnitId(unit), packet.begin(), packet.end()));

        if(timeoutMs && 
           packetFuture.wait_for(chrono::milliseconds(timeoutMs)) == future_status::timeout) { 
            LOCK_SCOPE;
            mExclusiveHandlers.erase(handler);
            throw runtime_error("Response timeout");
        }

        // if the handler got called, it has been removed from the handler
        // stack automatically.
        
        auto resp = packetFuture.get();


        return std::vector<unsigned char>(resp.Begin(), resp.End());
    }
Example #3
0
 size_t Emulator::AddLog(PacketListener::Ptr log) {
     LOCK_SCOPE;
     size_t idx = NextIdx(mLogs);
     mLogs.emplace(idx, log);
     
     return idx;                
 }
Example #4
0
int GameTree::DoMove(int move){
    if((currDepth==depth) || (move >= breath) || (move <0)) return 0;
    currIdx = NextIdx(move);
    currDepth++;
    val += MoveValue();
    return 1;
}