Beispiel #1
0
static int login_job(int sd, struct packet_st *pkt_r, int recv_len, struct sockaddr_in *hisend)
{
	uint32_t id;
	struct shadow_st sdw;
	int ret;
	int id_exist;
	struct packet_st pkt;
	struct database_record_st record, *recordp;

	debug("call %s\n", __func__);

	char ip_buf[32];
	inet_ntop(AF_INET, &hisend->sin_addr, ip_buf, 32);
	debug("in %s(), hisip: %s, hisport: %d\n", __func__, ip_buf, ntohs(hisend->sin_port));

	/* check minor */
	id = ntohl(pkt_r->id);

	id_exist = get_shadow(id, &sdw);

	debug("in %s(), %d, %s\n%s\n", __func__, sdw.id, sdw.salt, sdw.encrypt);

	ret = snprintf(pkt.salt, PADSIZE, "%s", sdw.salt);
	pkt.major = MAJOR_LOGIN;
	pkt.minor = 2;
	/* timeout ? */
	debug("in %s(), will sendto, ret = %d\n", __func__, ret);
	ret = sendto(sd, &pkt, ret + 2, 0, (struct sockaddr *)hisend, sizeof(*hisend));
	debug("in %s(), sendto() return %d\n", __func__, ret);
	perror("sendto()");

	ret = recvfrom(sd, &pkt, sizeof(pkt), 0, NULL, NULL);
	/* check src addr, major, minor */
	pkt.encrypt[ret - 2] = '\0';
	debug("%s\n%s\n", pkt.encrypt, sdw.encrypt);
	if (id_exist == 0 || strcmp(pkt.encrypt, sdw.encrypt) != 0) {
		/* fail */
		pkt.major = MAJOR_LOGIN;
		pkt.minor = 4;
		pkt.ack = 1;
		sendto(sd, &pkt, 3, 0, (struct sockaddr *)hisend, sizeof(*hisend));
	} else {
		/* success */
		pkt.major = MAJOR_LOGIN;
		pkt.minor = 4;
		pkt.ack = 0;

		/* update database */
		recordp = database_find(id);
		if (recordp == NULL) {
			record.id = id;
			memcpy(&record.addr, hisend, sizeof(struct sockaddr_in));
			record.last = time(NULL);
			record.online = 1;
			database_insert(&record);
			/* if error */
		} else {
			memcpy(&recordp->addr, hisend, sizeof(struct sockaddr_in));
                        recordp->last = time(NULL);
                        recordp->online = 1;
		}

		sendto(sd, &pkt, 3, 0, (struct sockaddr *)hisend, sizeof(*hisend));
	}

	return 0;
}
Beispiel #2
0
/**
 * \brief Tell if the element must be displayed with a shadow.
 */
bool bear::visual::scene_element::has_shadow() const
{
  return ( (get_shadow().x != 0) || (get_shadow().y != 0) )
    && (get_shadow_opacity() != 0);
} // scene_element::has_shadow()
/************************************************************************
 * This is the recursive ray tracer - you need to implement this!
 * You should decide what arguments to use.
 ************************************************************************/
vec3 recursive_ray_trace(vec3 eye, vec3 ray, int num, bool inobj) {
//
// do your thing here
//
	if(num>step_max) return null_clr;
	vec3 hit;
	int isplane;
	void *sph = intersect_scene(eye, ray, scene, &hit, &isplane);

	vec3 color = null_clr;
	if(sph==NULL) { 
		return background_clr;
	}

	vec3 lightvec = light1 - hit;
	vec3 lightvec_normal = normalize(lightvec);
	vec3 lighthit;
	int lightisplane;
	void * light_sph = intersect_scene(hit, lightvec_normal, scene, &lighthit, &lightisplane);
	
	vec3 surf_normal = isplane?vec3(0,1,0):sphere_normal(hit, (Spheres*)sph);

	if(light_sph==NULL) {
		color += phong(-1*ray, lightvec, surf_normal, sph, hit, isplane);
	}
	else {
		if(!shadow_on) {
			color += phong(-1*ray, lightvec, surf_normal, sph, hit, isplane);
		}
		else {
			color += get_shadow(-1*ray, lightvec, surf_normal, sph, hit, isplane);
		}
	}

	if(reflect_on) {
		vec3 reflect_vector = 2*dot(-1*ray, surf_normal)*surf_normal + ray;
		reflect_vector = normalize(reflect_vector);
		if(isplane) {
			color += ((struct plane*)sph)->reflectance * recursive_ray_trace(hit, reflect_vector,num+1, inobj);
		}
		else if(!isplane)
			color += ((Spheres *)sph)->reflectance * recursive_ray_trace(hit, reflect_vector, num+1, inobj);
	}
	
	if(refract_on) {
		vec3 outlightvector; 
		if(refraction(hit, -1*ray, sph, isplane, inobj, &outlightvector)) {
			if(!isplane) {
//				printf("refraction point\n");
				Spheres * refractsph = (Spheres *)sph;
				color += refractsph->refr*recursive_ray_trace(hit, outlightvector, num+1, !inobj);
			}
		}
	}
	
	if(diffuse_reflection_on && num<2) {
		int i;
		for (i=0;i<DIFFUSE_REFLECTION;i++) {
			float xtheta = 2.0 * static_cast <float> (rand()) / static_cast <float> (RAND_MAX) - 1.0;
			float ytheta = 2.0 * static_cast <float> (rand()) / static_cast <float> (RAND_MAX) - 1.0;
			float ztheta = 2.0 * static_cast <float> (rand()) / static_cast <float> (RAND_MAX) - 1.0;
			vec3 dfray;
			dfray = rotateX(xtheta*M_PI,surf_normal);
			dfray = rotateY(ytheta*M_PI,dfray);
			dfray = rotateZ(ztheta*M_PI,dfray);
			color += (0.1/DIFFUSE_REFLECTION)*recursive_ray_trace(hit, dfray, num+1, inobj);
		}
	}

	return color;

}