Ejemplo n.º 1
0
void
client_test() {
  char bytes[1024];
  int sock = make_client_socket("127.0.0.1", 6379);
  int n = write(sock, "*2\r\n$3\r\nGET\r\n$1\r\na\r\n", 20);
  debug_log("%d\n", n);
  n = read(sock, bytes, 1024);
  bytes[n] = '\0';
  debug_log("%s\n", bytes);
  close(sock);
}
Ejemplo n.º 2
0
// [-2, +1, e]
static int
lcf_socket_new(lua_State *L) {
  const char *host = luaL_checkstring(L, 1);
  int port = luaL_checkinteger(L, 2);

  lbind_checkregvalue(L, LRK_WORKER_CTX, LUA_TLIGHTUSERDATA,
                      "no worker ctx");
  as_mw_worker_ctx_t *ctx = (as_mw_worker_ctx_t *)lua_touserdata(L, -1);

  lbind_get_thread_local_vars(L, 1, LLK_THREAD);
  as_thread_t *th = (as_thread_t *)lua_touserdata(L, -1);

  as_lm_socket_t *sock = (as_lm_socket_t *)lua_newuserdata(
      L, sizeof(as_lm_socket_t));
  sock->res = NULL;
  sock->type = LM_SOCK_TYPE_CUSTOM;

  luaL_getmetatable(L, LM_SOCKET);
  lua_setmetatable(L, -2);

  sock->res = (as_thread_res_t *)memp_alloc(
      ctx->mem_pool, sizeof_thread_res(int));
  if (sock->res == NULL) {
    lua_pushstring(L, "alloc error");
    lua_error(L);
  }

  int fd = make_client_socket(host, port);
  if (fd < 0) {
    memp_recycle(sock->res);
    lua_pushstring(L, "connect error");
    lua_error(L);
  }
  set_non_block(fd);
  *((int *)sock->res->d) = fd;
  asthread_res_init(sock->res, res_free_f, res_fd_f);
  asthread_res_add_to_th(sock->res, th);
  asthread_res_ev_init(sock->res, ctx->epfd);

  return 1;
}
Ejemplo n.º 3
0
void client()
{
	ctx_t *ctx = make_ctx(SIZE);
	ctx->sock = make_client_socket();
	ctx->who = CLIENT;

	/* both delay */
	ctx->mili = unit;
	do_client_run(ctx);

	/* server delay */
	ctx->mili = 0;
	do_client_run(ctx);

	/* client delay */
	ctx->mili = unit;
	do_client_run(ctx);

	shutdown(ctx->sock, 0);
	free_ctx(ctx);
	return;
}
Ejemplo n.º 4
0
static int a2dp_connect(snd_pcm_a2dp_t * a2dp)
{
	if (a2dp->sk <= 0) {
		int sockfd = make_client_socket();
		a2dp->sk = -1;
		if (sockfd > 0) {
			int32_t client_type = A2DPD_PLUGIN_PCM_WRITE;
			if (send_socket(sockfd, &client_type, sizeof(client_type)) == sizeof(client_type)) {
				// Fill stream informations
				AUDIOSTREAMINFOS StreamInfos = INVALIDAUDIOSTREAMINFOS;
				StreamInfos.rate = a2dp->rate;
				StreamInfos.channels = a2dp->channels;
				StreamInfos.bitspersample = a2dp->frame_bytes/a2dp->channels;
				switch(a2dp->io.format) {
				case SND_PCM_FORMAT_S8:     StreamInfos.format = A2DPD_PCM_FORMAT_S8; break;
				case SND_PCM_FORMAT_U8:     StreamInfos.format = A2DPD_PCM_FORMAT_U8; break;
				case SND_PCM_FORMAT_S16_LE: StreamInfos.format = A2DPD_PCM_FORMAT_S16_LE; break;
				default: StreamInfos.format = A2DPD_PCM_FORMAT_UNKNOWN; break;
				}
				if (send_socket(sockfd, &StreamInfos, sizeof(StreamInfos)) == sizeof(StreamInfos)) {
					a2dp->sk = sockfd;
					syslog(LOG_INFO, "Connected a2dp %p, sk %d, fps %f", a2dp, a2dp->sk, a2dp->TimerInfos.fps);
				} else {
					syslog(LOG_WARNING, "Couldn't send stream informations");
					a2dp_disconnect(a2dp);
				}
			} else {
				close_socket(sockfd);
				syslog(LOG_WARNING, "Connected a2dp %p, sk %d, Authorisation failed", a2dp, a2dp->sk);
			}
		} else {
			syslog(LOG_ERR, "Socket failed a2dp %p, sk %d", a2dp, a2dp->sk);
		}
	}
	return 0;
}