Ejemplo n.º 1
0
int UnixBusComm::recvDetail(void * head, int head_len, void * data, int data_len)
{
    int ret;
    char * recv_head = NULL;
    char * recv_data = NULL;

    if (this->isConnectServer != 1)
    {
        cout<<"not connect to server 1"<<endl;
        return -1;
    }

    recv_head = (char *)malloc(head_len);
    recv_data = (char *)malloc(data_len);
    ret = comm_read(this->localFd, recv_head, head_len);
    ret = comm_read (this->localFd, recv_data, data_len);

    if (head != NULL)
    {
        ::memcpy (head, recv_head, head_len);
    }
    if (data != NULL)
    {
        ::memcpy (data, recv_data, data_len);
    }

    free(recv_head);
    free(recv_data);

    return 0;
}
Ejemplo n.º 2
0
Archivo: p2.c Proyecto: fpozzas/fic
int procesar(char ** trozos){
    if (trozos[0]==NULL) return 0;
    else if (strcmp(trozos[0],"exit")==0) comm_exit();
    else if (strcmp(trozos[0],"quit")==0) comm_exit();
    else if (strcmp(trozos[0],"autores")==0) comm_autores();
    else if (strcmp(trozos[0],"chdir")==0) comm_chdir(trozos);
    else if (strcmp(trozos[0],"prompt")==0) comm_prompt(trozos);
    else if (strcmp(trozos[0],"pid")==0) comm_pid(trozos);
    else if (strcmp(trozos[0],"fork")==0) comm_fork();
    else if (strcmp(trozos[0],"malloc")==0) comm_malloc(trozos);
	else if (strcmp(trozos[0],"display")==0) comm_display(trozos);
	else if (strcmp(trozos[0],"free")==0) comm_free(trozos);
	else if (strcmp(trozos[0],"stat")==0) comm_stat(trozos);
	else if (strcmp(trozos[0],"delete")==0) comm_delete(trozos);	
	else if (strcmp(trozos[0],"list")==0) comm_list(trozos);	
	else if (strcmp(trozos[0],"mmap")==0) comm_mmap(trozos);	
	else if (strcmp(trozos[0],"munmap")==0) comm_munmap(trozos);	
	else if (strcmp(trozos[0],"read")==0) comm_read(trozos);	
	else if (strcmp(trozos[0],"write")==0) comm_write(trozos);	
	else if (strcmp(trozos[0],"rec")==0) comm_rec(trozos);	
	else if (strcmp(trozos[0],"shared-creat")==0) comm_sharedcreat(trozos);	
	else if (strcmp(trozos[0],"shared-del")==0) comm_shareddel(trozos);	
	else if (strcmp(trozos[0],"detach")==0) comm_detach(trozos);	
	else if (strcmp(trozos[0],"shared-info")==0) comm_sharedinfo(trozos);	
 	else if (strcmp(trozos[0],"shared-cp")==0) comm_sharedcp(trozos);	
 	else if (strcmp(trozos[0],"shared-rm")==0) comm_sharedrm(trozos[1]);	
 	else if (strcmp(trozos[0],"direcciones")==0) comm_direcciones(trozos);	
 	else if (strcmp(trozos[0],"shared-mv")==0){
 		if (trozos[1]==NULL) {printf("Numero de parametros incorrectos.\n"); return -1;}
 		char arg[strlen(trozos[1])];
 		strcpy(arg,trozos[1]);
 		if ((comm_sharedcp(trozos))==0)
 			comm_sharedrm(arg);
 	}
 	// Comandos de p2
 	else if (strcmp(trozos[0],"path")==0) comm_path(trozos);	
 	else if (strcmp(trozos[0],"exec")==0) comm_exec(trozos+1);	
 	else if (strcmp(trozos[0],"getpriority")==0) comm_getpriority(trozos);	
 	else if (strcmp(trozos[0],"setpriority")==0) comm_setpriority(trozos);	
 	else if (strcmp(trozos[0],"jobs")==0) comm_jobs(trozos,IMPRIMIR); 	
	else if (strcmp(trozos[0],"cleanjobs")==0) comm_jobs(trozos,BORRAR);
	else comm_com(trozos);
}
Ejemplo n.º 3
0
Archivo: dfs.c Proyecto: bwhite/dfs
static void *listener(void *arg) 
{
    int		fd = *(int *)arg;

    dfs_out("\n\tLISTEN PROC IN, sock %d!\n\n", fd);
    
    Msg *m;
    while (m = comm_read(1, fd)) {

	switch (m->type) {
	case DFS_MSG_PUSH_LOG:
	    /* Lock tree and replay log.  Assumes we get the whole log.  */
	    pthread_mutex_lock(&treeMut);
	    pthread_mutex_lock(&replyLogserverMut);
	    dfs_out("Push calling play log\n");
	    char *log;
	    size_t log_sz;
	    assert(tuple_unserialize_log(&log, &log_sz, m->data, m->len) == 0);
	    playLog(log, log_sz);
	    free(log);
	    pthread_mutex_unlock(&replyLogserverMut);
	    pthread_mutex_unlock(&treeMut);
	    break;

	case MSG_REPLY:
	    /* Put on reply queue.  Waits until main thread gets the item
	     and sets the replyQueue to NULL to prevent losing messages */
	    pthread_mutex_lock(&replyLogserverMut);
	    assert(replyQueue == NULL);
	    replyQueue = m;
	    pthread_cond_signal(&replyLogserverCond);
	    pthread_cond_wait(&replyLogserverCond, &replyLogserverMut);
	    pthread_mutex_unlock(&replyLogserverMut);
	    break;
	default:
	    dfs_die("BAD MSG TYPE %d\n", m->type);
	}
    }
    
    dfs_out("\n\tLISTEN PROC EXIT, sock %d!\n\n", fd);
    return NULL;
}
Ejemplo n.º 4
0
void
client_read_cb(int fd, struct fde_comm *fc, void *arg, fde_comm_cb_status s,
    int retval)
{
	struct conn *c = arg;

#ifdef DO_DEBUG
	fprintf(stderr, "%s: FD %d: %p: s=%d, ret=%d\n",
	    __func__,
	    fd,
	    c,
	    s,
	    retval);
#endif

	/*
	 * If we've hit an error, do a comm_close() and then wait until we
	 * get notification for that.
	 */
	if (s != FDE_COMM_CB_COMPLETED) {
		if (s != FDE_COMM_CB_EOF) {
			fprintf(stderr, "%s: %p: FD %d: error; status=%d errno=%d\n",
			    __func__,
			    c,
			    fc->fd,
			    s,
			    errno);
		}

		/* Notify upper layer about an error */
		/* XXX not distinguishing between read/write errors? */
		if (c->cb.cb)
			c->cb.cb(c, c->cb.cbdata, CONN_STATE_ERROR);
		return;
	}

	if (c->stats_cb.cb != NULL)
		c->stats_cb.cb(c, c->stats_cb.cbdata, 0, retval);

	/* register for another read */
	(void) comm_read(c->comm, c->r.buf, c->r.size, client_read_cb, c);
}
Ejemplo n.º 5
0
void protocol_poll()
{
	uint8_t b;
	while (comm_available() && comm_read(&b)) {
		if (_err > 0 && b == START) {
			// recover from error condition
			status_error(0x00);
			_err = 0;
			_pos = 0;
		} else if (_err > 0) {
			continue;
		}
		
		if (_pos > 0 && b == START) {
			// unexpected start of frame
			status_error(STATUS_ERR_PROTOCOL);
			_err = 1;
			continue;
		}
		if (_pos > 0 && b == ESCAPE) {
			// unescape next byte
			_esc = 1;
			continue;
		}
		if (_esc) {
			// unescape current byte
			b = 0x20 ^ b;
			_esc = 0;
		}
		if (_pos > 1) { // start byte and length byte not included in checksum
			_chk += b;
		}
		
		switch(_pos) {
			case 0: // start frame
				_pos++;
				continue;
			case 1: // length
				_len = b;
				_pos++;
				continue;
			case 2:
				_api = b;
				_pos++;
				continue;
			default:
				if (_pos > MAX_SIZE) continue; // this probably can't happen since the xbee packet size is larger than any of our messages
				if (_pos == (_len + 2)) {
					if (_chk == 0xff) {
						_protocol_dispatch(_api, _len - 1);
					} else {
						status_error(STATUS_ERR_PROTOCOL);
						_err = 1;
					}
					_pos = 0;
					_chk = 0;
				} else {
					_buf[_pos++ - 3] = b;
				}
		}
	}
}
Ejemplo n.º 6
0
struct conn *
conn_new(struct fde_head *h, struct cfg *cfg, struct shm_alloc_state *sm,
    int fd, conn_owner_update_cb *cb, void *cbdata)
{
	struct conn *c;
	char *buf;
	int i;
	int sn;

	c = calloc(1, sizeof(*c));
	if (c == NULL) {
		warn("%s: calloc", __func__);
		return (NULL);
	}

	c->r.size = cfg->io_size;
	c->r.buf = malloc(c->r.size);
	if (c->r.buf == NULL) {
		warn("%s: malloc", __func__);
		free(c);
		return (NULL);
	}

	c->w.nb = iapp_netbuf_alloc(sm, cfg->atype, cfg->io_size);
	if (c->w.nb == NULL) {
		warn("%s: iapp_netbuf_alloc", __func__);
		free(c->r.buf);
		free(c);
		return (NULL);
	}

	/* Pre-populate with some data */
	buf = iapp_netbuf_buf_nonconst(c->w.nb);
	for (i = 0; i < iapp_netbuf_size(c->w.nb); i++) {
		buf[i] = (i % 10) + '0';
	}

	/*
	 * Limit the send size to one buffer for now.
	 *
	 * This isn't optimal but until we queue multiple buffers
	 * via sendfile, we will end up queueing the same memory region
	 * over and over again via different mbufs to the same socket
	 * and that isn't at all useful or correct.
	 *
	 * Once the shm allocator handles returning buffers, we can
	 * modify the transmit path to allocate buffers as required and
	 * then keep up to two in flight.  Then we can just remove
	 * this limit.
	 */
	sn = cfg->io_size;
	if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sn, sizeof(sn)) < 0)
		warn("%s: setsockopt(SO_SNDBUF)", __func__);

	c->fd = fd;
	c->comm = comm_create(fd, h, client_ev_close_cb, c);
	c->ev_cleanup = fde_create(h, -1, FDE_T_CALLBACK, 0,
	    client_ev_cleanup_cb, c);
	c->state = CONN_STATE_RUNNING;

	/* Link back to the parent fde loop */
	c->h = h;

	/* .. and the callback state for notification */
	c->cb.cb = cb;
	c->cb.cbdata = cbdata;

#if 0
	/*
	 * Start reading!
	 */
	(void) comm_read(c->comm, c->r.buf, c->r.size, client_read_cb, c);
#endif

	/*
	 * Start writing!
	 */
	comm_write(c->comm, c->w.nb, 0, iapp_netbuf_size(c->w.nb), conn_write_cb, c);

	return (c);
}