1 // 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 28 typedef int t_socket; 29 typedef t_socket* p_socket; 30 #endif 31 32 // Error Codes 33 enum { 34 SUCCESS = 0, 35 TIMEOUT = -1, 36 CLOSED = -2, 37 }; 38 typedef int T_ERRCODE; 39 40 static const char * TIMEOUT_MSG = "Timeout"; 41 static const char * CLOSED_MSG = "Connection Closed"; 42 43 typedef struct sockaddr t_sa; 44 typedef t_sa * p_sa; 45 46 T_ERRCODE socket_create(p_socket sock, int domain, int type, int protocol); 47 T_ERRCODE socket_destroy(p_socket sock); 48 T_ERRCODE socket_bind(p_socket sock, p_sa addr, int addr_len); 49 T_ERRCODE socket_get_info(p_socket sock, short *port, char *buf, size_t len); 50 T_ERRCODE socket_send(p_socket sock, const char *data, size_t len, int timeout); 51 T_ERRCODE socket_recv(p_socket sock, char *data, size_t len, int timeout, 52 int *received); 53 54 T_ERRCODE socket_setblocking(p_socket sock); 55 T_ERRCODE socket_setnonblocking(p_socket sock); 56 57 T_ERRCODE socket_accept(p_socket sock, p_socket sibling, 58 p_sa addr, socklen_t *addr_len, int timeout); 59 T_ERRCODE socket_listen(p_socket sock, int backlog); 60 61 T_ERRCODE socket_connect(p_socket sock, p_sa addr, int addr_len, int timeout); 62 63 const char * tcp_create(p_socket sock); 64 const char * tcp_destroy(p_socket sock); 65 const char * tcp_bind(p_socket sock, const char *host, unsigned short port); 66 const char * tcp_send(p_socket sock, const char *data, size_t w_len, 67 int timeout); 68 const char * tcp_receive(p_socket sock, char *data, size_t r_len, int timeout); 69 const char * tcp_raw_receive(p_socket sock, char * data, size_t r_len, 70 int timeout, int *received); 71 72 const char * tcp_listen(p_socket sock, int backlog); 73 const char * tcp_accept(p_socket sock, p_socket client, int timeout); 74 75 const char * tcp_connect(p_socket sock, const char *host, unsigned short port, 76 int timeout); 77 78 const char * tcp_create_and_connect(p_socket sock, const char *host, 79 unsigned short port, int timeout); 80 81 #endif 82