PageRenderTime 24ms CodeModel.GetById 16ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/fbus/fbus_source_listen.c

http://ftk.googlecode.com/
C | 121 lines | 73 code | 19 blank | 29 comment | 9 complexity | 511106b487f0cc39188e86d663fe9578 MD5 | raw file
  1/*
  2 * File:    fbus_source_listen.c
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   a source to listen on a socket port.
  5 *
  6 * Copyright (c) 2009 - 2010  Li XianJing <xianjimli@hotmail.com>
  7 *
  8 * Licensed under the Academic Free License version 2.1
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License as published by
 12 * the Free Software Foundation; either version 2 of the License, or
 13 * (at your option) any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 23 */
 24
 25/*
 26 * History:
 27 * ================================================================
 28 * 2010-07-25 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include "ftk_globals.h"
 33#include "ftk_main_loop.h"
 34#include "fbus_stream_socket.h"
 35#include "fbus_source_listen.h"
 36#include "fbus_source_service.h"
 37
 38typedef struct _PrivInfo
 39{
 40	int sock_no;
 41	FBusService* service;
 42}PrivInfo;
 43
 44static int fbus_source_listen_get_fd(FtkSource* thiz)
 45{
 46	DECL_PRIV(thiz, priv);
 47	
 48	return priv->sock_no;
 49}
 50
 51static int fbus_source_listen_check(FtkSource* thiz)
 52{
 53	return -1;
 54}
 55
 56Ret fbus_source_listen_dispatch(FtkSource* thiz)
 57{
 58	FtkSource* source = NULL;
 59	FBusStream* stream = NULL;
 60	DECL_PRIV(thiz, priv);
 61
 62	stream = fbus_socket_accept(priv->sock_no);
 63	return_val_if_fail(stream != NULL, RET_REMOVE);
 64
 65	source = fbus_source_service_create(priv->service, stream);
 66
 67	if(source != NULL)
 68	{
 69		ftk_main_loop_add_source(ftk_default_main_loop(), source);
 70	}
 71	else
 72	{
 73		fbus_stream_destroy(stream);
 74	}
 75
 76	return RET_OK;
 77}
 78
 79static void fbus_source_listen_destroy(FtkSource* thiz)
 80{
 81	if(thiz != NULL)
 82	{
 83		DECL_PRIV(thiz, priv);
 84		ftk_socket_close(priv->sock_no);
 85		fbus_service_destroy(priv->service);
 86		FTK_FREE(thiz);
 87	}
 88
 89	return;
 90}
 91
 92FtkSource* fbus_source_listen_create(FBusService* service, int port)
 93{
 94	int sock_no = 0;
 95	FtkSource* thiz = NULL;
 96	return_val_if_fail(service != NULL && port > 0, NULL);
 97	sock_no = fbus_stream_listen(port);
 98	return_val_if_fail(sock_no > 0, NULL);
 99
100	thiz = FTK_ZALLOC(sizeof(FtkSource) + sizeof(PrivInfo));
101	if(thiz != NULL)
102	{
103		DECL_PRIV(thiz, priv);
104
105		priv->sock_no = sock_no;
106		priv->service = service;
107
108		thiz->ref = 1;
109		thiz->check = fbus_source_listen_check;
110		thiz->get_fd = fbus_source_listen_get_fd;
111		thiz->dispatch = fbus_source_listen_dispatch;
112		thiz->destroy = fbus_source_listen_destroy;
113	}
114	else
115	{
116		ftk_socket_close(sock_no);
117	}
118
119	return thiz;
120}
121