parm_result init_parameters(void) { int i; struct parameter_table *pat = parmtab; if (mkdir(parm_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) printf("Directory %s already existed !\n", parm_dir); for (i=0;i<PAR_END_MARKER-1;i++) { /* First try to read the parameter */ if (pat->type == PAR_TYP_INT) { int val; parm_result res; if ((res = get_integer (pat->entry, &val)) != PARM_OK ) { if (res == -PARM_NOT_FOUND) { val = atoi (pat->default_value); printf("Parameter not found, writing default value !\n"); if (put_integer(pat->entry, val) != PARM_OK) { printf ("Failed to write default parameter %d !\n", pat->entry); } } else { printf("Fatal error while reading parameter %d !\n", pat->entry); } } } else if (pat->type == PAR_TYP_STRING) { int val; parm_result res; if ((res = get_integer (pat->entry, &val)) != PARM_OK ) { if (res == -PARM_NOT_FOUND) { val = atoi (pat->default_value); printf("Parameter not found, writing default value !\n"); if (put_integer(pat->entry, val) != PARM_OK) { printf ("Failed to write default parameter %d !\n", pat->entry); } } else { printf("Fatal error while reading parameter %d !\n", pat->entry); } } } pat++; } }
// -------------------------------------------------------------------------------------- void Ajp13Socket::OnTransferLimit() { char msg[8192]; msg[0] = 'A'; msg[1] = 'B'; // Send Body Chunk size_t n = m_res.GetFile().fread(msg + 7, 1, 8100); while (n > 0) { int ptr = 4; put_byte(msg, ptr, 0x03); // send body chunk put_integer(msg, ptr, (short)n); ptr += (int)n; short len = htons( ptr - 4 ); memcpy( msg + 2, &len, 2 ); SendBuf( msg, ptr ); if (GetOutputLength() > 1) { SetTransferLimit( 1 ); break; } // n = m_res.GetFile().fread(msg + 7, 1, 8100); } if (!GetOutputLength()) // all body data sent and no data in output buffer - send end response { // End Response int ptr = 4; put_byte(msg, ptr, 0x05); // end response put_boolean(msg, ptr, false); // reuse /* don't reuse - but with m_req.Reset() and m_res.Reset() it should be possible - also reset any AjpBaseSocket/Ajp13Socket specific states */ short len = htons( ptr - 4 ); memcpy( msg + 2, &len, 2 ); SendBuf( msg, ptr ); SetTransferLimit(0); m_res.GetFile().fclose(); OnResponseComplete(); } }
// -------------------------------------------------------------------------------------- void Ajp13Socket::ReceiveBody(const char *buf, size_t sz) { if (sz - 2 > m_body_size_left) { fprintf(stderr, "More body data received than expected\n"); SetCloseAndDelete(); return; } m_req.Write( buf + 2, sz - 2 ); m_body_size_left -= sz - 2; // request more body data if (m_body_size_left) { int ptr = 4; char msg[100]; msg[0] = 'A'; msg[1] = 'B'; // reply codes // 0x3 Send Body Chunk // 0x4 Send Headers // 0x5 End Response // 0x6 Get Body Chunk <------ // 0x9 CPong Reply put_byte(msg, ptr, 0x06); // GET_BODY_CHUNK; put_integer(msg, ptr, 1000); // request 1000 bytes short len = htons( ptr - 4 ); memcpy( msg + 2, &len, 2 ); SendBuf( msg, ptr ); return; } // Close m_req.CloseBody(); // no more body data left to read - execute Execute(); }
/* * Server has asked us to make a choice. * * Ask AI and return result. */ static void handle_choose(char *ptr) { player *p_ptr; char msg[1024]; int pos, type, num, num_special; int list[MAX_DECK], special[MAX_DECK]; int arg1, arg2, arg3; int i; /* Get player pointer */ p_ptr = &real_game.p[player_us]; /* Read choice log position expected */ pos = get_integer(&ptr); /* Check for further along in log than we are */ if (pos > p_ptr->choice_pos) { /* Adjust current position */ p_ptr->choice_size = p_ptr->choice_pos = pos; } /* Check for request for choice we have already made */ else if (pos < p_ptr->choice_pos) { /* XXX Do nothing */ return; } /* Read choice type */ type = get_integer(&ptr); /* Read number of items in list */ num = get_integer(&ptr); /* Loop over items in list */ for (i = 0; i < num; i++) { /* Read list item */ list[i] = get_integer(&ptr); } /* Read number of special items in list */ num_special = get_integer(&ptr); /* Loop over special items */ for (i = 0; i < num_special; i++) { /* Read special item */ special[i] = get_integer(&ptr); } /* Read extra arguments */ arg1 = get_integer(&ptr); arg2 = get_integer(&ptr); arg3 = get_integer(&ptr); /* Ask AI for decision */ ai_func.make_choice(&real_game, player_us, type, list, &num, special, &num_special, arg1, arg2, arg3); /* Start reply */ ptr = msg; /* Begin message */ start_msg(&ptr, MSG_CHOOSE); /* Put choice log position */ put_integer(p_ptr->choice_pos, &ptr); /* Copy entries from choice log */ for (i = p_ptr->choice_pos; i < p_ptr->choice_size; i++) { /* Copy entry */ put_integer(p_ptr->choice_log[i], &ptr); } /* Move current position to end of choice log */ p_ptr->choice_pos = p_ptr->choice_size; /* Finish message */ finish_msg(msg, ptr); /* Send reply */ send_msg(0, msg); }
// -------------------------------------------------------------------------------------- void Ajp13Socket::Respond(const HttpResponse& res) { char msg[8192]; msg[0] = 'A'; msg[1] = 'B'; m_res = res; // reply codes // 0x3 Send Body Chunk // 0x4 Send Headers // 0x5 End Response // 0x6 Get Body Chunk // 0x9 CPong Reply // check content length if (!m_res.ContentLength() && m_res.GetFile().size()) { // m_res.SetContentLength( m_res.GetFile().size() ); } // Send Headers { int ptr = 4; put_byte(msg, ptr, 0x04); // send headers put_integer(msg, ptr, m_res.HttpStatusCode() ); put_string(msg, ptr, m_res.HttpStatusMsg() ); put_integer(msg, ptr, (short)m_res.Headers().size() ); for (Utility::ncmap<std::string>::const_iterator it = m_res.Headers().begin(); it != m_res.Headers().end(); ++it) { Utility::ncmap<int>::const_iterator it2 = Init.ResponseHeader.find( it -> first ); if (it2 != Init.ResponseHeader.end()) { put_integer(msg, ptr, it2 -> second); } else { put_string(msg, ptr, it -> first); } put_string(msg, ptr, it -> second); } std::list<std::string> vec = m_res.CookieNames(); { for (std::list<std::string>::iterator it = vec.begin(); it != vec.end(); it++) { Utility::ncmap<int>::const_iterator it2 = Init.ResponseHeader.find( "set-cookie" ); if (it2 != Init.ResponseHeader.end()) { put_integer(msg, ptr, it2 -> second); } else { put_string(msg, ptr, "set-cookie"); } put_string(msg, ptr, m_res.Cookie(*it) ); } } short len = htons( ptr - 4 ); memcpy( msg + 2, &len, 2 ); SendBuf( msg, ptr ); } // Send Body Chunk OnTransferLimit(); }