Exemplo n.º 1
0
int
qnet_tcp_recv(qsocket_t *socket, int *error) {
  int                 fd, save;
  int                 nbytes, size, n;
  qbuffer_t          *buffer;

  fd = socket->fd;
  buffer = socket->in;

  nbytes = 0;
  /*
   * recv data from tcp socket until:
   *  1) some error occur or
   *  2) received data less then required size
   *    (means there is no data in the tcp stack buffer)
   */
  do {
    if (qbuffer_wlen(buffer) == 0) {
      qbuffer_extend(buffer, buffer->size * 2);
    }
    if (buffer->data == NULL) {
      return QERROR;
    }

    size = qbuffer_wlen(buffer);
    do {
      n = recv(fd, qbuffer_writeable(buffer), size, 0);
      save = errno;
    } while (save == EINTR);

    if (n == -1) { 
      if(save == EAGAIN || save == EWOULDBLOCK) {
        /* non-blocking mode, there is no data in the buffer now */
        break;
      } else if (save != EINTR) {
        /* some error has occured */
        qinfo("recv from %s error: %s", socket->peer, strerror(save));
        *error = save;
        return QERROR;
      }
    }

    /* socket has been closed */
    if (n == 0) {
      qinfo("socket from %s closed", socket->peer);
      *error = save;
      return QERROR;
    }

    buffer->end += n;
    buffer->data[buffer->end] = '\0';
    nbytes += n;
    qinfo("%s recv:%d, total:%d", socket->peer, n, nbytes);
    /* while more tcp stack buffer data available continue */
  } while (n == size);

  return nbytes;
}
Exemplo n.º 2
0
static int
worker_spawn_handler(qmsg_t *msg, void *reader) {
  int             ret;
  qactor_t       *actor;
  qworker_t      *worker;
  qwmsg_spawn_t  *spawn;

  qinfo("handle spawn msg");

  spawn  = (qwmsg_spawn_t*)msg;
  worker = (qworker_t*)reader;
  actor = spawn->actor;
  actor->state = spawn->state;
  actor->tid = worker->tid;
  lua_State *state = actor->state;
  qlua_dump_table(state, 1);
  if (qlua_call(state, 1, 0) == 0) {
    ret = (int)lua_tonumber(state, -1);
    lua_pop(state, 1 );
  } else {
    qlua_fail(state);
  }

  return ret;
}
Exemplo n.º 3
0
int
qnet_tcp_send(qsocket_t *socket, int *error) {
  int                 fd, nbytes;
  int                 save, n, size;
  qbuffer_t          *buffer;

  fd = socket->fd;
  buffer = socket->out;

  size = qbuffer_rlen(buffer);
  nbytes = 0;
  while(size > 0) {
    do {
#ifdef USE_LINUX      
      n = send(fd, qbuffer_readable(buffer), size, MSG_NOSIGNAL);
#elif defined USE_MACOSX
      n = send(fd, qbuffer_readable(buffer), size, 0);
#endif
      save = errno;
    } while (save == EINTR);

    if (n == -1) {
      if (save == EAGAIN || save == EWOULDBLOCK) {
        return nbytes;
      } else {
        qinfo("send to %s error: %s", socket->peer, strerror(save));
        *error = save;
        return QERROR;
      }
    }

    if (n == 0) {
      qinfo("socket from %s closed", socket->peer);
      *error = save;
      return QERROR;
    }

    buffer->start += n;
    nbytes += n;
    size -= n;
    qinfo("%s sent:%d, total:%d", socket->peer, n, nbytes);
  }

  return nbytes;
}
Exemplo n.º 4
0
static int
qllog(lua_State *state) {
  const char* str;
  
  str = lua_tostring(state, 1);
  qinfo("%s", str);

  return 0;
}
Exemplo n.º 5
0
static int
worker_start_handler(qmsg_t *msg, void *reader) {
  qwmsg_start_t  *start;
  qworker_t      *worker;
  int             ret;
  qid_t           aid;
  qactor_t       *actor;
  lua_State      *state;

  qinfo("handle start msg");

  worker = (qworker_t*)reader;
  start  = (qwmsg_start_t*)msg;
  aid    = start->aid;
  actor  = qactor_new(aid);
  if (actor == NULL) {
    qerror("new actor: %d error", aid);
    return QERROR;
  }
  qassert(actor->state == NULL);
  qactor_attach(actor, qlua_new_thread(worker, &actor->ref));
  actor->tid = worker->tid;

  if (qlua_threadloadfile(actor, actor->state, config.main) != 0) {
    qerror("load server start script %s error", config.main);
    return QERROR;
  }

  state = actor->state;
  lua_getglobal(state, "server");
  if (lua_isnil(state, -1)) {
    qerror("load server script server table error");
    return QERROR;
  }
  lua_getfield(state, -1, "start");
  if (lua_isnil(state, -1)) {
    qerror("load server script server.start func error");
    return QERROR;
  }
  if (qlua_call(state, 0, 0) == 0) {
    ret = (int)lua_tonumber(state, -1);
    lua_pop(state, 1 );
  } else {
    qlua_fail(state);
  }

  return ret;
}
Exemplo n.º 6
0
//------------------------------------------------------------------------
void CGameRules::ServerHit(const HitInfo &hitInfo)
{
	HitInfo info(hitInfo);

	if (IItem *pItem=gEnv->pGame->GetIGameFramework()->GetIItemSystem()->GetItem(info.weaponId))
	{
		if (CWeapon *pWeapon=static_cast<CWeapon *>(pItem->GetIWeapon()))
		{
			float distance=0.0f;

			if (IEntity *pEntity=gEnv->pEntitySystem->GetEntity(info.shooterId?info.shooterId:info.weaponId))
			{
				distance=(pEntity->GetWorldPos()-info.pos).len2();
				if (distance>0.0f)
					distance=cry_sqrtf_fast(distance);
			}

			info.damage=pWeapon->GetDamage(info.fmId, distance);
			
			if (info.type!=GetHitTypeId(pWeapon->GetDamageType(info.fmId)))
			{
				info.damage=0;
			}
		}
	}

	if (m_processingHit)
	{
		m_queuedHits.push(info);
		return;
	}

	++m_processingHit;

	ProcessServerHit(info);

	while (!m_queuedHits.empty())
	{
		HitInfo qinfo(m_queuedHits.front());
		ProcessServerHit(qinfo);
		m_queuedHits.pop();
	}

	--m_processingHit;
}
Exemplo n.º 7
0
static qsocket_t*
new_tcp_socket(int fd, lua_State *state, qactor_t *actor,
               struct sockaddr_in *remote) {
  qsocket_t *socket;

  socket = qsocket_new(fd, actor);
  if (socket == NULL) {
    return NULL;
  }

  socket->state = QINET_STATE_CONNECTED;
  memcpy(&(socket->remote), remote, sizeof(*remote));
  inet_ntop(AF_INET, &remote->sin_addr,
            socket->addr, sizeof(socket->addr));
  socket->port = ntohs(remote->sin_port);
  snprintf(socket->peer, sizeof(socket->peer),
           "%s:%d:%d", socket->addr, socket->port, fd);
  qinfo("accept connection from %s", socket->peer);

  return socket;
}
Exemplo n.º 8
0
int main(int argc, char* argv[])
{
	int sc = qinfo(qname);

//	printf("qinfo: %d\n", sc);
	printf("syntax: %s 1 (do not start a consumer)\n", argv[0]);
	printf("syntax: %s (starts a consumer)\n", argv[0]);

	if (argc == 1) {
		sc = create_consumer(true);
		printf("create_consumer: %d\n", sc);
	}

//	dump_headers();

	process();

	sc = remove_consumer();
	printf("remove_consumer: %d\n", sc);

	return 0;
}
Exemplo n.º 9
0
int
qnet_tcp_accept(int listen_fd, struct sockaddr *addr,
                socklen_t *addrlen, int *error) {
  int fd;

  while (1) {
    fd = accept(listen_fd, addr, addrlen);
    if (fd == -1) { 
      if (errno == EAGAIN || errno == EWOULDBLOCK) {
        return QOK;
      }
      if (errno == EINTR) {
        continue;
      }
      *error = errno;
      qinfo("accept error: %s", strerror(*error));
      return QERROR;
    }
    set_nonblocking(fd);
    break;
  }
  return fd;
}