#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/errno.h>
#include <sys/types.h>
#include <errno.h>
#include <signal.h>
#include <stdint.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
#include <limits.h> 
#include <fcntl.h>
#include <strings.h>
#include <sys/epoll.h>
#include <sys/resource.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "osp.h"
#include "ospTcp.h"


int32_t osp_tcp_send(int32_t socket_fd, char *pMsg, int32_t msgLen)
{
    return send(socket_fd, pMsg, msgLen, MSG_NOSIGNAL);

}

int32_t osp_tcp_rev(int32_t socket_fd, char *pMsg, int32_t msgLen)
{
    return recv(socket_fd, pMsg, msgLen, 0); 
}
#if 0
int tcpSerCreat(int port)
{
    int    socket_fd; 
    struct sockaddr_in    servaddr; 
    int  opt;
    int optLen = sizeof(int);
    
    if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
    { 
        return -1;
    } 


    


    opt=(64*1024);
    setsockopt(socket_fd, SOL_SOCKET, SO_SNDBUF,(char*)&opt, sizeof(opt));
    getsockopt(socket_fd, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt));

    opt=(64*1024);
    setsockopt(socket_fd, SOL_SOCKET, SO_RCVBUF,(char*)&opt, sizeof(opt));
    getsockopt(socket_fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
    opt = 1;
    setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR,(char*)&opt, sizeof(opt));

    

    memset(&servaddr, 0, sizeof(servaddr)); 
    servaddr.sin_family = AF_INET; 
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(port);

   
    if (bind(socket_fd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1) 
    { 
        return -1;
    }
    if (listen(socket_fd, 10) == -1) 
    { 
        return -1;
    }     
    return socket_fd;
    
}

int tcpCliCreat(int port)
{
    int socket_fd;
    int reuse = 0;
    int opt;
    struct sockaddr_in servaddr; 


    if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) 
    { 
        return -1;
    } 

    opt=(64*1024);
    setsockopt(socket_fd, SOL_SOCKET, SO_SNDBUF,(char*)&opt, sizeof(opt));
    getsockopt(socket_fd, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt));

    opt=(64*1024);
    setsockopt(socket_fd, SOL_SOCKET, SO_RCVBUF,(char*)&opt, sizeof(opt));
    getsockopt(socket_fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
    opt = 1;
    setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR,(char*)&opt, sizeof(opt));

    memset(&servaddr, 0, sizeof(servaddr)); 
    servaddr.sin_family = AF_INET; 
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(port);
#if 0
    if (bind(socket_fd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1) 
    { 
        return -1;
    }
#endif   
    return socket_fd;
   
    
}

int tcpAccept(int socket_fd)
{
    int connect_fd;
    
    if ((connect_fd = accept(socket_fd, (struct sockaddr*)NULL, NULL)) == -1)
    {
        return -1;
    }
    return connect_fd;
}

int tcpConnet(int socket_fd, char *targetIp, int port)
{
    struct sockaddr_in servaddr; 
    
    memset(&servaddr, 0, sizeof(servaddr)); 
    servaddr.sin_family = AF_INET; 
    servaddr.sin_port = htons(port); 
    servaddr.sin_addr.s_addr = inet_addr(targetIp);

    
    if (connect(socket_fd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) 
    { 
        return -1;
    } 
    return 0;
}
#endif


