winlin

get peer ip and add rtmp class

... ... @@ -82,7 +82,9 @@ LibSTfile="${LibSTRoot}/libst.a"
MODULE_ID="CORE"
MODULE_DEPENDS=()
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
CORE_OBJS="${MODULE_OBJS[@]}"
... ...
/*
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;
}
... ...
... ... @@ -21,22 +21,30 @@ 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_CONN_RTMP_HPP
#define SRS_CORE_CONN_RTMP_HPP
#ifndef SRS_CORE_CLIENT_HPP
#define SRS_CORE_CLIENT_HPP
/*
#include <srs_core_conn_rtmp.hpp>
#include <srs_core_client.hpp>
*/
#include <srs_core.hpp>
#include <srs_core_conn.hpp>
class SrsRtmpConnection : public SrsConnection
class SrsRtmp;
class SrsClient : public SrsConnection
{
private:
char* ip;
SrsRtmp* rtmp;
public:
SrsRtmpConnection(SrsServer* srs_server, st_netfd_t client_stfd);
virtual ~SrsRtmpConnection();
SrsClient(SrsServer* srs_server, st_netfd_t client_stfd);
virtual ~SrsClient();
protected:
virtual int do_cycle();
private:
virtual int get_peer_ip();
};
#endif
\ No newline at end of file
... ...
... ... @@ -35,7 +35,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
class SrsServer;
class SrsConnection
{
private:
protected:
SrsServer* server;
st_netfd_t stfd;
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_LISTEN 203
#define ERROR_SOCKET_CLOSED 204
#define ERROR_SOCKET_GET_PEER_NAME 205
#define ERROR_SOCKET_GET_PEER_IP 206
#endif
\ No newline at end of file
... ...
... ... @@ -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.
*/
#include <srs_core_conn_rtmp.hpp>
#include <srs_core_rtmp.hpp>
#include <srs_core_error.hpp>
SrsRtmpConnection::SrsRtmpConnection(SrsServer* srs_server, st_netfd_t client_stfd)
: SrsConnection(srs_server, client_stfd)
SrsRtmp::SrsRtmp(st_netfd_t client_stfd)
{
stfd = client_stfd;
}
SrsRtmpConnection::~SrsRtmpConnection()
SrsRtmp::~SrsRtmp()
{
}
int SrsRtmpConnection::do_cycle()
{
int ret = ERROR_SUCCESS;
return ret;
}
... ...
/*
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_RTMP_HPP
#define SRS_CORE_RTMP_HPP
/*
#include <srs_core_rtmp.hpp>
*/
#include <srs_core.hpp>
#include <st.h>
class SrsRtmp
{
private:
st_netfd_t stfd;
public:
SrsRtmp(st_netfd_t client_stfd);
virtual ~SrsRtmp();
};
#endif
\ No newline at end of file
... ...
... ... @@ -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_error.hpp>
#include <srs_core_conn_rtmp.hpp>
#include <srs_core_client.hpp>
#define SERVER_LISTEN_BACKLOG 10
... ... @@ -76,7 +76,7 @@ int SrsServer::initialize()
return ret;
}
int SrsServer::start(int port)
int SrsServer::listen(int port)
{
int ret = ERROR_SUCCESS;
... ... @@ -106,7 +106,7 @@ int SrsServer::start(int port)
}
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;
SrsError("listen socket error. ret=%d", ret);
return ret;
... ... @@ -159,7 +159,7 @@ int SrsServer::accept_client(st_netfd_t client_stfd)
{
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.
conns.push_back(conn);
... ...
... ... @@ -46,7 +46,7 @@ public:
virtual ~SrsServer();
public:
virtual int initialize();
virtual int start(int port);
virtual int listen(int port);
virtual int cycle();
virtual void remove(SrsConnection* conn);
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_server.hpp>
#include <unistd.h>
int main(int /*argc*/, char** /*argv*/){
int ret = ERROR_SUCCESS;
... ... @@ -36,7 +34,7 @@ int main(int /*argc*/, char** /*argv*/){
return ret;
}
if ((ret = server.start(19350)) != ERROR_SUCCESS) {
if ((ret = server.listen(19350)) != ERROR_SUCCESS) {
return ret;
}
... ...
... ... @@ -10,8 +10,10 @@ file
..\core\srs_core_server.cpp,
..\core\srs_core_conn.hpp,
..\core\srs_core_conn.cpp,
..\core\srs_core_conn_rtmp.hpp,
..\core\srs_core_conn_rtmp.cpp,
..\core\srs_core_client.hpp,
..\core\srs_core_client.cpp,
..\core\srs_core_rtmp.hpp,
..\core\srs_core_rtmp.cpp,
..\core\srs_core_log.hpp,
..\core\srs_core_log.cpp;
mainconfig
... ...