mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
get peer ip and add rtmp class
This commit is contained in:
parent
96e1de255c
commit
d4b6178f0f
11 changed files with 178 additions and 34 deletions
4
trunk/configure
vendored
4
trunk/configure
vendored
|
@ -82,7 +82,9 @@ LibSTfile="${LibSTRoot}/libst.a"
|
||||||
MODULE_ID="CORE"
|
MODULE_ID="CORE"
|
||||||
MODULE_DEPENDS=()
|
MODULE_DEPENDS=()
|
||||||
ModuleLibIncs=(${LibSTRoot})
|
ModuleLibIncs=(${LibSTRoot})
|
||||||
MODULE_FILES=("srs_core" "srs_core_log" "srs_core_server" "srs_core_error" "srs_core_conn" "srs_core_conn_rtmp")
|
MODULE_FILES=("srs_core" "srs_core_log" "srs_core_server"
|
||||||
|
"srs_core_error" "srs_core_conn" "srs_core_client"
|
||||||
|
"srs_core_rtmp")
|
||||||
MODULE_DIR="src/core" . auto/modules.sh
|
MODULE_DIR="src/core" . auto/modules.sh
|
||||||
CORE_OBJS="${MODULE_OBJS[@]}"
|
CORE_OBJS="${MODULE_OBJS[@]}"
|
||||||
|
|
||||||
|
|
97
trunk/src/core/srs_core_client.cpp
Executable file
97
trunk/src/core/srs_core_client.cpp
Executable file
|
@ -0,0 +1,97 @@
|
||||||
|
/*
|
||||||
|
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_client.hpp>
|
||||||
|
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
#include <srs_core_error.hpp>
|
||||||
|
#include <srs_core_log.hpp>
|
||||||
|
#include <srs_core_rtmp.hpp>
|
||||||
|
|
||||||
|
SrsClient::SrsClient(SrsServer* srs_server, st_netfd_t client_stfd)
|
||||||
|
: SrsConnection(srs_server, client_stfd)
|
||||||
|
{
|
||||||
|
ip = NULL;
|
||||||
|
rtmp = new SrsRtmp(client_stfd);
|
||||||
|
}
|
||||||
|
|
||||||
|
SrsClient::~SrsClient()
|
||||||
|
{
|
||||||
|
if (ip) {
|
||||||
|
delete[] ip;
|
||||||
|
ip = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rtmp) {
|
||||||
|
delete rtmp;
|
||||||
|
rtmp = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int SrsClient::do_cycle()
|
||||||
|
{
|
||||||
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
|
if ((ret = get_peer_ip()) != ERROR_SUCCESS) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int SrsClient::get_peer_ip()
|
||||||
|
{
|
||||||
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
|
int fd = st_netfd_fileno(stfd);
|
||||||
|
|
||||||
|
// discovery client information
|
||||||
|
sockaddr_in addr;
|
||||||
|
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);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
SrsVerbose("get peer name success.");
|
||||||
|
|
||||||
|
// ip v4 or v6
|
||||||
|
char buf[INET6_ADDRSTRLEN];
|
||||||
|
memset(buf, 0, sizeof(buf));
|
||||||
|
|
||||||
|
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);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
SrsVerbose("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);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
50
trunk/src/core/srs_core_client.hpp
Executable file
50
trunk/src/core/srs_core_client.hpp
Executable file
|
@ -0,0 +1,50 @@
|
||||||
|
/*
|
||||||
|
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_CLIENT_HPP
|
||||||
|
#define SRS_CORE_CLIENT_HPP
|
||||||
|
|
||||||
|
/*
|
||||||
|
#include <srs_core_client.hpp>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <srs_core.hpp>
|
||||||
|
|
||||||
|
#include <srs_core_conn.hpp>
|
||||||
|
|
||||||
|
class SrsRtmp;
|
||||||
|
class SrsClient : public SrsConnection
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
char* ip;
|
||||||
|
SrsRtmp* rtmp;
|
||||||
|
public:
|
||||||
|
SrsClient(SrsServer* srs_server, st_netfd_t client_stfd);
|
||||||
|
virtual ~SrsClient();
|
||||||
|
protected:
|
||||||
|
virtual int do_cycle();
|
||||||
|
private:
|
||||||
|
virtual int get_peer_ip();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
|
@ -35,7 +35,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
class SrsServer;
|
class SrsServer;
|
||||||
class SrsConnection
|
class SrsConnection
|
||||||
{
|
{
|
||||||
private:
|
protected:
|
||||||
SrsServer* server;
|
SrsServer* server;
|
||||||
st_netfd_t stfd;
|
st_netfd_t stfd;
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -43,5 +43,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
#define ERROR_SOCKET_BIND 202
|
#define ERROR_SOCKET_BIND 202
|
||||||
#define ERROR_SOCKET_LISTEN 203
|
#define ERROR_SOCKET_LISTEN 203
|
||||||
#define ERROR_SOCKET_CLOSED 204
|
#define ERROR_SOCKET_CLOSED 204
|
||||||
|
#define ERROR_SOCKET_GET_PEER_NAME 205
|
||||||
|
#define ERROR_SOCKET_GET_PEER_IP 206
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -21,22 +21,13 @@ 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.
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <srs_core_conn_rtmp.hpp>
|
#include <srs_core_rtmp.hpp>
|
||||||
|
|
||||||
#include <srs_core_error.hpp>
|
SrsRtmp::SrsRtmp(st_netfd_t client_stfd)
|
||||||
|
|
||||||
SrsRtmpConnection::SrsRtmpConnection(SrsServer* srs_server, st_netfd_t client_stfd)
|
|
||||||
: SrsConnection(srs_server, client_stfd)
|
|
||||||
{
|
{
|
||||||
|
stfd = client_stfd;
|
||||||
}
|
}
|
||||||
|
|
||||||
SrsRtmpConnection::~SrsRtmpConnection()
|
SrsRtmp::~SrsRtmp()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
int SrsRtmpConnection::do_cycle()
|
|
||||||
{
|
|
||||||
int ret = ERROR_SUCCESS;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
|
@ -21,22 +21,24 @@ 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.
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef SRS_CORE_CONN_RTMP_HPP
|
#ifndef SRS_CORE_RTMP_HPP
|
||||||
#define SRS_CORE_CONN_RTMP_HPP
|
#define SRS_CORE_RTMP_HPP
|
||||||
|
|
||||||
/*
|
/*
|
||||||
#include <srs_core_conn_rtmp.hpp>
|
#include <srs_core_rtmp.hpp>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <srs_core_conn.hpp>
|
#include <srs_core.hpp>
|
||||||
|
|
||||||
class SrsRtmpConnection : public SrsConnection
|
#include <st.h>
|
||||||
|
|
||||||
|
class SrsRtmp
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
st_netfd_t stfd;
|
||||||
public:
|
public:
|
||||||
SrsRtmpConnection(SrsServer* srs_server, st_netfd_t client_stfd);
|
SrsRtmp(st_netfd_t client_stfd);
|
||||||
virtual ~SrsRtmpConnection();
|
virtual ~SrsRtmp();
|
||||||
protected:
|
|
||||||
virtual int do_cycle();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -33,7 +33,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
#include <srs_core_log.hpp>
|
#include <srs_core_log.hpp>
|
||||||
#include <srs_core_error.hpp>
|
#include <srs_core_error.hpp>
|
||||||
#include <srs_core_conn_rtmp.hpp>
|
#include <srs_core_client.hpp>
|
||||||
|
|
||||||
#define SERVER_LISTEN_BACKLOG 10
|
#define SERVER_LISTEN_BACKLOG 10
|
||||||
|
|
||||||
|
@ -76,7 +76,7 @@ int SrsServer::initialize()
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SrsServer::start(int port)
|
int SrsServer::listen(int port)
|
||||||
{
|
{
|
||||||
int ret = ERROR_SUCCESS;
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
|
@ -106,7 +106,7 @@ int SrsServer::start(int port)
|
||||||
}
|
}
|
||||||
SrsVerbose("bind socket success. fd=%d", fd);
|
SrsVerbose("bind socket success. fd=%d", fd);
|
||||||
|
|
||||||
if (listen(fd, SERVER_LISTEN_BACKLOG) == -1) {
|
if (::listen(fd, SERVER_LISTEN_BACKLOG) == -1) {
|
||||||
ret = ERROR_SOCKET_LISTEN;
|
ret = ERROR_SOCKET_LISTEN;
|
||||||
SrsError("listen socket error. ret=%d", ret);
|
SrsError("listen socket error. ret=%d", ret);
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -159,7 +159,7 @@ int SrsServer::accept_client(st_netfd_t client_stfd)
|
||||||
{
|
{
|
||||||
int ret = ERROR_SUCCESS;
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
SrsConnection* conn = new SrsRtmpConnection(this, client_stfd);
|
SrsConnection* conn = new SrsClient(this, client_stfd);
|
||||||
|
|
||||||
// directly enqueue, the cycle thread will remove the client.
|
// directly enqueue, the cycle thread will remove the client.
|
||||||
conns.push_back(conn);
|
conns.push_back(conn);
|
||||||
|
|
|
@ -46,7 +46,7 @@ public:
|
||||||
virtual ~SrsServer();
|
virtual ~SrsServer();
|
||||||
public:
|
public:
|
||||||
virtual int initialize();
|
virtual int initialize();
|
||||||
virtual int start(int port);
|
virtual int listen(int port);
|
||||||
virtual int cycle();
|
virtual int cycle();
|
||||||
virtual void remove(SrsConnection* conn);
|
virtual void remove(SrsConnection* conn);
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -25,8 +25,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
#include <srs_core_error.hpp>
|
#include <srs_core_error.hpp>
|
||||||
#include <srs_core_server.hpp>
|
#include <srs_core_server.hpp>
|
||||||
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
int main(int /*argc*/, char** /*argv*/){
|
int main(int /*argc*/, char** /*argv*/){
|
||||||
int ret = ERROR_SUCCESS;
|
int ret = ERROR_SUCCESS;
|
||||||
|
|
||||||
|
@ -36,7 +34,7 @@ int main(int /*argc*/, char** /*argv*/){
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((ret = server.start(19350)) != ERROR_SUCCESS) {
|
if ((ret = server.listen(19350)) != ERROR_SUCCESS) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,8 +10,10 @@ file
|
||||||
..\core\srs_core_server.cpp,
|
..\core\srs_core_server.cpp,
|
||||||
..\core\srs_core_conn.hpp,
|
..\core\srs_core_conn.hpp,
|
||||||
..\core\srs_core_conn.cpp,
|
..\core\srs_core_conn.cpp,
|
||||||
..\core\srs_core_conn_rtmp.hpp,
|
..\core\srs_core_client.hpp,
|
||||||
..\core\srs_core_conn_rtmp.cpp,
|
..\core\srs_core_client.cpp,
|
||||||
|
..\core\srs_core_rtmp.hpp,
|
||||||
|
..\core\srs_core_rtmp.cpp,
|
||||||
..\core\srs_core_log.hpp,
|
..\core\srs_core_log.hpp,
|
||||||
..\core\srs_core_log.cpp;
|
..\core\srs_core_log.cpp;
|
||||||
mainconfig
|
mainconfig
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue