void Client::startGame(bool isDefaultLogic, std::string firstTurn) { if((_state != ConnectionState::connected) || (_socket == NULL)){ if(_turnlistener != NULL) { _turnlistener->onStartGameDone(ResultCode::connection_error); } return; } int byteLen; byte *req; std::string payload; cJSON *payloadJSON; payloadJSON = cJSON_CreateObject(); if(isDefaultLogic == true) cJSON_AddTrueToObject(payloadJSON, "isDefaultLogic"); else cJSON_AddFalseToObject(payloadJSON, "isDefaultLogic"); cJSON_AddStringToObject(payloadJSON, "nextTurn", firstTurn.c_str()); char *cRet = cJSON_PrintUnformatted(payloadJSON); payload = cRet; req = buildWarpRequest(RequestType::start_game, payload, byteLen); _socket->sockSend((char*)req, byteLen); delete[] req; cJSON_Delete(payloadJSON); free(cRet); }
void Client::getOnlineUsers() { if((_state != ConnectionState::connected) || (_socket == NULL)){ if(_zonelistener != NULL) { liveresult _res; _res.result = ResultCode::connection_error; _zonelistener->onGetOnlineUsersDone(_res); } return; } int len; byte * req = buildWarpRequest(RequestType::get_users, "", len); char *data = new char[len]; for(int i=0; i< len; ++i) { data[i] = req[i]; } _socket->sockSend(data, len); delete[] data; delete[] req; }
void Client::getMoveHistory() { if((_state != ConnectionState::connected) || (_socket == NULL)){ if(_turnlistener != NULL) { std::vector<move> history; _turnlistener->onGetMoveHistoryDone(ResultCode::connection_error, history); } return; } int byteLen; byte *req; std::string payload; cJSON *payloadJSON; payloadJSON = cJSON_CreateObject(); cJSON_AddNumberToObject(payloadJSON, "count", 5); char *cRet = cJSON_PrintUnformatted(payloadJSON); payload = cRet; req = buildWarpRequest(RequestType::get_move_history, payload, byteLen); _socket->sockSend((char*)req, byteLen); delete[] req; cJSON_Delete(payloadJSON); free(cRet); }
void Client::invokeRoomRPC(std::string roomId,std::string name, Arguments *args) { int byteLen; byte *req; std::string payload; cJSON *payloadJSON; payloadJSON = cJSON_CreateObject(); cJSON_AddStringToObject(payloadJSON, "function", name.c_str()); cJSON_AddItemReferenceToObject(payloadJSON, "args", args->get()); cJSON_AddStringToObject(payloadJSON, "roomId", roomId.c_str()); char *cRet = cJSON_PrintUnformatted(payloadJSON); payload = cRet; req = buildWarpRequest(RequestType::room_rpc, payload, byteLen); char *data = new char[byteLen]; for(int i=0; i< byteLen; ++i) { data[i] = req[i]; } _socket->sockSend(data, byteLen); delete[] data; delete[] req; cJSON_Delete(payloadJSON); free(cRet); }
void Client::setNextTurn(std::string nextTurn) { if((_state != ConnectionState::connected) || (_socket == NULL)){ if(_turnlistener != NULL) { _turnlistener->onSetNextTurnDone(ResultCode::connection_error); } return; } int byteLen; byte *req; std::string payload; cJSON *payloadJSON; payloadJSON = cJSON_CreateObject(); cJSON_AddStringToObject(payloadJSON, "nextTurn", nextTurn.c_str()); char *cRet = cJSON_PrintUnformatted(payloadJSON); payload = cRet; req = buildWarpRequest(RequestType::set_next_turn, payload, byteLen); _socket->sockSend((char*)req, byteLen); delete[] req; cJSON_Delete(payloadJSON); free(cRet); }
void Client::sendPrivateUpdate(std::string toUser, byte *update , int updateLen) { if((_state != ConnectionState::connected) || (_socket == NULL)){ if(_updatelistener != NULL) { _updatelistener->onSendPrivateUpdateDone(ResultCode::connection_error); } return; } if(updateLen >= 1000) { if(_updatelistener != NULL) _updatelistener->onSendPrivateUpdateDone(ResultCode::bad_request); return; } byte userNameLen = (byte)toUser.length(); int msgLen = userNameLen + updateLen + 1; byte *msg = new byte[msgLen]; msg[0] = userNameLen; memcpy(msg+1, toUser.c_str(), userNameLen); memcpy(msg+1+userNameLen, update, updateLen); int len; byte * req = buildWarpRequest(RequestType::private_update, msg, msgLen,len); _socket->sockSend((char*)req, len); delete[] req; delete[] msg; }
byte* buildAuthRequest(std::string username, int &byteLen,std::string APIKEY, std::string SECRETKEY) { std::string timeStamp = getODataUTCDateFilter(); std::string params; params.append("apiKey"); params.append(APIKEY); params.append("timeStamp"); params.append(timeStamp); params.append("user"); params.append(username); params.append("version1.5"); unsigned char hmac_digest[20]; memset(hmac_digest, 0, 20); CHMAC_SHA1 hmac_sha1; hmac_sha1.HMAC_SHA1((unsigned char *)params.c_str(),params.length(), (unsigned char *)SECRETKEY.c_str(), SECRETKEY.length(),hmac_digest); std::string hmac = urlencode(base64_encode(hmac_digest, 20)); cJSON *payloadJSON; payloadJSON = cJSON_CreateObject(); cJSON_AddStringToObject(payloadJSON,"apiKey", APIKEY.c_str()); cJSON_AddStringToObject(payloadJSON,"version", "1.5"); cJSON_AddStringToObject(payloadJSON,"timeStamp", timeStamp.c_str()); cJSON_AddStringToObject(payloadJSON,"user", username.c_str()); cJSON_AddStringToObject(payloadJSON,"signature", hmac.c_str()); cJSON_AddNumberToObject(payloadJSON, "keepalive", WARP_KEEP_ALIVE_TIME_INTERVAL);//recoverytime cJSON_AddNumberToObject(payloadJSON, "recoverytime", RECOVERY_ALLOWANCE_TIME); char* cRet = cJSON_PrintUnformatted(payloadJSON); std::string payload = cRet; free(cRet); cJSON_Delete(payloadJSON); return buildWarpRequest(RequestType::auth, payload, byteLen); }
byte * buildKeepAliveRequest(int requestType, int &len) { std::string payload; cJSON *payloadJSON; payloadJSON = cJSON_CreateObject(); char* cRet = cJSON_PrintUnformatted(payloadJSON); payload = cRet; free(cRet); cJSON_Delete(payloadJSON); return buildWarpRequest(requestType, payload, len); }
byte * buildLobbyRequest(int requestType, int &len) { std::string payload; cJSON *payloadJSON; payloadJSON = cJSON_CreateObject(); cJSON_AddTrueToObject(payloadJSON, "isPrimary"); char* cRet = cJSON_PrintUnformatted(payloadJSON); payload = cRet; free(cRet); cJSON_Delete(payloadJSON); return buildWarpRequest(requestType, payload, len); }
byte * buildRoomRequest(int requestType,std::string id, int &len) { std::string payload; cJSON *payloadJSON; payloadJSON = cJSON_CreateObject(); cJSON_AddStringToObject(payloadJSON, "id",id.c_str()); char* cRet = cJSON_PrintUnformatted(payloadJSON); payload = cRet; free(cRet); cJSON_Delete(payloadJSON); return buildWarpRequest(requestType, payload, len); }
void Client::sendUdpUpdate(byte *update, int updateLen) { if((_state != ConnectionState::connected) || (updateLen >= 1000) || (_udpsocket == NULL)) { return; } int len; byte * req = buildWarpRequest(RequestType::update_peers, update, updateLen,len,2); _udpsocket->sockSend((char*)req, len); delete[] req; }
void Client::udpresponse(response* res) { if(res->resultCode == ResultCode::success){ // send ack back to complete handshake int len; byte* req = buildWarpRequest(RequestType::ack_assoc_port, "", len, 2); _udpsocket->sockSend((char*)req, len); } // fire oninitudpdone. if(_connectionReqListener!=NULL){ _connectionReqListener->onInitUDPDone(res->resultCode); } }
void Client::initUDP() { if(_udpsocket != NULL){ return; } _udpsocket = new Utility::UdpSocket(this); if(result_success != _udpsocket->connect(APPWARPSERVERHOST, APPWARPSERVERPORT)){ // fail this return; } int len; byte * req = buildWarpRequest(RequestType::assoc_port, "", len, 2); _udpsocket->sockSend((char*)req, len); }
byte * buildCreateRoomRequest(std::string name,std::string owner,int max, int &len) { std::string payload; cJSON *payloadJSON; payloadJSON = cJSON_CreateObject(); cJSON_AddStringToObject(payloadJSON, "name",name.c_str()); cJSON_AddStringToObject(payloadJSON, "owner",owner.c_str()); cJSON_AddNumberToObject(payloadJSON, "maxUsers", max); char* cRet = cJSON_PrintUnformatted(payloadJSON); payload = cRet; free(cRet); cJSON_Delete(payloadJSON); return buildWarpRequest(RequestType::create_room, payload, len); }
void Client::getRoomWithProperties(std::map<std::string,std::string> properties) { if((_state != ConnectionState::connected) || (_socket == NULL)){ if(_zonelistener != NULL) { matchedroom _room; _room.result = ResultCode::connection_error; _zonelistener->onGetMatchedRoomsDone(_room); } return; } int byteLen; byte *req; std::map<std::string,std::string>::iterator it; std::string payload; cJSON *propJSON; cJSON *payloadJSON; payloadJSON = cJSON_CreateObject(); propJSON = cJSON_CreateObject(); for(it = properties.begin(); it != properties.end(); ++it) { cJSON_AddStringToObject(propJSON, it->first.c_str(),it->second.c_str()); } char *tmp = cJSON_PrintUnformatted(propJSON); cJSON_AddStringToObject(payloadJSON, "properties", tmp); char *cRet = cJSON_PrintUnformatted(payloadJSON); payload = cRet; req = buildWarpRequest(RequestType::get_room_with_properties, payload, byteLen); char *data = new char[byteLen]; for(int i=0; i< byteLen; ++i) { data[i] = req[i]; } _socket->sockSend(data, byteLen); delete[] data; delete[] req; cJSON_Delete(propJSON); cJSON_Delete(payloadJSON); free(tmp); free(cRet); }
void Client::stopGame() { if((_state != ConnectionState::connected) || (_socket == NULL)){ if(_turnlistener != NULL) { _turnlistener->onStopGameDone(ResultCode::connection_error); } return; } int len; byte * req = buildWarpRequest(RequestType::stop_game, NULL, 0, len); _socket->sockSend((char*)req, len); delete[] req; }
void Client::unlockProperties(std::vector<std::string> properties) { if((_state != ConnectionState::connected) || (_socket == NULL)){ if(_roomlistener != NULL) { _roomlistener->onUnlockPropertiesDone(ResultCode::connection_error); } return; } int byteLen; byte *req; std::string payload; cJSON *payloadJSON; payloadJSON = cJSON_CreateObject(); std::string unlockArrayStr = ""; for(unsigned int i=0; i<properties.size(); ++i) { if(i < properties.size()-1) unlockArrayStr += properties[i] + ";"; else unlockArrayStr += properties[i]; } cJSON_AddStringToObject(payloadJSON,"unlockProperties",unlockArrayStr.c_str()); char *cRet = cJSON_PrintUnformatted(payloadJSON); payload = cRet; req = buildWarpRequest(RequestType::unlock_properties, payload, byteLen); char *data = new char[byteLen]; for(int i=0; i< byteLen; ++i) { data[i] = req[i]; } _socket->sockSend(data, byteLen); delete[] data; delete[] req; cJSON_Delete(payloadJSON); free(cRet); }
void Client::sendPrivateChat(std::string toUser, std::string message) { if((_state != ConnectionState::connected) || (_socket == NULL)){ if(_chatlistener != NULL) { _chatlistener->onSendPrivateChatDone(ResultCode::connection_error); } return; } if(message.length() >= 512) { if(_chatlistener != NULL) _chatlistener->onSendPrivateChatDone(ResultCode::bad_request); return; } std::string payload; int len; cJSON *payloadJSON; payloadJSON = cJSON_CreateObject(); cJSON_AddStringToObject(payloadJSON, "to" ,toUser.c_str()); cJSON_AddStringToObject(payloadJSON, "chat" ,message.c_str()); char *cRet = cJSON_PrintUnformatted(payloadJSON); payload = cRet; byte * req = buildWarpRequest(RequestType::private_chat, payload, len); char *data = new char[len]; for(int i=0; i< len; ++i) { data[i] = req[i]; } _socket->sockSend(data, len); delete[] data; delete[] req; cJSON_Delete(payloadJSON); free(cRet); }
void Client::joinRoomInUserRange(int minJoinedUsers, int maxJoinedUsers, bool maxPreferred) { if((_state != ConnectionState::connected) || (_socket == NULL)){ if(_roomlistener != NULL) { room _room; _room.result = ResultCode::connection_error; _roomlistener->onJoinRoomDone(_room); } return; } int byteLen; byte *req; std::string payload; cJSON *payloadJSON; payloadJSON = cJSON_CreateObject(); cJSON_AddNumberToObject(payloadJSON, "minUsers", minJoinedUsers); cJSON_AddNumberToObject(payloadJSON, "maxUsers", maxJoinedUsers); maxPreferred ? cJSON_AddTrueToObject(payloadJSON, "maxPreferred") : cJSON_AddFalseToObject(payloadJSON, "maxPreferred"); char *cRet = cJSON_PrintUnformatted(payloadJSON); payload = cRet; req = buildWarpRequest(RequestType::join_room_range, payload, byteLen); char *data = new char[byteLen]; for(int i=0; i< byteLen; ++i) { data[i] = req[i]; } _socket->sockSend(data, byteLen); delete[] data; delete[] req; cJSON_Delete(payloadJSON); free(cRet); }
void Client::sendPrivateUdpUpdate(std::string toUser,byte *update, int updateLen) { if((_state != ConnectionState::connected) || (updateLen >= 1000) || (_udpsocket == NULL)) { return; } byte userNameLen = (byte)toUser.length(); int msgLen = userNameLen + updateLen + 1; byte *msg = new byte[msgLen]; msg[0] = userNameLen; memcpy(msg+1, toUser.c_str(), userNameLen); memcpy(msg+1+userNameLen, update, updateLen); int len; byte * req = buildWarpRequest(RequestType::private_update, msg, msgLen,len,2); _udpsocket->sockSend((char*)req, len); delete[] req; delete[] msg; }
void Client::setCustomRoomData(std::string roomId, std::string customData) { if((_state != ConnectionState::connected) || (_socket == NULL)){ if(_roomlistener != NULL) { liveroom _room; _room.result = ResultCode::connection_error; _roomlistener->onSetCustomRoomDataDone(_room); } return; } int byteLen; byte *req; std::string payload; cJSON *payloadJSON; payloadJSON = cJSON_CreateObject(); cJSON_AddStringToObject(payloadJSON, "id",roomId.c_str()); cJSON_AddStringToObject(payloadJSON, "data",customData.c_str()); char *cRet = cJSON_PrintUnformatted(payloadJSON); payload = cRet; req = buildWarpRequest(RequestType::set_custom_room_data, payload, byteLen); char *data = new char[byteLen]; for(int i=0; i< byteLen; ++i) { data[i] = req[i]; } _socket->sockSend(data, byteLen); delete[] data; delete[] req; cJSON_Delete(payloadJSON); free(cRet); }
void Client::getLiveUserInfo(std::string user) { if((_state != ConnectionState::connected) || (_socket == NULL)){ if(_zonelistener != NULL) { liveuser _user; _user.result = ResultCode::connection_error; _zonelistener->onGetLiveUserInfoDone(_user); } return; } int byteLen; byte *req; std::string payload; cJSON *payloadJSON; payloadJSON = cJSON_CreateObject(); cJSON_AddStringToObject(payloadJSON, "name",user.c_str()); char *cRet = cJSON_PrintUnformatted(payloadJSON); payload = cRet; req = buildWarpRequest(RequestType::get_user_info, payload, byteLen); char *data = new char[byteLen]; for(int i=0; i< byteLen; ++i) { data[i] = req[i]; } _socket->sockSend(data, byteLen); delete[] data; delete[] req; cJSON_Delete(payloadJSON); free(cRet); }
void Client::sendUpdate(byte *update,int data_len) { if((_state != ConnectionState::connected) || (_socket == NULL)){ if(_updatelistener != NULL) { _updatelistener->onSendUpdateDone(ResultCode::connection_error); } return; } if(data_len >= 1000) { if(_updatelistener != NULL) _updatelistener->onSendUpdateDone(ResultCode::bad_request); return; } int len; byte * req = buildWarpRequest(RequestType::update_peers, update, data_len,len); _socket->sockSend((char*)req, len); delete[] req; }
void Client::updateRoomProperties(std::string roomID, std::map<std::string,std::string> properties,std::vector<std::string> removeArray) { if((_state != ConnectionState::connected) || (_socket == NULL)){ if(_roomlistener != NULL) { liveroom _room; _room.result = ResultCode::connection_error; _roomlistener->onUpdatePropertyDone(_room); } return; } int byteLen; byte *req; std::map<std::string,std::string>::iterator it; std::string payload; cJSON *propJSON; cJSON *payloadJSON; payloadJSON = cJSON_CreateObject(); propJSON = cJSON_CreateObject(); for(it = properties.begin(); it != properties.end(); ++it) { cJSON_AddStringToObject(propJSON, it->first.c_str(),it->second.c_str()); } cJSON_AddStringToObject(payloadJSON, "id", roomID.c_str()); char *tmp = cJSON_PrintUnformatted(propJSON); cJSON_AddStringToObject(payloadJSON, "addOrUpdate", tmp); std::string removeArrayStr = ""; for(unsigned int i=0; i<removeArray.size(); ++i) { if(i < removeArray.size()-1) removeArrayStr += removeArray[i] + ";"; else removeArrayStr += removeArray[i]; } cJSON_AddStringToObject(payloadJSON,"remove",removeArrayStr.c_str()); char *cRet = cJSON_PrintUnformatted(payloadJSON); payload = cRet; req = buildWarpRequest(RequestType::update_room_property, payload, byteLen); char *data = new char[byteLen]; for(int i=0; i< byteLen; ++i) { data[i] = req[i]; } _socket->sockSend(data, byteLen); delete[] data; delete[] req; cJSON_Delete(propJSON); cJSON_Delete(payloadJSON); free(cRet); free(tmp); }