mirror of
https://github.com/ossrs/srs.git
synced 2025-02-12 11:21:52 +00:00
add socket rtmp and buffer. rename logs
This commit is contained in:
parent
d4b6178f0f
commit
0609eeb7c8
14 changed files with 297 additions and 58 deletions
2
trunk/configure
vendored
2
trunk/configure
vendored
|
@ -84,7 +84,7 @@ MODULE_DEPENDS=()
|
|||
ModuleLibIncs=(${LibSTRoot})
|
||||
MODULE_FILES=("srs_core" "srs_core_log" "srs_core_server"
|
||||
"srs_core_error" "srs_core_conn" "srs_core_client"
|
||||
"srs_core_rtmp")
|
||||
"srs_core_rtmp" "srs_core_socket" "srs_core_buffer")
|
||||
MODULE_DIR="src/core" . auto/modules.sh
|
||||
CORE_OBJS="${MODULE_OBJS[@]}"
|
||||
|
||||
|
|
24
trunk/src/core/srs_core_buffer.cpp
Executable file
24
trunk/src/core/srs_core_buffer.cpp
Executable file
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 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_core_buffer.hpp>
|
33
trunk/src/core/srs_core_buffer.hpp
Executable file
33
trunk/src/core/srs_core_buffer.hpp
Executable file
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 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.
|
||||
*/
|
||||
|
||||
#ifndef SRS_CORE_BUFFER_HPP
|
||||
#define SRS_CORE_BUFFER_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_buffer.hpp>
|
||||
*/
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#endif
|
|
@ -54,8 +54,16 @@ int SrsClient::do_cycle()
|
|||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if ((ret = get_peer_ip()) != ERROR_SUCCESS) {
|
||||
srs_error("get peer ip failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("get peer ip success. ip=%s", ip);
|
||||
|
||||
if ((ret = rtmp->handshake()) != ERROR_SUCCESS) {
|
||||
srs_error("rtmp handshake failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
srs_verbose("rtmp handshake success");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -71,10 +79,10 @@ int SrsClient::get_peer_ip()
|
|||
socklen_t addrlen = sizeof(addr);
|
||||
if (getpeername(fd, (sockaddr*)&addr, &addrlen) == -1) {
|
||||
ret = ERROR_SOCKET_GET_PEER_NAME;
|
||||
SrsError("discovery client information failed. ret=%d", ret);
|
||||
srs_error("discovery client information failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
SrsVerbose("get peer name success.");
|
||||
srs_verbose("get peer name success.");
|
||||
|
||||
// ip v4 or v6
|
||||
char buf[INET6_ADDRSTRLEN];
|
||||
|
@ -82,15 +90,15 @@ int SrsClient::get_peer_ip()
|
|||
|
||||
if ((inet_ntop(addr.sin_family, &addr.sin_addr, buf, sizeof(buf))) == NULL) {
|
||||
ret = ERROR_SOCKET_GET_PEER_IP;
|
||||
SrsError("convert client information failed. ret=%d", ret);
|
||||
srs_error("convert client information failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
SrsVerbose("get peer ip of client ip=%s, fd=%d", buf, fd);
|
||||
srs_verbose("get peer ip of client ip=%s, fd=%d", buf, fd);
|
||||
|
||||
ip = new char[strlen(buf) + 1];
|
||||
strcpy(ip, buf);
|
||||
|
||||
SrsInfo("get peer ip success. ip=%s, fd=%d", ip, fd);
|
||||
srs_info("get peer ip success. ip=%s, fd=%d", ip, fd);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -47,10 +47,10 @@ int SrsConnection::start()
|
|||
|
||||
if (st_thread_create(cycle_thread, this, 0, 0) == NULL) {
|
||||
ret = ERROR_ST_CREATE_CYCLE_THREAD;
|
||||
SrsError("st_thread_create conn cycle thread error. ret=%d", ret);
|
||||
srs_error("st_thread_create conn cycle thread error. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
SrsVerbose("create st conn cycle thread success.");
|
||||
srs_verbose("create st conn cycle thread success.");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -59,16 +59,17 @@ void SrsConnection::cycle()
|
|||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
log_context->generate_id();
|
||||
ret = do_cycle();
|
||||
|
||||
// success.
|
||||
if (ret == ERROR_SUCCESS) {
|
||||
SrsTrace("client process normally finished. ret=%d", ret);
|
||||
srs_trace("client process normally finished. ret=%d", ret);
|
||||
}
|
||||
|
||||
// client close peer.
|
||||
if (ret == ERROR_SOCKET_CLOSED) {
|
||||
SrsTrace("client disconnect peer. ret=%d", ret);
|
||||
srs_trace("client disconnect peer. ret=%d", ret);
|
||||
}
|
||||
|
||||
server->remove(this);
|
||||
|
|
|
@ -45,5 +45,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||
#define ERROR_SOCKET_CLOSED 204
|
||||
#define ERROR_SOCKET_GET_PEER_NAME 205
|
||||
#define ERROR_SOCKET_GET_PEER_IP 206
|
||||
#define ERROR_SOCKET_READ 207
|
||||
#define ERROR_SOCKET_READ_FULLY 208
|
||||
#define ERROR_SOCKET_WRITE 209
|
||||
|
||||
#endif
|
|
@ -52,7 +52,7 @@ private:
|
|||
DateTime();
|
||||
virtual ~DateTime();
|
||||
public:
|
||||
virtual const char* FormatTime();
|
||||
virtual const char* format_time();
|
||||
};
|
||||
private:
|
||||
DateTime time;
|
||||
|
@ -61,10 +61,10 @@ public:
|
|||
LogContext();
|
||||
virtual ~LogContext();
|
||||
public:
|
||||
virtual void SetId();
|
||||
virtual int GetId();
|
||||
virtual void generate_id();
|
||||
virtual int get_id();
|
||||
public:
|
||||
virtual const char* FormatTime();
|
||||
virtual const char* format_time();
|
||||
};
|
||||
|
||||
ILogContext* log_context = new LogContext();
|
||||
|
@ -78,7 +78,7 @@ LogContext::DateTime::~DateTime()
|
|||
{
|
||||
}
|
||||
|
||||
const char* LogContext::DateTime::FormatTime()
|
||||
const char* LogContext::DateTime::format_time()
|
||||
{
|
||||
// clock time
|
||||
timeval tv;
|
||||
|
@ -108,19 +108,19 @@ LogContext::~LogContext()
|
|||
{
|
||||
}
|
||||
|
||||
void LogContext::SetId()
|
||||
void LogContext::generate_id()
|
||||
{
|
||||
static int id = 1;
|
||||
cache[st_thread_self()] = id++;
|
||||
}
|
||||
|
||||
int LogContext::GetId()
|
||||
int LogContext::get_id()
|
||||
{
|
||||
return cache[st_thread_self()];
|
||||
}
|
||||
|
||||
const char* LogContext::FormatTime()
|
||||
const char* LogContext::format_time()
|
||||
{
|
||||
return time.FormatTime();
|
||||
return time.format_time();
|
||||
}
|
||||
|
||||
|
|
|
@ -42,27 +42,27 @@ public:
|
|||
ILogContext();
|
||||
virtual ~ILogContext();
|
||||
public:
|
||||
virtual void SetId() = 0;
|
||||
virtual int GetId() = 0;
|
||||
virtual void generate_id() = 0;
|
||||
virtual int get_id() = 0;
|
||||
public:
|
||||
virtual const char* FormatTime() = 0;
|
||||
virtual const char* format_time() = 0;
|
||||
};
|
||||
|
||||
// user must implements the LogContext and define a global instance.
|
||||
extern ILogContext* log_context;
|
||||
|
||||
#if 0
|
||||
#define SrsVerbose(msg, ...) printf("[%s][%d][verbs] ", log_context->FormatTime(), log_context->GetId());printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define SrsInfo(msg, ...) printf("[%s][%d][infos] ", log_context->FormatTime(), log_context->GetId());printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define SrsTrace(msg, ...) printf("[%s][%d][trace] ", log_context->FormatTime(), log_context->GetId());printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define SrsWarn(msg, ...) printf("[%s][%d][warns] ", log_context->FormatTime(), log_context->GetId());printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")
|
||||
#define SrsError(msg, ...) printf("[%s][%d][error] ", log_context->FormatTime(), log_context->GetId());printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")
|
||||
#define srs_verbose(msg, ...) printf("[%s][%d][verbs] ", log_context->format_time(), log_context->get_id());printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define srs_info(msg, ...) printf("[%s][%d][infos] ", log_context->format_time(), log_context->get_id());printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define srs_trace(msg, ...) printf("[%s][%d][trace] ", log_context->format_time(), log_context->get_id());printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define srs_warn(msg, ...) printf("[%s][%d][warns] ", log_context->format_time(), log_context->get_id());printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")
|
||||
#define srs_error(msg, ...) printf("[%s][%d][error] ", log_context->format_time(), log_context->get_id());printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")
|
||||
#else
|
||||
#define SrsVerbose(msg, ...) printf("[%s][%d][verbs][%s] ", log_context->FormatTime(), log_context->GetId(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define SrsInfo(msg, ...) printf("[%s][%d][infos][%s] ", log_context->FormatTime(), log_context->GetId(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define SrsTrace(msg, ...) printf("[%s][%d][trace][%s] ", log_context->FormatTime(), log_context->GetId(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define SrsWarn(msg, ...) printf("[%s][%d][warns][%s] ", log_context->FormatTime(), log_context->GetId(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")
|
||||
#define SrsError(msg, ...) printf("[%s][%d][error][%s] ", log_context->FormatTime(), log_context->GetId(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")
|
||||
#define srs_verbose(msg, ...) printf("[%s][%d][verbs][%s] ", log_context->format_time(), log_context->get_id(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define srs_info(msg, ...) printf("[%s][%d][infos][%s] ", log_context->format_time(), log_context->get_id(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define srs_trace(msg, ...) printf("[%s][%d][trace][%s] ", log_context->format_time(), log_context->get_id(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf("\n")
|
||||
#define srs_warn(msg, ...) printf("[%s][%d][warns][%s] ", log_context->format_time(), log_context->get_id(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")
|
||||
#define srs_error(msg, ...) printf("[%s][%d][error][%s] ", log_context->format_time(), log_context->get_id(), __FUNCTION__);printf(msg, ##__VA_ARGS__);printf(" errno=%d(%s)", errno, strerror(errno));printf("\n")
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
|
@ -23,6 +23,11 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||
|
||||
#include <srs_core_rtmp.hpp>
|
||||
|
||||
#include <srs_core_log.hpp>
|
||||
#include <srs_core_error.hpp>
|
||||
#include <srs_core_socket.hpp>
|
||||
#include <srs_core_buffer.hpp>
|
||||
|
||||
SrsRtmp::SrsRtmp(st_netfd_t client_stfd)
|
||||
{
|
||||
stfd = client_stfd;
|
||||
|
@ -31,3 +36,23 @@ SrsRtmp::SrsRtmp(st_netfd_t client_stfd)
|
|||
SrsRtmp::~SrsRtmp()
|
||||
{
|
||||
}
|
||||
|
||||
int SrsRtmp::handshake()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
ssize_t nsize;
|
||||
Socket skt(stfd);
|
||||
|
||||
char buf[1537];
|
||||
buf[0] = 0x03; // plain text.
|
||||
|
||||
char* c0c1 = buf;
|
||||
if ((ret = skt.read_fully(c0c1, 1537, &nsize)) != ERROR_SUCCESS) {
|
||||
srs_error("read c0c1 failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
|
@ -39,6 +39,8 @@ private:
|
|||
public:
|
||||
SrsRtmp(st_netfd_t client_stfd);
|
||||
virtual ~SrsRtmp();
|
||||
public:
|
||||
virtual int handshake();
|
||||
};
|
||||
|
||||
#endif
|
|
@ -57,21 +57,21 @@ int SrsServer::initialize()
|
|||
// use linux epoll.
|
||||
if (st_set_eventsys(ST_EVENTSYS_ALT) == -1) {
|
||||
ret = ERROR_ST_SET_EPOLL;
|
||||
SrsError("st_set_eventsys use linux epoll failed. ret=%d", ret);
|
||||
srs_error("st_set_eventsys use linux epoll failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
SrsVerbose("st_set_eventsys use linux epoll success");
|
||||
srs_verbose("st_set_eventsys use linux epoll success");
|
||||
|
||||
if(st_init() != 0){
|
||||
ret = ERROR_ST_INITIALIZE;
|
||||
SrsError("st_init failed. ret=%d", ret);
|
||||
srs_error("st_init failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
SrsVerbose("st_init success");
|
||||
srs_verbose("st_init success");
|
||||
|
||||
// set current log id.
|
||||
log_context->SetId();
|
||||
SrsInfo("log set id success");
|
||||
log_context->generate_id();
|
||||
srs_info("log set id success");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -82,18 +82,18 @@ int SrsServer::listen(int port)
|
|||
|
||||
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
|
||||
ret = ERROR_SOCKET_CREATE;
|
||||
SrsError("create linux socket error. ret=%d", ret);
|
||||
srs_error("create linux socket error. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
SrsVerbose("create linux socket success. fd=%d", fd);
|
||||
srs_verbose("create linux socket success. fd=%d", fd);
|
||||
|
||||
int reuse_socket = 1;
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_socket, sizeof(int)) == -1) {
|
||||
ret = ERROR_SOCKET_SETREUSE;
|
||||
SrsError("setsockopt reuse-addr error. ret=%d", ret);
|
||||
srs_error("setsockopt reuse-addr error. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
SrsVerbose("setsockopt reuse-addr success. fd=%d", fd);
|
||||
srs_verbose("setsockopt reuse-addr success. fd=%d", fd);
|
||||
|
||||
sockaddr_in addr;
|
||||
addr.sin_family = AF_INET;
|
||||
|
@ -101,33 +101,33 @@ int SrsServer::listen(int port)
|
|||
addr.sin_addr.s_addr = INADDR_ANY;
|
||||
if (bind(fd, (const sockaddr*)&addr, sizeof(sockaddr_in)) == -1) {
|
||||
ret = ERROR_SOCKET_BIND;
|
||||
SrsError("bind socket error. ret=%d", ret);
|
||||
srs_error("bind socket error. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
SrsVerbose("bind socket success. fd=%d", fd);
|
||||
srs_verbose("bind socket success. fd=%d", fd);
|
||||
|
||||
if (::listen(fd, SERVER_LISTEN_BACKLOG) == -1) {
|
||||
ret = ERROR_SOCKET_LISTEN;
|
||||
SrsError("listen socket error. ret=%d", ret);
|
||||
srs_error("listen socket error. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
SrsVerbose("listen socket success. fd=%d", fd);
|
||||
srs_verbose("listen socket success. fd=%d", fd);
|
||||
|
||||
if ((stfd = st_netfd_open_socket(fd)) == NULL){
|
||||
ret = ERROR_ST_OPEN_SOCKET;
|
||||
SrsError("st_netfd_open_socket open socket failed. ret=%d", ret);
|
||||
srs_error("st_netfd_open_socket open socket failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
SrsVerbose("st open socket success. fd=%d", fd);
|
||||
srs_verbose("st open socket success. fd=%d", fd);
|
||||
|
||||
if (st_thread_create(listen_thread, this, 0, 0) == NULL) {
|
||||
ret = ERROR_ST_CREATE_LISTEN_THREAD;
|
||||
SrsError("st_thread_create listen thread error. ret=%d", ret);
|
||||
srs_error("st_thread_create listen thread error. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
SrsVerbose("create st listen thread success.");
|
||||
srs_verbose("create st listen thread success.");
|
||||
|
||||
SrsTrace("server started, listen at port=%d, fd=%d", port, fd);
|
||||
srs_trace("server started, listen at port=%d, fd=%d", port, fd);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -148,7 +148,7 @@ void SrsServer::remove(SrsConnection* conn)
|
|||
conns.erase(it);
|
||||
}
|
||||
|
||||
SrsInfo("conn removed. conns=%d", (int)conns.size());
|
||||
srs_info("conn removed. conns=%d", (int)conns.size());
|
||||
|
||||
// all connections are created by server,
|
||||
// so we delete it here.
|
||||
|
@ -163,13 +163,13 @@ int SrsServer::accept_client(st_netfd_t client_stfd)
|
|||
|
||||
// directly enqueue, the cycle thread will remove the client.
|
||||
conns.push_back(conn);
|
||||
SrsVerbose("add conn to vector. conns=%d", (int)conns.size());
|
||||
srs_verbose("add conn to vector. conns=%d", (int)conns.size());
|
||||
|
||||
// cycle will start process thread and when finished remove the client.
|
||||
if ((ret = conn->start()) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
SrsVerbose("conn start finished. ret=%d", ret);
|
||||
srs_verbose("conn start finished. ret=%d", ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -178,22 +178,25 @@ void SrsServer::listen_cycle()
|
|||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
log_context->generate_id();
|
||||
srs_trace("listen cycle start.");
|
||||
|
||||
while (true) {
|
||||
st_netfd_t client_stfd = st_accept(stfd, NULL, NULL, ST_UTIME_NO_TIMEOUT);
|
||||
|
||||
if(client_stfd == NULL){
|
||||
// ignore error.
|
||||
SrsWarn("ignore accept thread stoppped for accept client error");
|
||||
srs_warn("ignore accept thread stoppped for accept client error");
|
||||
continue;
|
||||
}
|
||||
SrsVerbose("get a client. fd=%d", st_netfd_fileno(client_stfd));
|
||||
srs_verbose("get a client. fd=%d", st_netfd_fileno(client_stfd));
|
||||
|
||||
if ((ret = accept_client(client_stfd)) != ERROR_SUCCESS) {
|
||||
SrsWarn("accept client error. ret=%d", ret);
|
||||
srs_warn("accept client error. ret=%d", ret);
|
||||
continue;
|
||||
}
|
||||
|
||||
SrsVerbose("accept client finished. conns=%d, ret=%d", (int)conns.size(), ret);
|
||||
srs_verbose("accept client finished. conns=%d, ret=%d", (int)conns.size(), ret);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
87
trunk/src/core/srs_core_socket.cpp
Executable file
87
trunk/src/core/srs_core_socket.cpp
Executable file
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 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_core_socket.hpp>
|
||||
|
||||
#include <srs_core_error.hpp>
|
||||
|
||||
Socket::Socket(st_netfd_t client_stfd)
|
||||
{
|
||||
stfd = client_stfd;
|
||||
}
|
||||
|
||||
Socket::~Socket()
|
||||
{
|
||||
}
|
||||
|
||||
int Socket::read(const void* buf, size_t size, ssize_t* nread)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
*nread = st_read(stfd, (void*)buf, size, ST_UTIME_NO_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) {
|
||||
if (*nread == 0) {
|
||||
errno = ECONNRESET;
|
||||
}
|
||||
|
||||
ret = ERROR_SOCKET_READ;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int Socket::read_fully(const void* buf, size_t size, ssize_t* nread)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
*nread = st_read_fully(stfd, (void*)buf, size, ST_UTIME_NO_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) {
|
||||
if (*nread >= 0) {
|
||||
errno = ECONNRESET;
|
||||
}
|
||||
|
||||
ret = ERROR_SOCKET_READ_FULLY;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int Socket::write(const void* buf, size_t size, ssize_t* nwrite)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
*nwrite = st_write(stfd, (void*)buf, size, ST_UTIME_NO_TIMEOUT);
|
||||
|
||||
if (*nwrite <= 0) {
|
||||
ret = ERROR_SOCKET_WRITE;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
49
trunk/src/core/srs_core_socket.hpp
Executable file
49
trunk/src/core/srs_core_socket.hpp
Executable file
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 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.
|
||||
*/
|
||||
|
||||
#ifndef SRS_CORE_SOCKET_HPP
|
||||
#define SRS_CORE_SOCKET_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_socket.hpp>
|
||||
*/
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#include <st.h>
|
||||
|
||||
// the socket base on st.
|
||||
class Socket
|
||||
{
|
||||
private:
|
||||
st_netfd_t stfd;
|
||||
public:
|
||||
Socket(st_netfd_t client_stfd);
|
||||
virtual ~Socket();
|
||||
public:
|
||||
virtual int read(const void* buf, size_t size, ssize_t* nread);
|
||||
virtual int read_fully(const void* buf, size_t size, ssize_t* nread);
|
||||
virtual int write(const void* buf, size_t size, ssize_t* nwrite);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -14,6 +14,10 @@ file
|
|||
..\core\srs_core_client.cpp,
|
||||
..\core\srs_core_rtmp.hpp,
|
||||
..\core\srs_core_rtmp.cpp,
|
||||
..\core\srs_core_socket.hpp,
|
||||
..\core\srs_core_socket.cpp,
|
||||
..\core\srs_core_buffer.hpp,
|
||||
..\core\srs_core_buffer.cpp,
|
||||
..\core\srs_core_log.hpp,
|
||||
..\core\srs_core_log.cpp;
|
||||
mainconfig
|
||||
|
|
Loading…
Reference in a new issue