Blame view

trunk/src/app/srs_app_st_socket.cpp 5.0 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_st_socket.hpp>
winlin authored
25
26
#include <srs_kernel_error.hpp>
winlin authored
27
28
SrsStSocket::SrsStSocket(st_netfd_t client_stfd)
winlin authored
29 30
{
    stfd = client_stfd;
31 32
    send_timeout = recv_timeout = ST_UTIME_NO_TIMEOUT;
    recv_bytes = send_bytes = 0;
winlin authored
33 34
}
35
SrsStSocket::~SrsStSocket()
winlin authored
36 37 38
{
}
39
bool SrsStSocket::is_never_timeout(int64_t timeout_us)
40
{
41
    return timeout_us == (int64_t)ST_UTIME_NO_TIMEOUT;
42 43
}
44
void SrsStSocket::set_recv_timeout(int64_t timeout_us)
winlin authored
45
{
46
    recv_timeout = timeout_us;
winlin authored
47 48
}
49
int64_t SrsStSocket::get_recv_timeout()
winlin authored
50
{
51
    return recv_timeout;
winlin authored
52 53
}
54
void SrsStSocket::set_send_timeout(int64_t timeout_us)
winlin authored
55
{
56
    send_timeout = timeout_us;
winlin authored
57 58
}
59
int64_t SrsStSocket::get_send_timeout()
60
{
61
    return send_timeout;
62 63
}
64
int64_t SrsStSocket::get_recv_bytes()
winlin authored
65
{
66
    return recv_bytes;
winlin authored
67 68
}
69
int64_t SrsStSocket::get_send_bytes()
winlin authored
70
{
71
    return send_bytes;
winlin authored
72 73
}
74
int SrsStSocket::read(void* buf, size_t size, ssize_t* nread)
winlin authored
75 76 77
{
    int ret = ERROR_SUCCESS;
    
78
    ssize_t nb_read = st_read(stfd, buf, size, recv_timeout);
79 80 81
    if (nread) {
        *nread = nb_read;
    }
winlin authored
82 83 84
    
    // 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).
85
    // Otherwise, a value of -1 is returned and errno is set to indicate the error. 
86
    if (nb_read <= 0) {
87
        // @see https://github.com/winlinvip/simple-rtmp-server/issues/200
88
        if (nb_read < 0 && errno == ETIME) {
89 90 91
            return ERROR_SOCKET_TIMEOUT;
        }
        
92
        if (nb_read == 0) {
winlin authored
93 94 95 96 97 98
            errno = ECONNRESET;
        }
        
        return ERROR_SOCKET_READ;
    }
    
99
    recv_bytes += nb_read;
winlin authored
100 101 102 103
        
    return ret;
}
104
int SrsStSocket::read_fully(void* buf, size_t size, ssize_t* nread)
winlin authored
105 106 107
{
    int ret = ERROR_SUCCESS;
    
108
    ssize_t nb_read = st_read_fully(stfd, buf, size, recv_timeout);
109 110 111
    if (nread) {
        *nread = nb_read;
    }
winlin authored
112 113 114
    
    // 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)
115
    // Otherwise, a value of -1 is returned and errno is set to indicate the error. 
116
    if (nb_read != (ssize_t)size) {
117
        // @see https://github.com/winlinvip/simple-rtmp-server/issues/200
118
        if (nb_read < 0 && errno == ETIME) {
119 120 121
            return ERROR_SOCKET_TIMEOUT;
        }
        
122
        if (nb_read >= 0) {
winlin authored
123 124 125 126 127 128
            errno = ECONNRESET;
        }
        
        return ERROR_SOCKET_READ_FULLY;
    }
    
129
    recv_bytes += nb_read;
winlin authored
130 131 132 133
    
    return ret;
}
134
int SrsStSocket::write(void* buf, size_t size, ssize_t* nwrite)
winlin authored
135 136 137
{
    int ret = ERROR_SUCCESS;
    
138
    ssize_t nb_write = st_write(stfd, buf, size, send_timeout);
139 140 141
    if (nwrite) {
        *nwrite = nb_write;
    }
winlin authored
142
    
143 144
    // On success a non-negative integer equal to nbyte is returned. 
    // Otherwise, a value of -1 is returned and errno is set to indicate the error.
145
    if (nb_write <= 0) {
146
        // @see https://github.com/winlinvip/simple-rtmp-server/issues/200
147
        if (nb_write < 0 && errno == ETIME) {
148 149 150
            return ERROR_SOCKET_TIMEOUT;
        }
        
winlin authored
151 152 153
        return ERROR_SOCKET_WRITE;
    }
    
154
    send_bytes += nb_write;
winlin authored
155 156 157 158
        
    return ret;
}
159
int SrsStSocket::writev(const iovec *iov, int iov_size, ssize_t* nwrite)
winlin authored
160 161 162
{
    int ret = ERROR_SUCCESS;
    
163 164 165 166
    ssize_t nb_write = st_writev(stfd, iov, iov_size, send_timeout);
    if (nwrite) {
        *nwrite = nb_write;
    }
winlin authored
167
    
168 169
    // On success a non-negative integer equal to nbyte is returned. 
    // Otherwise, a value of -1 is returned and errno is set to indicate the error.
170
    if (nb_write <= 0) {
171
        // @see https://github.com/winlinvip/simple-rtmp-server/issues/200
172
        if (nb_write < 0 && errno == ETIME) {
173 174 175
            return ERROR_SOCKET_TIMEOUT;
        }
        
winlin authored
176 177 178
        return ERROR_SOCKET_WRITE;
    }
    
179
    send_bytes += nb_write;
winlin authored
180 181 182 183
    
    return ret;
}
184