Blame view

trunk/src/app/srs_app_socket.cpp 4.6 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 34
    send_timeout = recv_timeout = ST_UTIME_NO_TIMEOUT;
    recv_bytes = send_bytes = 0;
    start_time_ms = srs_get_system_time_ms();
winlin authored
35 36 37 38 39 40
}

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

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

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

int64_t SrsSocket::get_send_bytes()
{
73
    return send_bytes;
winlin authored
74 75 76 77
}

int SrsSocket::get_recv_kbps()
{
78 79 80 81 82 83 84
    int64_t diff_ms = srs_get_system_time_ms() - start_time_ms;
    
    if (diff_ms <= 0) {
        return 0;
    }
    
    return recv_bytes * 8 / diff_ms;
winlin authored
85 86 87 88
}

int SrsSocket::get_send_kbps()
{
89 90 91 92 93 94 95
    int64_t diff_ms = srs_get_system_time_ms() - start_time_ms;
    
    if (diff_ms <= 0) {
        return 0;
    }
    
    return send_bytes * 8 / diff_ms;
winlin authored
96 97 98 99 100 101
}

int SrsSocket::read(const void* buf, size_t size, ssize_t* nread)
{
    int ret = ERROR_SUCCESS;
    
102 103 104 105
    ssize_t nb_read = st_read(stfd, (void*)buf, size, recv_timeout);
    if (nread) {
        *nread = nb_read;
    }
winlin authored
106 107 108
    
    // 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).
109
    if (nb_read <= 0) {
110 111 112 113
        if (errno == ETIME) {
            return ERROR_SOCKET_TIMEOUT;
        }
        
114
        if (nb_read == 0) {
winlin authored
115 116 117 118 119 120
            errno = ECONNRESET;
        }
        
        return ERROR_SOCKET_READ;
    }
    
121
    recv_bytes += nb_read;
winlin authored
122 123 124 125 126 127 128 129
        
    return ret;
}

int SrsSocket::read_fully(const void* buf, size_t size, ssize_t* nread)
{
    int ret = ERROR_SUCCESS;
    
130 131 132 133
    ssize_t nb_read = st_read_fully(stfd, (void*)buf, size, recv_timeout);
    if (nread) {
        *nread = nb_read;
    }
winlin authored
134 135 136
    
    // 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)
137
    if (nb_read != (ssize_t)size) {
138 139 140 141
        if (errno == ETIME) {
            return ERROR_SOCKET_TIMEOUT;
        }
        
142
        if (nb_read >= 0) {
winlin authored
143 144 145 146 147 148
            errno = ECONNRESET;
        }
        
        return ERROR_SOCKET_READ_FULLY;
    }
    
149
    recv_bytes += nb_read;
winlin authored
150 151 152 153 154 155 156 157
    
    return ret;
}

int SrsSocket::write(const void* buf, size_t size, ssize_t* nwrite)
{
    int ret = ERROR_SUCCESS;
    
158 159 160 161
    ssize_t nb_write = st_write(stfd, (void*)buf, size, send_timeout);
    if (nwrite) {
        *nwrite = nb_write;
    }
winlin authored
162
    
163
    if (nb_write <= 0) {
164 165 166 167
        if (errno == ETIME) {
            return ERROR_SOCKET_TIMEOUT;
        }
        
winlin authored
168 169 170
        return ERROR_SOCKET_WRITE;
    }
    
171
    send_bytes += nb_write;
winlin authored
172 173 174 175 176 177 178 179
        
    return ret;
}

int SrsSocket::writev(const iovec *iov, int iov_size, ssize_t* nwrite)
{
    int ret = ERROR_SUCCESS;
    
180 181 182 183
    ssize_t nb_write = st_writev(stfd, iov, iov_size, send_timeout);
    if (nwrite) {
        *nwrite = nb_write;
    }
winlin authored
184
    
185
    if (nb_write <= 0) {
186 187 188 189
        if (errno == ETIME) {
            return ERROR_SOCKET_TIMEOUT;
        }
        
winlin authored
190 191 192
        return ERROR_SOCKET_WRITE;
    }
    
193
    send_bytes += nb_write;
winlin authored
194 195 196 197
    
    return ret;
}