Blame view

trunk/src/libs/srs_lib_simple_socket.cpp 5.2 KB
winlin authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
The MIT License (MIT)

Copyright (c) 2013-2014 winlin

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.
*/

#include <srs_lib_simple_socket.hpp>
winlin authored
26 27 28 29 30
#include <srs_kernel_error.hpp>

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
31 32
#include <netinet/in.h>
#include <arpa/inet.h>
33 34 35
#include <errno.h>
#include <sys/uio.h>
winlin authored
36 37
#include <srs_kernel_utility.hpp>
38
#ifndef ST_UTIME_NO_TIMEOUT
39
    #define ST_UTIME_NO_TIMEOUT -1
40
#endif
winlin authored
41 42 43

SimpleSocketStream::SimpleSocketStream()
{
44 45 46 47 48 49
    fd = -1;
    send_timeout = recv_timeout = ST_UTIME_NO_TIMEOUT;
    recv_bytes = send_bytes = 0;
    
    srs_update_system_time_ms();
    start_time_ms = srs_get_system_time_ms();
winlin authored
50 51
}
winlin authored
52 53
SimpleSocketStream::~SimpleSocketStream()
{
54 55 56 57
    if (fd != -1) {
        ::close(fd);
        fd = -1;
    }
winlin authored
58 59 60 61
}

int SimpleSocketStream::create_socket()
{
62
    if((fd = ::socket(AF_INET, SOCK_STREAM, 0)) < 0){
winlin authored
63 64 65
        return -1;
    }
66
    return ERROR_SUCCESS;
winlin authored
67 68
}
69 70 71 72 73 74 75 76 77 78 79
int SimpleSocketStream::connect(const char* server_ip, int port)
{
    sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = inet_addr(server_ip);
    
    if(::connect(fd, (const struct sockaddr*)&addr, sizeof(sockaddr_in)) < 0){
        return -1;
    }
80
    return ERROR_SUCCESS;
81 82 83 84 85
}

// ISrsBufferReader
int SimpleSocketStream::read(const void* buf, size_t size, ssize_t* nread)
{
86 87 88
    int ret = ERROR_SUCCESS;
    
    *nread = ::recv(fd, (void*)buf, size, 0);
89 90 91 92
    
    // 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) {
93 94 95 96
        if (errno == ETIME) {
            return ERROR_SOCKET_TIMEOUT;
        }
        
97 98 99 100 101 102 103 104
        if (*nread == 0) {
            errno = ECONNRESET;
        }
        
        return ERROR_SOCKET_READ;
    }
    
    recv_bytes += *nread;
105 106
    
    return ret;
107 108 109 110 111
}

// ISrsProtocolReader
void SimpleSocketStream::set_recv_timeout(int64_t timeout_us)
{
112
    recv_timeout = timeout_us;
113 114 115 116
}

int64_t SimpleSocketStream::get_recv_timeout()
{
117
    return recv_timeout;
118 119 120 121
}

int64_t SimpleSocketStream::get_recv_bytes()
{
122
    return recv_bytes;
123 124 125 126
}

int SimpleSocketStream::get_recv_kbps()
{
127 128 129 130 131 132 133 134
    srs_update_system_time_ms();
    int64_t diff_ms = srs_get_system_time_ms() - start_time_ms;
    
    if (diff_ms <= 0) {
        return 0;
    }
    
    return recv_bytes * 8 / diff_ms;
135 136 137 138 139
}

// ISrsProtocolWriter
void SimpleSocketStream::set_send_timeout(int64_t timeout_us)
{
140
    send_timeout = timeout_us;
141 142 143 144
}

int64_t SimpleSocketStream::get_send_timeout()
{
145
    return send_timeout;
146 147 148 149
}

int64_t SimpleSocketStream::get_send_bytes()
{
150
    return send_bytes;
151 152 153 154
}

int SimpleSocketStream::get_send_kbps()
{
155 156 157 158 159 160 161 162
    srs_update_system_time_ms();
    int64_t diff_ms = srs_get_system_time_ms() - start_time_ms;
    
    if (diff_ms <= 0) {
        return 0;
    }
    
    return send_bytes * 8 / diff_ms;
163 164 165 166
}

int SimpleSocketStream::writev(const iovec *iov, int iov_size, ssize_t* nwrite)
{
167
    int ret = ERROR_SUCCESS;
168 169 170 171
    
    *nwrite = ::writev(fd, iov, iov_size);
    
    if (*nwrite <= 0) {
172 173 174 175
        if (errno == ETIME) {
            return ERROR_SOCKET_TIMEOUT;
        }
        
176 177 178 179 180
        return ERROR_SOCKET_WRITE;
    }
    
    send_bytes += *nwrite;
    
181
    return ret;
182 183 184 185 186
}

// ISrsProtocolReaderWriter
bool SimpleSocketStream::is_never_timeout(int64_t timeout_us)
{
187
    return timeout_us == (int64_t)ST_UTIME_NO_TIMEOUT;
188 189 190 191
}

int SimpleSocketStream::read_fully(const void* buf, size_t size, ssize_t* nread)
{
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    int ret = ERROR_SUCCESS;
    
    size_t left = size;
    *nread = 0;
    
    while (left > 0) {
        char* this_buf = (char*)buf + *nread;
        ssize_t this_nread;
        
        if ((ret = this->read(this_buf, left, &this_nread)) != ERROR_SUCCESS) {
            return ret;
        }
        
        *nread += this_nread;
        left -= this_nread;
    }
208 209
    
    recv_bytes += *nread;
210 211
    
    return ret;
212 213 214 215
}

int SimpleSocketStream::write(const void* buf, size_t size, ssize_t* nwrite)
{
216
    int ret = ERROR_SUCCESS;
217 218 219 220
    
    *nwrite = ::send(fd, (void*)buf, size, 0);
    
    if (*nwrite <= 0) {
221 222 223 224
        if (errno == ETIME) {
            return ERROR_SOCKET_TIMEOUT;
        }
        
225 226 227 228 229
        return ERROR_SOCKET_WRITE;
    }
    
    send_bytes += *nwrite;
    
230
    return ret;
231 232
}