Blame view

trunk/src/app/srs_app_socket.cpp 4.4 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 102 103 104 105 106
}

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

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

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

int SrsSocket::writev(const iovec *iov, int iov_size, ssize_t* nwrite)
{
    int ret = ERROR_SUCCESS;
    
    *nwrite = st_writev(stfd, iov, iov_size, send_timeout);
    
    if (*nwrite <= 0) {
174 175 176 177
        if (errno == ETIME) {
            return ERROR_SOCKET_TIMEOUT;
        }
        
winlin authored
178 179 180 181 182 183 184 185
        return ERROR_SOCKET_WRITE;
    }
    
    send_bytes += *nwrite;
    
    return ret;
}