upload to RSA
[framework/system/libsf-common.git] / include / csock.h
1 /*
2  *  libsf-common
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: JuHyun Kim <jh8212.kim@samsung.com>
7  * 
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */ 
21
22
23
24
25 #if !defined(_CSOCK_CLASS_H_)
26 #define _CSOCK_CLASS_H_
27 #include <cipc_worker.h>
28 class csock : public cmutex
29 {
30 public:
31         enum sock_type_t {
32                 SOCK_IPC        = 0x01,
33                 SOCK_NET        = 0x02,
34                 SOCK_TCP        = 0x04,
35                 SOCK_UDP        = 0x08,
36
37                 SOCK_WORKER     = 0x10,
38         };
39
40         struct thread_arg_t {
41                 csock *sock;
42                 int client_handle;
43                 cipc_worker *worker;
44                 void *client_ctx;
45         };
46
47         csock(int handle, int mode = 0x15);
48         csock(char *name, int mode, int port, int server = 0);
49         virtual ~csock(void);
50
51         bool connect_to_server(void);
52         bool wait_for_client(void);
53
54         bool is_connected(void);
55
56         void set_on_close(bool on_close = true);
57
58         void set_worker(void *(*start)(void *data), void *(*running)(void *data), void *(*stop)(void *data));
59
60         void set_client_ctx(void *client_ctx);
61         void *client_ctx();
62
63         bool recv(void *buffer, int len);
64         bool send(void *buffer, int len);
65
66         int handle(void);
67         int mode(void);
68
69         bool disconnect(void);
70
71         static void *thread_tcp_main(void *arg);
72
73         static void *worker_terminate(void *data);
74
75 private:
76         int m_handle;
77         char *m_name;
78
79         sockaddr_un m_addr;     
80         sockaddr_in m_net_addr; 
81
82         void *(*m_start)(void *data);
83         void *(*m_running)(void *data);
84         void *(*m_stop)(void *data);
85
86         void *m_client_ctx;
87         pthread_t m_thid;
88         bool m_on_close;
89         int m_mode;
90 };
91
92
93
94 #endif
95 //! End of a file