blob: 8019ffed8e37838bd988b452a669f923bcad59b4 [file] [log] [blame]
Roger Meier6cf0ffc2014-04-05 00:45:42 +02001//
2// Licensed to the Apache Software Foundation (ASF) under one
3// or more contributor license agreements. See the NOTICE file
4// distributed with this work for additional information
5// regarding copyright ownership. The ASF licenses this file
6// to you under the Apache License, Version 2.0 (the
7// "License"); you may not use this file except in compliance
8// with the License. You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing,
13// software distributed under the License is distributed on an
14// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15// KIND, either express or implied. See the License for the
16// specific language governing permissions and limitations
17// under the License.
18//
19
20#ifndef LUA_THRIFT_SOCKET_H
21#define LUA_THRIFT_SOCKET_H
22
23#include <sys/socket.h>
24
25#ifdef _WIN32
26// SOL
27#else
28typedef int t_socket;
29typedef t_socket* p_socket;
30#endif
31
32// Error Codes
33enum {
34 SUCCESS = 0,
35 TIMEOUT = -1,
36 CLOSED = -2,
37};
38typedef int T_ERRCODE;
39
40static const char * TIMEOUT_MSG = "Timeout";
41static const char * CLOSED_MSG = "Connection Closed";
42
43typedef struct sockaddr t_sa;
44typedef t_sa * p_sa;
45
46T_ERRCODE socket_create(p_socket sock, int domain, int type, int protocol);
47T_ERRCODE socket_destroy(p_socket sock);
48T_ERRCODE socket_bind(p_socket sock, p_sa addr, int addr_len);
49T_ERRCODE socket_get_info(p_socket sock, short *port, char *buf, size_t len);
50T_ERRCODE socket_send(p_socket sock, const char *data, size_t len, int timeout);
51T_ERRCODE socket_recv(p_socket sock, char *data, size_t len, int timeout,
52 int *received);
53
54void socket_setblocking(p_socket sock);
55void socket_setnonblocking(p_socket sock);
56
57T_ERRCODE socket_accept(p_socket sock, p_socket sibling,
58 p_sa addr, socklen_t *addr_len, int timeout);
59T_ERRCODE socket_listen(p_socket sock, int backlog);
60
61T_ERRCODE socket_connect(p_socket sock, p_sa addr, int addr_len, int timeout);
62
63const char * tcp_create(p_socket sock);
64const char * tcp_destroy(p_socket sock);
65const char * tcp_bind(p_socket sock, const char *host, unsigned short port);
66const char * tcp_send(p_socket sock, const char *data, size_t w_len,
67 int timeout);
68const char * tcp_receive(p_socket sock, char *data, size_t r_len, int timeout);
69const char * tcp_raw_receive(p_socket sock, char * data, size_t r_len,
70 int timeout, int *received);
71
72const char * tcp_listen(p_socket sock, int backlog);
73const char * tcp_accept(p_socket sock, p_socket client, int timeout);
74
75const char * tcp_connect(p_socket sock, const char *host, unsigned short port,
76 int timeout);
77
78#endif