Blame view

trunk/src/app/srs_app_socket.cpp 4.1 KB
winlin authored
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2014 winlin
winlin authored
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
24
#include <srs_app_socket.hpp>
winlin authored
25
26
#include <srs_kernel_error.hpp>
27
#include <srs_kernel_utility.hpp>
winlin authored
28 29 30 31

SrsSocket::SrsSocket(st_netfd_t client_stfd)
{
    stfd = client_stfd;
32 33
    send_timeout = recv_timeout = ST_UTIME_NO_TIMEOUT;
    recv_bytes = send_bytes = 0;
winlin authored
34 35 36 37 38 39
}

SrsSocket::~SrsSocket()
{
}
40 41
bool SrsSocket::is_never_timeout(int64_t timeout_us)
{
42
    return timeout_us == (int64_t)ST_UTIME_NO_TIMEOUT;
43 44
}
winlin authored
45 46
void SrsSocket::set_recv_timeout(int64_t timeout_us)
{
47
    recv_timeout = timeout_us;
winlin authored
48 49 50 51
}

int64_t SrsSocket::get_recv_timeout()
{
52
    return recv_timeout;
winlin authored
53 54 55 56
}

void SrsSocket::set_send_timeout(int64_t timeout_us)
{
57
    send_timeout = timeout_us;
winlin authored
58 59
}
60 61
int64_t SrsSocket::get_send_timeout()
{
62
    return send_timeout;
63 64
}
winlin authored
65 66
int64_t SrsSocket::get_recv_bytes()
{
67
    return recv_bytes;
winlin authored
68 69 70 71
}

int64_t SrsSocket::get_send_bytes()
{
72
    return send_bytes;
winlin authored
73 74
}
75
int SrsSocket::read(void* buf, size_t size, ssize_t* nread)
winlin authored
76 77 78
{
    int ret = ERROR_SUCCESS;
    
79
    ssize_t nb_read = st_read(stfd, buf, size, recv_timeout);
80 81 82
    if (nread) {
        *nread = nb_read;
    }
winlin authored
83 84 85
    
    // On success a non-negative integer indicating the number of bytes actually read is returned 
    // (a value of 0 means the network connection is closed or end of file is reached).
86
    if (nb_read <= 0) {
87 88 89 90
        if (errno == ETIME) {
            return ERROR_SOCKET_TIMEOUT;
        }
        
91
        if (nb_read == 0) {
winlin authored
92 93 94 95 96 97
            errno = ECONNRESET;
        }
        
        return ERROR_SOCKET_READ;
    }
    
98
    recv_bytes += nb_read;
winlin authored
99 100 101 102
        
    return ret;
}
103
int SrsSocket::read_fully(void* buf, size_t size, ssize_t* nread)
winlin authored
104 105 106
{
    int ret = ERROR_SUCCESS;
    
107
    ssize_t nb_read = st_read_fully(stfd, buf, size, recv_timeout);
108 109 110
    if (nread) {
        *nread = nb_read;
    }
winlin authored
111 112 113
    
    // On success a non-negative integer indicating the number of bytes actually read is returned 
    // (a value less than nbyte means the network connection is closed or end of file is reached)
114
    if (nb_read != (ssize_t)size) {
115 116 117 118
        if (errno == ETIME) {
            return ERROR_SOCKET_TIMEOUT;
        }
        
119
        if (nb_read >= 0) {
winlin authored
120 121 122 123 124 125
            errno = ECONNRESET;
        }
        
        return ERROR_SOCKET_READ_FULLY;
    }
    
126
    recv_bytes += nb_read;
winlin authored
127 128 129 130
    
    return ret;
}
131
int SrsSocket::write(void* buf, size_t size, ssize_t* nwrite)
winlin authored
132 133 134
{
    int ret = ERROR_SUCCESS;
    
135
    ssize_t nb_write = st_write(stfd, buf, size, send_timeout);
136 137 138
    if (nwrite) {
        *nwrite = nb_write;
    }
winlin authored
139
    
140
    if (nb_write <= 0) {
141 142 143 144
        if (errno == ETIME) {
            return ERROR_SOCKET_TIMEOUT;
        }
        
winlin authored
145 146 147
        return ERROR_SOCKET_WRITE;
    }
    
148
    send_bytes += nb_write;
winlin authored
149 150 151 152 153 154 155 156
        
    return ret;
}

int SrsSocket::writev(const iovec *iov, int iov_size, ssize_t* nwrite)
{
    int ret = ERROR_SUCCESS;
    
157 158 159 160
    ssize_t nb_write = st_writev(stfd, iov, iov_size, send_timeout);
    if (nwrite) {
        *nwrite = nb_write;
    }
winlin authored
161
    
162
    if (nb_write <= 0) {
163 164 165 166
        if (errno == ETIME) {
            return ERROR_SOCKET_TIMEOUT;
        }
        
winlin authored
167 168 169
        return ERROR_SOCKET_WRITE;
    }
    
170
    send_bytes += nb_write;
winlin authored
171 172 173 174
    
    return ret;
}