Example #1
0
int serverWrite(event *ev, int fd) {
	lookup_log(LOG_DEBUG, "write\n");
	int write = 0;
	if (ev->wr_pos == 0) {
		ev->body_data = build_request_str(ev->data);
	}
	write = anetWrite(fd, ev->body_data, ev->data->body_len  + PROTOCOL_HAEDER_SIZE);
	if (write == 0 && errno == ECONNRESET) {
		lookup_log(LOG_DEBUG, "client close conn\n");
	}
	ev->wr_pos == 0;
	if (del_event(pool, fd) < 0) {
		printf("%s\n", strerror(errno));
		exit(0);
	}

	// dump_request(ev->data);

	int tmp = 0;
	int i = 0;
	while (tmp < ev->data->body_len) {
		lookup_log(LOG_DEBUG, "free:%s\n", ev->data->body[i].data);
		zfree(ev->data->body[i].data);
		tmp += ev->data->body[i].len + 3;
		zfree(ev->data->body[i]);
		i++;
	}	
	close(fd);
	zfree(ev->data);
	zfree(ev->body_data);
	lookup_log(LOG_DEBUG, "close fd\n");
	return 0;
}
Example #2
0
int serverRead(event *ev, int fd) {
	lookup_log(LOG_DEBUG, "read\n");
	int read = 0;
	char buf[IO_PACKET_SIZE];

	if (ev->wr_pos == 0) {
		read = anetRead(fd, &buf[0], PROTOCOL_HAEDER_SIZE);
		if (read == 0 && errno == ECONNRESET) {
free_conn:
			ev->wr_pos = 0;
			del_event(pool, fd);	
			close(fd);
			zfree(ev->data);
			lookup_log(LOG_DEBUG, "close fd\n");
			return 0;
		}
		ev->data = parse_protocol_header(buf);
	} else {
		int block = (ev->data->body_len + PROTOCOL_HAEDER_SIZE - ev->wr_pos) 
			/ IO_PACKET_SIZE;
		if (block > 0) {
			read = anetRead(fd, &buf[0], IO_PACKET_SIZE);			
		} else {
			read = anetRead(fd, &buf[0], 
				(ev->data->body_len + PROTOCOL_HAEDER_SIZE - ev->wr_pos) % IO_PACKET_SIZE);			
		}
		if (read == 0 && errno == ECONNRESET) {
			goto free_conn;
		}
		if (ev->body_data == NULL) {
			ev->body_data_size = IO_PACKET_SIZE * 2;
			ev->body_data = (char *)zmalloc(ev->body_data_size);
			memcpy(ev->body_data + ev->body_data_wr_pos, buf, read);
			ev->body_data_wr_pos += read;
		} else {
			if ((ev->body_data_wr_pos + read) > ev->body_data_size) {
				//resize
				ev->body_data_size = ev->body_data_size * 2;
				ev->body_data = (char *)zrealloc(ev->body_data, 
					ev->body_data_size);
			}
			memcpy(ev->body_data + ev->body_data_wr_pos, buf, read);
			ev->body_data_wr_pos += read;
		}
	}
	if (read > 0) {
		ev->wr_pos += read;	
	}
	lookup_log(LOG_DEBUG, "pos:%d\n", ev->wr_pos);
	if (ev->wr_pos >= (ev->data->body_len +  PROTOCOL_HAEDER_SIZE)) {
		parse_protocol_body(ev->body_data, ev->data);
		ev->wr_pos = 0;
		processRequest((lookup_protocol *)ev->data);
		ev->cb = serverWrite;
		set_event(pool, fd, EPOLLOUT);
	}
	return 0;
}
Example #3
0
u_short
del_event_func(void (*func)())
{
	event_t *event = main_event;

	for (; event; event = event->next)
		if (event->func == func) {
			del_event(event);
			return 1;
		}
	return 0;
}
Example #4
0
void track::update_event(unsigned short int row, event *src) {
    int note = src->note;
    int inst = src->inst;
    int vol = src->vol;
    int length = src->length;
    int effect = src->effect;
    int effect_data = src->effect_data;
    event *e;
    int a;

    if (lock_mutex(song->hEditMutex, EDIT_LOCK_TIMEOUT)) {
        

        if (e = get_event(row))
            a=0;
        else {
            e = new event;
            e->row = row;
            a=1;
        }
        if (note>=0)
            e->note = (unsigned char)note;
        if (vol>=0)
            e->vol = (unsigned char)vol;
        if (inst>=0)
            e->inst = (unsigned char)inst;
        if (length>=0)
            e->length = (unsigned short int)length;
        if (effect>=0)
            e->effect = (unsigned char)effect;
        if (effect_data>=0)
            e->effect_data = (unsigned short int)effect_data;
        if (a) {
            if (e->blank() || row>=this->length) 
                delete e;
            else {
                e->next_event = event_list;
                event_list = e;
            }
        } else {
            if (e->blank())
                del_event(row,0);
        }
        file_changed++;
        unlock_mutex(song->hEditMutex);
    }
}
Example #5
0
void track::update_event_safe(unsigned short int row, int note, int inst, int vol, int length, int effect, int effect_data) {
    event *e;
    int a;
    if (lock_mutex(song->hEditMutex, EDIT_LOCK_TIMEOUT)) {

        if (row>=this->length) row=this->length-1;

        if (e = get_event(row))
            a=0;
        else {
            e = new event;
            e->row = row;
            a=1;
        }
    
        if (note>=0)
            e->note = (unsigned char)note;
        if (vol>0x7f) vol=0x80;
        if (vol>=0)
            e->vol = (unsigned char)vol;
        if (inst>=ZTM_MAX_INSTS) inst=ZTM_MAX_INSTS-1;
        if (inst>=0)
            e->inst = (unsigned char)inst;
        if (length>999)length=999;
        if (length>=0)
            e->length = (unsigned short int)length;
        if (effect>=0)
            e->effect = (unsigned char)effect;
        if (effect_data>=0)
            e->effect_data = (unsigned short int)effect_data;
        if (a) {
            if (e->blank() || row>=this->length) 
                delete e;
            else {
                e->next_event = event_list;
                event_list = e;
            }
        } else {
            if (e->blank())
                del_event(row,0);
        }
        file_changed++;
        unlock_mutex(song->hEditMutex);
    }
}
Example #6
0
int add_reconn(conn_t* conn){
	
	int sockfd = conn->sockfd;
	int need = 0;
	int original_state ;
	int rc;
	
	pthread_mutex_lock(&conn->lock);
	original_state = conn->conn_state;
	if(conn->conn_state == CONNECTED){
		debug_info("closed sockfd[%d]\n", sockfd);
		rc = close(sockfd);
		check(rc);
		/*
		if(conn->conn_type == ACCEPT){
			rc = close(sockfd);
			check(rc);
		}else{
			rc =shutdown(sockfd,SHUT_RDWR);
			check(rc);
		}*/
		need = 1;
	}else if(conn->conn_state == CLOSED){  //when active connect failed
		need = 1;	
	}//other COONECING

	conn->conn_state = CONNECTING;
	pthread_mutex_unlock(&conn->lock);

	assert(conn->conn_state == CONNECTING);
	//
	del_event(conn);

	
	if(need && conn->conn_type == ACTIVE){
		debug_info("add to reconn queue, conn->conn_type[%d], original state[%d]\n",
					conn->conn_type, original_state);
		enque(&g_ctx.reconn_q, &conn->reconn_link);
	}else{		//accept connect, do nothing ,but wait for be connected
		debug_info("accept connect do nothing, conn->conn_type[%d], original state[%d]\n",
					conn->conn_type, original_state);
	}
}
Example #7
0
void
do_events(time_t now)
{
	event_t *event = main_event;

	while (event) {
		if (event->when <= now) {
			(*event->func)();
			if (event->often != 0)
				event->when = now + event->often;
			else
				event->when = 0;
		}
		if (event->when == 0)
			event = del_event(event);
		else
			event = event->next;
	}
}
Example #8
0
void track::del_row(unsigned short int which) {
    event *e;
/*
    int timeout = 0;
    while(ztPlayer->edit_lock && timeout<EDIT_LOCK_TIMEOUT) {
        timeout++; SDL_Delay(1);
    } if (ztPlayer->edit_lock) return;
    ztPlayer->edit_lock = 1;
*/
    del_event(which,0);
    e = event_list;
    while(e) {
        if (e->row>which)
            e->row--;
        e = (event *)e->next_event;
    }
    file_changed++;

//  ztPlayer->edit_lock = 0;
}