static int HandleBackupStart(Network &net, ChunkDB &chunk_db) { printf("backup start\n"); strcpy(backup_path, recv_buf+1); int ret = 1; send_buf[0] = BACKUP_START_REP; send_buf[1] = 1; files_info_fp = fopen(kFilesInfoPath, "w"); if(files_info_fp == NULL) { send_buf[1] = -1; ret = -1; } files_compose_fp = fopen(kFilesComposePath, "wb"); if(files_compose_fp == NULL) { send_buf[1] = -2; ret = -2; } current_file_compose_offset = 0; if(chunk_db.OpenDB() < 0) { send_buf[1] = -3; ret = -3; } if(InitContainerRecord() < 0) { send_buf[1] = -4; ret = -4; } char tmp_path[kMaxPathLen]; GetContainerPath(current_container_id, tmp_path); write_container_fd = fopen(tmp_path, "a"); if(write_container_fd == NULL) { send_buf[1] = -5; ret = -5; } if(net.Send(send_buf, 2) < 0) { ret = -6; } total_chunk_count = 0; same_chunk_count = 0; total_chunk_bytes = 0; save_chunk_bytes = 0; return ret; }
static int HandleIncData(Network &net) { fwrite(recv_buf+1, 1, msg_len-1, current_inc_fd); send_buf[0] = INC_DATA_REP; if(net.Send(send_buf, 1) < 0) return -1; return 1; }
static int HandleIncStart(Network &net) { send_buf[0] = INC_START_REP; current_inc_fd = fopen(kCurrentIncFilePath, "wb"); if(current_inc_fd == NULL) send_buf[1] = 0; else send_buf[1] = 1; if(net.Send(send_buf, 2) < 0) return -1; return 1; }
static int HandleChunkStart(Network &net) { memcpy(¤t_chunk_count, recv_buf+1, 4); strcpy(current_file_path, recv_buf+5); total_chunk_count += current_chunk_count; file_chunks.clear(); send_buf[0] = CHUNK_START_REP; if(net.Send(send_buf, 1) < 0) return -1; return 1; }
int __cdecl main(void) { // Create a SOCKET for connecting to server // Setup the TCP listening socket Network nt; int iResult; SOCKET ListenSocket; ListenSocket = nt.Bind("0.0.0.0", "5005"); ListenSocket = nt.Listen(ListenSocket); SOCKET ClientSocket = nt.Accept(ListenSocket); char recvbuf[DEFAULT_BUFLEN]; // Receive until the peer shuts down the connection do { nt.Recv(ClientSocket, recvbuf); // Echo the buffer back to the sender nt.Send(ClientSocket, "hello my name"); } while (ClientSocket != INVALID_SOCKET); // shutdown the connection since we're done iResult = shutdown(ClientSocket, SD_SEND); if (iResult == SOCKET_ERROR) { printf("shutdown failed with error: %d\n", WSAGetLastError()); closesocket(ClientSocket); WSACleanup(); return 1; } // cleanup closesocket(ClientSocket); WSACleanup(); return 0; }
static int HandleChunkEnd(Network &net) { send_buf[0] = CHUNK_END_REP; for(Uint4 i = 0; i < current_chunk_count; i++) { if(file_chunks[i].is_duplication) { send_buf[1+i] = 1; } else { send_buf[1+i] = 0; } } if(net.Send(send_buf, current_chunk_count+1) < 0) return -1; return 1; }
static int HandleBackupEnd(Network &net, ChunkDB &chunk_db) { fclose(files_info_fp); fclose(files_compose_fp); chunk_db.CloseDB(); UpdateContainerRecord(); fclose(write_container_fd); send_buf[0] = BACKUP_END_REP; if(net.Send(send_buf, 1) < 0) return -1; printf("backup end\n"); printf("same: %u total: %u\n", same_chunk_count, total_chunk_count); printf("save: %u total: %u\n\n\n", save_chunk_bytes, total_chunk_bytes); return 1; }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { #else int main (int argc, char *argv[]) { #endif Ogre::Root *ogre; Ogre::RenderWindow *window; Ogre::SceneManager *sceneMgr; Ogre::SceneManager *guiSceneMgr; Ogre::Camera *camera; Ogre::Camera *guiCamera; // fire up an Ogre rendering window. Clearing the first two (of three) params will let us // specify plugins and resources in code instead of via text file ogre = new Ogre::Root("", ""); #if defined(_DEBUG) ogre->loadPlugin("RenderSystem_GL_d"); #else ogre->loadPlugin("RenderSystem_GL"); #endif Ogre::RenderSystemList *renderSystems = NULL; Ogre::RenderSystemList::iterator r_it; renderSystems = ogre->getAvailableRenderers(); r_it = renderSystems->begin(); ogre->setRenderSystem(*r_it); ogre->initialise(false); // load common plugins #if defined(_DEBUG) //ogre->loadPlugin("Plugin_CgProgramManager_d"); ogre->loadPlugin("Plugin_OctreeSceneManager_d"); #else //ogre->loadPlugin("Plugin_CgProgramManager"); ogre->loadPlugin("Plugin_OctreeSceneManager"); #endif // load the basic resource location(s) Ogre::ResourceGroupManager::getSingleton().addResourceLocation("resource", "FileSystem", "General"); Ogre::ResourceGroupManager::getSingleton().addResourceLocation("resource/gui.zip", "Zip", "GUI"); Ogre::ResourceGroupManager::getSingleton().addResourceLocation("meshes", "FileSystem", "Meshes"); #if defined(WIN32) Ogre::ResourceGroupManager::getSingleton().addResourceLocation("c:\\windows\\fonts", "FileSystem", "GUI"); #endif Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup("General"); Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup("GUI"); Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup("Meshes"); // setup main window; hardcode some defaults for the sake of presentation Ogre::NameValuePairList opts; opts["resolution"] = "1024x768"; opts["fullscreen"] = "false"; opts["vsync"] = "false"; // create a rendering window with the title "CDK" window = ogre->createRenderWindow("Bouwgame Client v0.1", 1024, 768, false, &opts); // since this is basically a CEGUI app, we can use the ST_GENERIC scene manager for now; in a later article // we'll see how to change this sceneMgr = ogre->createSceneManager(Ogre::ST_GENERIC); guiSceneMgr = ogre->createSceneManager(Ogre::ST_GENERIC); guiCamera = guiSceneMgr->createCamera("GUICamera"); guiCamera->setNearClipDistance(5); camera = sceneMgr->createCamera("camera"); camera->setNearClipDistance(5); Ogre::Viewport* vp = window->addViewport(guiCamera); vp->setBackgroundColour(Ogre::ColourValue(0.2f,0.2f,0.8f)); /* ambient light */ sceneMgr->setAmbientLight(Ogre::ColourValue(0.8, 0.8, 0.8)); sceneMgr->setShadowTechnique(Ogre::SHADOWTYPE_STENCIL_ADDITIVE); /* meshes */ Ogre::Entity* ent = sceneMgr->createEntity( "BouwPlaatsEntity", "world.mesh" ); Ogre::SceneNode* node = sceneMgr->getRootSceneNode()->createChildSceneNode("BouwNode", Ogre::Vector3(0, 0, 0)); node->attachObject(ent); //node->setScale(Ogre::Vector3(0.7,0.7,0.7)); ent = sceneMgr->createEntity("plaats", "bouwplaats_step_00.mesh"); Ogre::Vector3 size = ent->getBoundingBox().getSize(); Ogre::LogManager::getSingletonPtr()->getDefaultLog()->logMessage(Ogre::String("Bouwplaats size: ") + Ogre::StringConverter::toString(size)); size = ent->getBoundingBox().getMaximum(); Ogre::LogManager::getSingletonPtr()->getDefaultLog()->logMessage(Ogre::String("Bouwplaats max: ") + Ogre::StringConverter::toString(size)); size = ent->getBoundingBox().getMinimum(); Ogre::LogManager::getSingletonPtr()->getDefaultLog()->logMessage(Ogre::String("Bouwplaats min: ") + Ogre::StringConverter::toString(size)); Ogre::Entity* ent1 = sceneMgr->createEntity( "KeetEntity", "keet.mesh" ); Ogre::SceneNode* scenenode = sceneMgr->getRootSceneNode()->createChildSceneNode("KeetNode", Ogre::Vector3(0, 0, 0)); scenenode->attachObject(ent1); Ogre::Entity* ent2 = sceneMgr->createEntity( "HekjeEntity", "hekje.mesh" ); Ogre::SceneNode* scenenode2 = sceneMgr->getRootSceneNode()->createChildSceneNode("HekjeNode", Ogre::Vector3(0, -100, 0)); scenenode2->attachObject(ent2); scenenode2->setScale(Ogre::Vector3(400,0,100)); // most examples get the viewport size to calculate this; for now, we'll just // set it to 4:3 the easy way camera->setAspectRatio((Ogre::Real)1.333333); camera->setPosition(Ogre::Vector3(40,100,10)); guiCamera->setPosition(0, 0, 300); guiCamera->lookAt(node->getPosition()); // this next bit is for the sake of the input handler unsigned long hWnd; window->getCustomAttribute("WINDOW", &hWnd); // set up the input handlers Simulation *sim = new Simulation(); InputHandler *handler = new InputHandler(sim, hWnd, camera); DataManager *dataManager = new DataManager(); GameAI* gameAI = new GameAI(dataManager); //Create Network Network * net = new Network(dataManager); //net->start(); sim->requestStateChange(GUI); gui = new GuiManager(); // networkshit while(!net->isConnected()) { Sleep(1000); } net->Send(GETGROUPS, "", "", NULL); net->Send(LOGIN, "gast", "gast", 1); gui->setSimulation(sim); gui->init("", ogre, guiSceneMgr, window); gui->activate("main"); handler->setWindowExtents(1024,768); SimulationState cState; Ogre::WindowEventUtilities::addWindowEventListener(window, handler); DWORD tFrameStart = 0x0; //in miliseconds signed long tFrameX = 0x0; float tDelta2 = 0.0f; //in seconds float m_fps = 60.0f; tFrameStart = GetTickCount(); float tDelta; //testAI->calculateNextPath(40,10); while ((cState = sim->getCurrentState()) != SHUTDOWN) { tFrameX = GetTickCount() - tFrameStart; tDelta2 = (float)tFrameX / 1000.0f; if (tDelta2 > 3600) // tDelta > 1 hour tDelta2 = 1.0f / m_fps; //< System tick count has highly likely overflowed, so get approximation tFrameStart = GetTickCount(); m_fps = (int)(1.0f / tDelta2); tDelta = tDelta2; handler->capture(); handler->update(tDelta); gui->update(tDelta); //if (sim->getCurrentState() == SIMULATION || sim->getCurrentState() == SIMULATION_MOUSELOOK) // testAI->update(tDelta); // run the message pump (Eihort) Ogre::WindowEventUtilities::messagePump(); ogre->renderOneFrame(); if (sim->getCurrentState() != cState) { handler->StateSwitched(cState, sim->getCurrentState()); switch (sim->getCurrentState()) { case GUI: window->getViewport(0)->setCamera(guiCamera); break; case SIMULATION: window->getViewport(0)->setCamera(camera); break; } } } // clean up after ourselves //delete testAI; delete sim; delete ogre; delete handler; delete gameAI; delete dataManager; return 0; }
static int HandleIncEnd(Network &net) { fclose(current_inc_fd); int ret = 0; send_buf[0] = INC_END_REP; send_buf[1] = 1; FILE *fp = fopen(kCurrentIncFilePath, "rb"); if(fp == NULL) { send_buf[1] = 0; ret = -1; goto clear; } fprintf(files_info_fp, "%s %u %u\n", current_file_path, current_file_compose_offset, current_chunk_count); current_file_compose_offset += current_chunk_count * 10; #ifdef DEBUG_ printf("count: %d\n", current_chunk_count); #endif for(Uint4 i = 0; i < current_chunk_count; i++) { fwrite(&file_chunks[i].container_id, 1, 4, files_compose_fp); fwrite(&file_chunks[i].container_offset, 1, 4, files_compose_fp); fwrite(&file_chunks[i].len, 1, 2, files_compose_fp); if(!file_chunks[i].is_duplication) { #ifdef DEBUG_ printf("%d ", file_chunks[i].len); #endif if(kContainerCapacity - write_container_offset >= file_chunks[i].len) { write_container_offset += file_chunks[i].len; } else { fclose(write_container_fd); write_container_id++; write_container_offset = 0; char tmp_path[kMaxPathLen]; GetContainerPath(write_container_id, tmp_path); write_container_fd = fopen(tmp_path, "a"); if(write_container_fd == NULL) { send_buf[1] = 0; ret = -2; goto clear; } write_container_offset += file_chunks[i].len; } fread(file_buf, 1, file_chunks[i].len, fp); fwrite(file_buf, 1, file_chunks[i].len, write_container_fd); } } send_buf[1] = 1; fclose(fp); #ifdef DEBUG_ printf("\n"); #endif clear: if(net.Send(send_buf, 2) < 0) { ret = -3; } return ret; }
static int HandleChunkInfo(Network &net, ChunkDB &chunk_db) { ChunkInfo chunk; memcpy(&chunk.len, recv_buf+1, 2); string key, value; key.assign(recv_buf+3, 20); //PrintKey(key); total_chunk_bytes += chunk.len; if(chunk_db.Get(key, &value) > 0) { chunk.is_duplication = true; sscanf(value.c_str(), "%u,%u", &chunk.container_id, &chunk.container_offset); file_chunks.push_back(chunk); same_chunk_count++; save_chunk_bytes += chunk.len; } else { chunk.is_duplication = false; if(kContainerCapacity - current_container_offset >= chunk.len) { chunk.container_id = current_container_id; chunk.container_offset = current_container_offset; current_container_offset += chunk.len; } else { //fclose(write_container_fd); current_container_id++; current_container_offset = 0; //char tmp_path[kMaxPathLen]; //GetContainerPath(current_container_id, tmp_path); //write_container_fd = fopen(tmp_path, "a"); //if(write_container_fd == NULL) //return -1; chunk.container_id = current_container_id; chunk.container_offset = current_container_offset; current_container_offset += chunk.len; } char tmp[128]; sprintf(tmp, "%u,%u", chunk.container_id, chunk.container_offset); value.assign(tmp); if(chunk_db.Put(key, value) < 0) return -2; file_chunks.push_back(chunk); } send_buf[0] = CHUNK_INFO_REP; if(net.Send(send_buf, 1) < 0) { return -3; } return 1; }