Rename a project

master
Ryan Lee 2016-08-12 12:38:53 +09:00
parent d41add5722
commit cdd1ccfa41
27 changed files with 1151 additions and 400 deletions

View File

@ -1,18 +1,18 @@
# ============================================================================
#
# CMake file for ThroughNet
# CMake file for PeerConnect
#
# ============================================================================
cmake_minimum_required(VERSION 2.8)
project(throughnet)
project(peerconnect)
# ============================================================================
# The version number.
# ============================================================================
set(PACKAGE "throughnet")
set(PACKAGE "peerconnect")
set(CPACK_PACKAGE_NAME "${PACKAGE}")
set(CPACK_PACKAGE_VERSION_MAJOR "0")
set(CPACK_PACKAGE_VERSION_MINOR "0")
@ -21,10 +21,10 @@ set(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSIO
#set(CPACK_PACKAGE_VENDOR "")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "${PACKAGE} ${PACKAGE_VERSION}")
option(TN_WITH_STATIC "Build the static version of the library" ON)
option(TN_WITH_SHARED "Build the shared version of the library" ON)
option(PC_WITH_STATIC "Build the static version of the library" ON)
option(PC_WITH_SHARED "Build the shared version of the library" ON)
if (NOT (TN_WITH_STATIC OR TN_WITH_SHARED))
if (NOT (PC_WITH_STATIC OR PC_WITH_SHARED))
message(FATAL_ERROR "Makes no sense to compile with neither static nor shared libraries.")
endif()
@ -81,7 +81,7 @@ find_package(Asio)
# Headers and sources.
# ============================================================================
set(HEADERS
"src/throughnet.h"
"src/peerconnect.h"
"src/control.h"
"src/controlobserver.h"
"src/peer.h"
@ -90,7 +90,7 @@ set(HEADERS
)
set(SOURCES
"src/throughnet.cc"
"src/peerconnect.cc"
"src/control.cc"
"src/peer.cc"
"src/signalconnection.cc"
@ -101,12 +101,12 @@ set(SOURCES
# Target settings
# ============================================================================
set(_TN_INTERNAL_DEFINES
set(_PC_INTERNAL_DEFINES
${WEBRTC_DEFINES}
${WEBSOCKETPP_DEFINES}
)
set(_TN_INTERNAL_INCLUDE_DIR
set(_PC_INTERNAL_INCLUDE_DIR
"${WEBRTC_INCLUDE_DIR}"
"${ASIO_INCLUDE_DIR}"
"${WEBSOCKETPP_INCLUDE_DIIR}"
@ -114,79 +114,79 @@ set(_TN_INTERNAL_INCLUDE_DIR
"${PROJECT_SOURCE_DIR}/src"
)
set(_TN_INTERNAL_LIBRARIES
set(_PC_INTERNAL_LIBRARIES
"${WEBRTC_LIBRARIES_INTERNAL}"
"${WEBRTC_LIBRARIES_EXTERNAL}"
)
set(TN_INCLUDE_DIRECTORY
set(PC_INCLUDE_DIRECTORY
"${PROJECT_BINARY_DIR}"
"${PROJECT_SOURCE_DIR}/src"
)
if (TN_WITH_STATIC)
add_library(throughnet STATIC ${HEADERS} ${SOURCES})
if (PC_WITH_STATIC)
add_library(peerconnect STATIC ${HEADERS} ${SOURCES})
target_compile_definitions(throughnet PRIVATE ${_TN_INTERNAL_DEFINES})
target_include_directories(throughnet PRIVATE ${_TN_INTERNAL_INCLUDE_DIR} )
target_compile_definitions(peerconnect PRIVATE ${_PC_INTERNAL_DEFINES})
target_include_directories(peerconnect PRIVATE ${_PC_INTERNAL_INCLUDE_DIR} )
if (WIN32)
# Windows uses the same .lib ending for static libraries and shared
# library linker files, so rename the static library.
set_target_properties(throughnet PROPERTIES OUTPUT_NAME throughnet_static)
set_target_properties(peerconnect PROPERTIES OUTPUT_NAME peerconnect_static)
endif()
if (MSVC)
MERGE_STATIC_LIBRARIES( throughnet
MERGE_STATIC_LIBRARIES( peerconnect
"${WEBRTC_LIBRARIES_INTERNAL_RELEASE}"
"${WEBRTC_LIBRARIES_INTERNAL_DEBUG}")
elseif (APPLE)
MERGE_STATIC_LIBRARIES(throughnet
MERGE_STATIC_LIBRARIES(peerconnect
"${WEBRTC_LIBRARIES_INTERNAL_RELEASE}"
"${WEBRTC_LIBRARIES_INTERNAL_DEBUG}")
elseif (UNIX)
MERGE_STATIC_LIBRARIES(throughnet
MERGE_STATIC_LIBRARIES(peerconnect
"${WEBRTC_LIBRARIES_INTERNAL_RELEASE}"
"${WEBRTC_LIBRARIES_INTERNAL_DEBUG}")
endif(MSVC)
endif()
if (TN_WITH_SHARED)
add_library(throughnet_shared SHARED ${HEADERS} ${SOURCES})
if (PC_WITH_SHARED)
add_library(peerconnect_shared SHARED ${HEADERS} ${SOURCES})
target_compile_definitions(throughnet_shared PRIVATE ${_TN_INTERNAL_DEFINES})
target_include_directories(throughnet_shared PRIVATE ${_TN_INTERNAL_INCLUDE_DIR} )
target_compile_definitions(peerconnect_shared PRIVATE ${_PC_INTERNAL_DEFINES})
target_include_directories(peerconnect_shared PRIVATE ${_PC_INTERNAL_INCLUDE_DIR} )
set_target_properties(throughnet_shared PROPERTIES OUTPUT_NAME throughnet)
target_link_libraries(throughnet_shared ${_TN_INTERNAL_LIBRARIES})
set_target_properties(peerconnect_shared PROPERTIES OUTPUT_NAME peerconnect)
target_link_libraries(peerconnect_shared ${_PC_INTERNAL_LIBRARIES})
if (WIN32)
# # Compile as DLL (export function declarations)
# set_property(
# TARGET throughnet_shared
# TARGET peerconnect_shared
# PROPERTY COMPILE_DEFINITIONS)
endif()
if (APPLE)
set_property(TARGET throughnet_shared PROPERTY MACOSX_RPATH YES)
set_property(TARGET peerconnect_shared PROPERTY MACOSX_RPATH YES)
endif()
endif()
# ============================================================================
# Variables for parent project including throughnet using add_subdirectory()
# Variables for parent project including peerconnect using add_subdirectory()
# ============================================================================
set(THROUGHNET_INCLUDE_DIRECTORY ${TN_INCLUDE_DIRECTORY}
CACHE STRING "ThroughNet include directories")
if (TN_WITH_STATIC)
set(THROUGHNET_LIBRARIES_STATIC throughnet ${WEBRTC_LIBRARIES_EXTERNAL}
CACHE STRING "ThroughNet static library")
set(PEERCONNECT_INCLUDE_DIRECTORY ${PC_INCLUDE_DIRECTORY}
CACHE STRING "PeerConnect include directories")
if (PC_WITH_STATIC)
set(PEERCONNECT_LIBRARIES_STATIC peerconnect ${WEBRTC_LIBRARIES_EXTERNAL}
CACHE STRING "PeerConnect static library")
endif()
if (TN_WITH_SHARED)
set(THROUGHNET_LIBRARIES_SHARED throughnet_shared ${WEBRTC_LIBRARIES_EXTERNAL}
CACHE STRING "ThroughNet shared library")
if (PC_WITH_SHARED)
set(PEERCONNECT_LIBRARIES_SHARED peerconnect_shared ${WEBRTC_LIBRARIES_EXTERNAL}
CACHE STRING "PeerConnect shared library")
endif()
@ -195,10 +195,10 @@ endif()
# ============================================================================
add_executable(test_main src/test/test_main.cc)
add_dependencies(test_main throughnet)
add_dependencies(test_main peerconnect)
target_include_directories(test_main PRIVATE ${THROUGHNET_INCLUDE_DIRECTORY})
target_link_libraries(test_main ${THROUGHNET_LIBRARIES_STATIC})
target_include_directories(test_main PRIVATE ${PEERCONNECT_INCLUDE_DIRECTORY})
target_link_libraries(test_main ${PEERCONNECT_LIBRARIES_STATIC})
set_target_properties (test_main PROPERTIES FOLDER test)
add_test(test_main test_main)
@ -210,22 +210,22 @@ add_test(test_main test_main)
# echo server
add_executable(echo_server examples/echo_server/main.cc)
add_dependencies(echo_server throughnet)
target_include_directories(echo_server PRIVATE ${THROUGHNET_INCLUDE_DIRECTORY})
target_link_libraries(echo_server ${THROUGHNET_LIBRARIES_STATIC})
add_dependencies(echo_server peerconnect)
target_include_directories(echo_server PRIVATE ${PEERCONNECT_INCLUDE_DIRECTORY})
target_link_libraries(echo_server ${PEERCONNECT_LIBRARIES_STATIC})
set_target_properties (echo_server PROPERTIES FOLDER examples)
# echo client
add_executable(echo_client examples/echo_client/main.cc)
add_dependencies(echo_client throughnet)
target_include_directories(echo_client PRIVATE ${THROUGHNET_INCLUDE_DIRECTORY})
target_link_libraries(echo_client ${THROUGHNET_LIBRARIES_STATIC})
add_dependencies(echo_client peerconnect)
target_include_directories(echo_client PRIVATE ${PEERCONNECT_INCLUDE_DIRECTORY})
target_link_libraries(echo_client ${PEERCONNECT_LIBRARIES_STATIC})
set_target_properties (echo_client PROPERTIES FOLDER examples)
# p2p netcat
add_executable(p2p_netcat examples/p2p_netcat/main.cc)
add_dependencies(p2p_netcat throughnet)
target_include_directories(p2p_netcat PRIVATE ${THROUGHNET_INCLUDE_DIRECTORY})
target_link_libraries(p2p_netcat ${THROUGHNET_LIBRARIES_STATIC})
add_dependencies(p2p_netcat peerconnect)
target_include_directories(p2p_netcat PRIVATE ${PEERCONNECT_INCLUDE_DIRECTORY})
target_link_libraries(p2p_netcat ${PEERCONNECT_LIBRARIES_STATIC})
set_target_properties (p2p_netcat PROPERTIES FOLDER examples)
set_target_properties (p2p_netcat PROPERTIES OUTPUT_NAME p2pnc)
set_target_properties (p2p_netcat PROPERTIES OUTPUT_NAME pnc)

View File

@ -1,13 +1,13 @@
/*
* Copyright 2016 The ThroughNet Project Authors. All rights reserved.
* Copyright 2016 The PeerConnect Project Authors. All rights reserved.
*
* Ryan Lee (ryan.lee at throughnet.com)
* Ryan Lee
*/
#include <iostream>
#include <string>
#include "throughnet.h"
#include "peerconnect.h"
using namespace std;
void usage(const char* prg);
@ -22,29 +22,29 @@ int main(int argc, char *argv[]) {
string name = argv[1];
Throughnet tn;
PeerConnect pc;
tn.On("signin", function_tn(Throughnet* tn, string id) {
tn->Connect(name);
pc.On("signin", function_pc(PeerConnect* pc, string id) {
pc->Connect(name);
});
tn.On("connect", function_tn(Throughnet* tn, string id) {
tn->Send(id, "Hello world");
pc.On("connect", function_pc(PeerConnect* pc, string id) {
pc->Send(id, "Hello world");
std::cout << "Sent 'Hello world' message to " << id << "." << std::endl;
});
tn.On("disconnect", function_tn(Throughnet* tn, string id) {
pc.On("disconnect", function_pc(PeerConnect* pc, string id) {
std::cout << "Peer " << id << " has been disconnected" << std::endl;
Throughnet::Stop();
PeerConnect::Stop();
});
tn.OnMessage(function_tn(Throughnet* tn, string id, Throughnet::Buffer& data) {
pc.OnMessage(function_pc(PeerConnect* pc, string id, PeerConnect::Buffer& data) {
std::cout << "Message '" << std::string(data.buf_, data.size_) <<
"' has been received." << std::endl;
});
tn.SignIn();
Throughnet::Run();
pc.SignIn();
PeerConnect::Run();
return 0;
}

View File

@ -1,13 +1,13 @@
/*
* Copyright 2016 The ThroughNet Project Authors. All rights reserved.
* Copyright 2016 The PeerConnect Project Authors. All rights reserved.
*
* Ryan Lee (ryan.lee at throughnet.com)
* Ryan Lee
*/
#include <iostream>
#include <string>
#include "throughnet.h"
#include "peerconnect.h"
using namespace std;
void usage(const char* prg);
@ -19,24 +19,24 @@ int main(int argc, char *argv[]) {
}
string name = argv[1];
Throughnet tn;
PeerConnect pc;
tn.On("connect", function_tn(Throughnet* tn, string id) {
pc.On("connect", function_pc(PeerConnect* pc, string id) {
std::cout << "Peer " << id << " has been connected." << std::endl;
});
tn.On("disconnect", function_tn(Throughnet* tn, string id) {
pc.On("disconnect", function_pc(PeerConnect* pc, string id) {
std::cout << "Peer " << id << " has been disconnected." << std::endl;
});
tn.OnMessage(function_tn(Throughnet* tn, string id, Throughnet::Buffer& data) {
pc.OnMessage(function_pc(PeerConnect* pc, string id, PeerConnect::Buffer& data) {
std::cout << "Message " << std::string(data.buf_, data.size_) <<
" has been received." << std::endl;
tn->Send(id, data.buf_, data.size_);
pc->Send(id, data.buf_, data.size_);
});
tn.SignIn(name);
Throughnet::Run();
pc.SignIn(name);
PeerConnect::Run();
return 0;
}

View File

@ -1,7 +1,7 @@
/*
* Copyright 2016 The ThroughNet Project Authors. All rights reserved.
* Copyright 2016 The PeerConnect Project Authors. All rights reserved.
*
* Ryan Lee (ryan.lee at throughnet.com)
* Ryan Lee
*/
#include <mutex>
@ -11,14 +11,14 @@
#include <string>
#include <stdlib.h>
#ifndef WIN32
#include <unistd.h>
#include <sys/uio.h>
#else
#include <io.h>
#endif
#include <fcntl.h>
#include <signal.h>
#include "throughnet.h"
#include <unistd.h>
#include <sys/uio.h>
#else
#include <io.h>
#endif
#include <fcntl.h>
#include <signal.h>
#include "peerconnect.h"
#ifdef WIN32
#pragma warning(disable:4996)
@ -28,12 +28,12 @@ using namespace std;
bool parse_args(int argc, char* argv[], std::string& alias, std::string& connect_to, bool& server_mode);
void usage(const char* prg);
void read_stdin(Throughnet* tn, std::string id);
void read_stdin(PeerConnect* pc, std::string id);
bool write_stdout(const char* buf, int len);
void set_mode(Throughnet* tn);
void set_mode(PeerConnect* pc);
void ctrlc_handler(int s);
static Throughnet *tn_;
static PeerConnect *pc_;
int main(int argc, char *argv[]) {
@ -55,43 +55,43 @@ int main(int argc, char *argv[]) {
// Set event handlers
//
Throughnet tn;
set_mode(&tn);
PeerConnect pc;
set_mode(&pc);
tn.On("signin", function_tn(Throughnet* tn, string id) {
pc.On("signin", function_pc(PeerConnect* pc, string id) {
if (server_mode) {
std::cerr << "Listening " << id << std::endl;
}
else {
std::cerr << "Connecting to " << connec_to << std::endl;
if (!server_mode) tn->Connect(connec_to);
if (!server_mode) pc->Connect(connec_to);
}
});
tn.On("connect", function_tn(Throughnet* tn, string id) {
pc.On("connect", function_pc(PeerConnect* pc, string id) {
std::cerr << "Connected" << std::endl;
std::thread(read_stdin, tn, id).detach();
std::thread(read_stdin, pc, id).detach();
});
tn.On("disconnect", function_tn(Throughnet* tn, string id) {
pc.On("disconnect", function_pc(PeerConnect* pc, string id) {
if (server_mode)
std::cerr << "Disconnected" << std::endl;
else
tn->SignOut();
pc->SignOut();
});
tn.On("signout", function_tn(Throughnet* tn, string id) {
Throughnet::Stop();
pc.On("signout", function_pc(PeerConnect* pc, string id) {
PeerConnect::Stop();
});
tn.On("error", function_tn(Throughnet* tn, string id){
std::cerr << tn->GetErrorMessage() << std::endl;
Throughnet::Stop();
pc.On("error", function_pc(PeerConnect* pc, string id){
std::cerr << pc->GetErrorMessage() << std::endl;
PeerConnect::Stop();
});
tn.OnMessage(function_tn(Throughnet* tn, string id, Throughnet::Buffer& data) {
pc.OnMessage(function_pc(PeerConnect* pc, string id, PeerConnect::Buffer& data) {
if (!write_stdout(data.buf_, data.size_)) {
tn->Disconnect(id);
pc->Disconnect(id);
}
});
@ -99,76 +99,76 @@ int main(int argc, char *argv[]) {
// Sign in as anonymous user
//
tn.SignIn(alias, "anonymous", "nopassword");
pc.SignIn(alias, "anonymous", "nopassword");
Throughnet::Run();
PeerConnect::Run();
return 0;
}
#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif
#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif
#ifndef STDERR_FILENO
#define STDERR_FILENO 2
#endif
#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif
void read_stdin(Throughnet* tn, std::string id)
{
int nbytes;
char buf[32*1024];
for (;;) {
nbytes = read(STDIN_FILENO, buf, sizeof(buf));
if (nbytes <= 0) {
tn->Disconnect(id);
return;
}
if (!tn->SyncSend(id, buf, nbytes)) {
return;
}
}
#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif
#ifndef STDERR_FILENO
#define STDERR_FILENO 2
#endif
void read_stdin(PeerConnect* pc, std::string id)
{
int nbytes;
char buf[32*1024];
for (;;) {
nbytes = read(STDIN_FILENO, buf, sizeof(buf));
if (nbytes <= 0) {
pc->Disconnect(id);
return;
}
if (!pc->SyncSend(id, buf, nbytes)) {
return;
}
}
}
bool write_stdout(const char* buf, int len)
{
int nbytes;
int remain = len;
for (;;) {
nbytes = write(STDOUT_FILENO, buf, len);
if (nbytes <= 0) {
return false;
}
remain -= nbytes;
if (remain <= 0) break;
}
return true;
}
void ctrlc_handler(int s) {
std::cerr << "Terminating..." << std::endl;
tn_->SignOut();
}
bool write_stdout(const char* buf, int len)
{
int nbytes;
int remain = len;
void set_mode(Throughnet* tn)
{
tn_ = tn;
for (;;) {
nbytes = write(STDOUT_FILENO, buf, len);
if (nbytes <= 0) {
return false;
}
remain -= nbytes;
if (remain <= 0) break;
}
return true;
}
void ctrlc_handler(int s) {
std::cerr << "Terminating..." << std::endl;
pc_->SignOut();
}
void set_mode(PeerConnect* pc)
{
pc_ = pc;
signal(SIGINT, ctrlc_handler);
#ifdef WIN32
if (isatty(STDIN_FILENO)) setmode(STDIN_FILENO, O_TEXT);
else setmode(STDIN_FILENO, O_BINARY);
setmode(STDOUT_FILENO, O_BINARY);
#endif
#ifdef WIN32
if (isatty(STDIN_FILENO)) setmode(STDIN_FILENO, O_TEXT);
else setmode(STDIN_FILENO, O_BINARY);
setmode(STDOUT_FILENO, O_BINARY);
#endif
}
bool parse_args(int argc, char* argv[], std::string& alias, std::string& connect_to, bool& server_mode) {
@ -186,7 +186,7 @@ bool parse_args(int argc, char* argv[], std::string& alias, std::string& connect
}
void usage(const char* prg) {
std::cerr << "P2P netcat version 0.1 (http://github.com/throughnet/throughnet)" << std::endl << std::endl;
std::cerr << "P2P netcat version 0.1 (http://github.com/peerconnect/peerconnect)" << std::endl << std::endl;
std::cerr << "Usage: " << prg << " [-l] name" << std::endl << std::endl;
std::cerr << " Options:" << std::endl;
std::cerr << " -l Listen mode, for inbound connections" << std::endl << std::endl;

View File

@ -1,7 +1,7 @@
/*
* Copyright 2016 The ThroughNet Project Authors. All rights reserved.
* Copyright 2016 The PeerConnect Project Authors. All rights reserved.
*
* Ryan Lee (ryan.lee at throughnet.com)
* Ryan Lee
*/
#include "control.h"
@ -23,7 +23,7 @@ namespace rtc {
} // namespace rtc
#endif // WEBRTC_POSIX
namespace tn {
namespace pc {
Control::Control()
: Control(nullptr){
@ -77,7 +77,7 @@ void Control::SignIn(const std::string& user_id, const std::string& user_passwor
// 2. Send signin command to signal server
// 3. Send createchannel command to signal server (channel name is id or alias)
// Other peers connect to this peer by channel name, that is id or alias
// 4. Generate 'signedin' event to Throughnet
// 4. Generate 'signedin' event to PeerConnect
if (signal_.get() == NULL) {
LOG(LS_ERROR) << "SignIn failed, no signal server";
@ -641,4 +641,4 @@ void Control::DisconnectPeer(const std::string id) {
}
} // namespace tn
} // namespace pc

View File

@ -1,11 +1,11 @@
/*
* Copyright 2016 The ThroughNet Project Authors. All rights reserved.
* Copyright 2016 The PeerConnect Project Authors. All rights reserved.
*
* Ryan Lee (ryan.lee at throughnet.com)
* Ryan Lee
*/
#ifndef __THROUGHNET_CONTROL_H__
#define __THROUGHNET_CONTROL_H__
#ifndef __PEERCONNECT_CONTROL_H__
#define __PEERCONNECT_CONTROL_H__
#include <memory>
@ -17,7 +17,7 @@
#include "fakeaudiocapturemodule.h"
namespace tn {
namespace pc {
class Control
: public PeerObserver,
@ -113,20 +113,20 @@ protected:
private:
enum {
MSG_COMMAND_RECEIVED, // Command has been received from signal server
MSG_DISCONNECT, // Queue disconnection request (+subsequent peer disconnection)
MSG_DISCONNECT_PEER, // Queue peer disconnection request
MSG_ON_PEER_DISCONNECTED, // Queue onpeerdisconnected event
MSG_ON_PEER_CHANNEL_CLOSED, // Queue onchanneldisconnected event
MSG_SIGNOUT, // Queue signout request
MSG_SIGNAL_SERVER_CLOSED // Connection to signal server has been closed
enum {
MSG_COMMAND_RECEIVED, // Command has been received from signal server
MSG_DISCONNECT, // Queue disconnection request (+subsequent peer disconnection)
MSG_DISCONNECT_PEER, // Queue peer disconnection request
MSG_ON_PEER_DISCONNECTED, // Queue onpeerdisconnected event
MSG_ON_PEER_CHANNEL_CLOSED, // Queue onchanneldisconnected event
MSG_SIGNOUT, // Queue signout request
MSG_SIGNAL_SERVER_CLOSED // Connection to signal server has been closed
};
struct ControlMessageData : public rtc::MessageData {
explicit ControlMessageData(Json::Value data, std::shared_ptr<Control> ref) : data_json_(data), ref_(ref) {}
explicit ControlMessageData(const std::string data, std::shared_ptr<Control> ref) : data_string_(data), ref_(ref) {}
explicit ControlMessageData(const uint32_t data, std::shared_ptr<Control> ref) : data_int32_(data), ref_(ref) {}
explicit ControlMessageData(Json::Value data, std::shared_ptr<Control> ref) : data_json_(data), ref_(ref) {}
explicit ControlMessageData(const std::string data, std::shared_ptr<Control> ref) : data_string_(data), ref_(ref) {}
explicit ControlMessageData(const uint32_t data, std::shared_ptr<Control> ref) : data_int32_(data), ref_(ref) {}
Json::Value data_json_;
std::string data_string_;
uint32_t data_int32_;
@ -140,6 +140,6 @@ private:
std::shared_ptr<Control> ref_;
};
} // namespace tn
} // namespace pc
#endif // __THROUGHNET_CONTROL_H__
#endif // __PEERCONNECT_CONTROL_H__

View File

@ -1,13 +1,13 @@
/*
* Copyright 2016 The ThroughNet Project Authors. All rights reserved.
* Copyright 2016 The PeerConnect Project Authors. All rights reserved.
*
* Ryan Lee (ryan.lee at throughnet.com)
* Ryan Lee
*/
#ifndef __THROUGHNET_CONTROLOBSERVER_H__
#define __THROUGHNET_CONTROLOBSERVER_H__
#ifndef __PEERCONNECT_CONTROLOBSERVER_H__
#define __PEERCONNECT_CONTROLOBSERVER_H__
namespace tn {
namespace pc {
class ControlObserver {
public:
@ -20,6 +20,6 @@ public:
virtual void OnError(const std::string id, const std::string& reason) = 0;
};
} // namespace tn
} // namespace pc
#endif // __THROUGHNET_CONTROLOBSERVER_H__
#endif // __PEERCONNECT_CONTROLOBSERVER_H__

0
src/libs/asio/compile Executable file → Normal file
View File

0
src/libs/asio/config.guess vendored Executable file → Normal file
View File

0
src/libs/asio/config.sub vendored Executable file → Normal file
View File

0
src/libs/asio/configure vendored Executable file → Normal file
View File

0
src/libs/asio/depcomp Executable file → Normal file
View File

View File

@ -16,7 +16,7 @@
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
//
// Begining of Throughnet patch to support BoringSSL instead of OpenSSL
// Begining of PeerConnect patch to support BoringSSL instead of OpenSSL
//
#if defined(WIN32) || defined(_WIN32)

0
src/libs/asio/install-sh Executable file → Normal file
View File

0
src/libs/asio/missing Executable file → Normal file
View File

0
src/libs/asio/src/tools/handlerviz.pl Executable file → Normal file
View File

0
src/libs/asio/test-driver Executable file → Normal file
View File

View File

@ -0,0 +1,104 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef WEBSOCKETPP_TRANSPORT_DEBUG_BASE_HPP
#define WEBSOCKETPP_TRANSPORT_DEBUG_BASE_HPP
#include <websocketpp/common/system_error.hpp>
#include <websocketpp/common/cpp11.hpp>
#include <string>
namespace websocketpp {
namespace transport {
/// Debug transport policy that is used for various mocking and stubbing duties
/// in unit tests.
namespace debug {
/// debug transport errors
namespace error {
enum value {
/// Catch-all error for transport policy errors that don't fit in other
/// categories
general = 1,
/// not implemented
not_implemented,
invalid_num_bytes,
double_read
};
/// debug transport error category
class category : public lib::error_category {
public:
category() {}
char const * name() const _WEBSOCKETPP_NOEXCEPT_TOKEN_ {
return "websocketpp.transport.debug";
}
std::string message(int value) const {
switch(value) {
case general:
return "Generic stub transport policy error";
case not_implemented:
return "feature not implemented";
case invalid_num_bytes:
return "Invalid number of bytes";
case double_read:
return "Read while another read was outstanding";
default:
return "Unknown";
}
}
};
/// Get a reference to a static copy of the debug transport error category
inline lib::error_category const & get_category() {
static category instance;
return instance;
}
/// Get an error code with the given value and the debug transport category
inline lib::error_code make_error_code(error::value e) {
return lib::error_code(static_cast<int>(e), get_category());
}
} // namespace error
} // namespace debug
} // namespace transport
} // namespace websocketpp
_WEBSOCKETPP_ERROR_CODE_ENUM_NS_START_
template<> struct is_error_code_enum<websocketpp::transport::debug::error::value>
{
static bool const value = true;
};
_WEBSOCKETPP_ERROR_CODE_ENUM_NS_END_
#endif // WEBSOCKETPP_TRANSPORT_DEBUG_BASE_HPP

View File

@ -0,0 +1,412 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef WEBSOCKETPP_TRANSPORT_DEBUG_CON_HPP
#define WEBSOCKETPP_TRANSPORT_DEBUG_CON_HPP
#include <websocketpp/transport/debug/base.hpp>
#include <websocketpp/transport/base/connection.hpp>
#include <websocketpp/uri.hpp>
#include <websocketpp/logger/levels.hpp>
#include <websocketpp/common/connection_hdl.hpp>
#include <websocketpp/common/memory.hpp>
#include <websocketpp/common/platforms.hpp>
#include <string>
#include <vector>
namespace websocketpp {
namespace transport {
namespace debug {
/// Empty timer class to stub out for timer functionality that stub
/// transport doesn't support
struct timer {
void cancel() {}
};
template <typename config>
class connection : public lib::enable_shared_from_this< connection<config> > {
public:
/// Type of this connection transport component
typedef connection<config> type;
/// Type of a shared pointer to this connection transport component
typedef lib::shared_ptr<type> ptr;
/// transport concurrency policy
typedef typename config::concurrency_type concurrency_type;
/// Type of this transport's access logging policy
typedef typename config::alog_type alog_type;
/// Type of this transport's error logging policy
typedef typename config::elog_type elog_type;
// Concurrency policy types
typedef typename concurrency_type::scoped_lock_type scoped_lock_type;
typedef typename concurrency_type::mutex_type mutex_type;
typedef lib::shared_ptr<timer> timer_ptr;
explicit connection(bool is_server, alog_type & alog, elog_type & elog)
: m_reading(false), m_is_server(is_server), m_alog(alog), m_elog(elog)
{
m_alog.write(log::alevel::devel,"debug con transport constructor");
}
/// Get a shared pointer to this component
ptr get_shared() {
return type::shared_from_this();
}
/// Set whether or not this connection is secure
/**
* Todo: docs
*
* @since 0.3.0-alpha4
*
* @param value Whether or not this connection is secure.
*/
void set_secure(bool) {}
/// Tests whether or not the underlying transport is secure
/**
* TODO: docs
*
* @return Whether or not the underlying transport is secure
*/
bool is_secure() const {
return false;
}
/// Set uri hook
/**
* Called by the endpoint as a connection is being established to provide
* the uri being connected to to the transport layer.
*
* Implementation is optional and can be ignored if the transport has no
* need for this information.
*
* @since 0.6.0
*
* @param u The uri to set
*/
void set_uri(uri_ptr) {}
/// Set human readable remote endpoint address
/**
* Sets the remote endpoint address returned by `get_remote_endpoint`. This
* value should be a human readable string that describes the remote
* endpoint. Typically an IP address or hostname, perhaps with a port. But
* may be something else depending on the nature of the underlying
* transport.
*
* If none is set a default is returned.
*
* @since 0.3.0-alpha4
*
* @param value The remote endpoint address to set.
*/
void set_remote_endpoint(std::string) {}
/// Get human readable remote endpoint address
/**
* TODO: docs
*
* This value is used in access and error logs and is available to the end
* application for including in user facing interfaces and messages.
*
* @return A string identifying the address of the remote endpoint
*/
std::string get_remote_endpoint() const {
return "unknown (debug transport)";
}
/// Get the connection handle
/**
* @return The handle for this connection.
*/
connection_hdl get_handle() const {
return connection_hdl();
}
/// Call back a function after a period of time.
/**
* Timers are not implemented in this transport. The timer pointer will
* always be empty. The handler will never be called.
*
* @param duration Length of time to wait in milliseconds
* @param callback The function to call back when the timer has expired
* @return A handle that can be used to cancel the timer if it is no longer
* needed.
*/
timer_ptr set_timer(long, timer_handler handler) {
m_alog.write(log::alevel::devel,"debug connection set timer");
m_timer_handler = handler;
return timer_ptr();
}
/// Manual input supply (read all)
/**
* Similar to read_some, but continues to read until all bytes in the
* supplied buffer have been read or the connection runs out of read
* requests.
*
* This method still may not read all of the bytes in the input buffer. if
* it doesn't it indicates that the connection was most likely closed or
* is in an error state where it is no longer accepting new input.
*
* @since 0.3.0
*
* @param buf Char buffer to read into the websocket
* @param len Length of buf
* @return The number of characters from buf actually read.
*/
size_t read_all(char const * buf, size_t len) {
size_t total_read = 0;
size_t temp_read = 0;
do {
temp_read = this->read_some_impl(buf+total_read,len-total_read);
total_read += temp_read;
} while (temp_read != 0 && total_read < len);
return total_read;
}
// debug stuff to invoke the async handlers
void expire_timer(lib::error_code const & ec) {
m_timer_handler(ec);
}
void fullfil_write() {
m_write_handler(lib::error_code());
}
protected:
/// Initialize the connection transport
/**
* Initialize the connection's transport component.
*
* @param handler The `init_handler` to call when initialization is done
*/
void init(init_handler handler) {
m_alog.write(log::alevel::devel,"debug connection init");
handler(lib::error_code());
}
/// Initiate an async_read for at least num_bytes bytes into buf
/**
* Initiates an async_read request for at least num_bytes bytes. The input
* will be read into buf. A maximum of len bytes will be input. When the
* operation is complete, handler will be called with the status and number
* of bytes read.
*
* This method may or may not call handler from within the initial call. The
* application should be prepared to accept either.
*
* The application should never call this method a second time before it has
* been called back for the first read. If this is done, the second read
* will be called back immediately with a double_read error.
*
* If num_bytes or len are zero handler will be called back immediately
* indicating success.
*
* @param num_bytes Don't call handler until at least this many bytes have
* been read.
* @param buf The buffer to read bytes into
* @param len The size of buf. At maximum, this many bytes will be read.
* @param handler The callback to invoke when the operation is complete or
* ends in an error
*/
void async_read_at_least(size_t num_bytes, char * buf, size_t len,
read_handler handler)
{
std::stringstream s;
s << "debug_con async_read_at_least: " << num_bytes;
m_alog.write(log::alevel::devel,s.str());
if (num_bytes > len) {
handler(make_error_code(error::invalid_num_bytes),size_t(0));
return;
}
if (m_reading == true) {
handler(make_error_code(error::double_read),size_t(0));
return;
}
if (num_bytes == 0 || len == 0) {
handler(lib::error_code(),size_t(0));
return;
}
m_buf = buf;
m_len = len;
m_bytes_needed = num_bytes;
m_read_handler = handler;
m_cursor = 0;
m_reading = true;
}
/// Asyncronous Transport Write
/**
* Write len bytes in buf to the output stream. Call handler to report
* success or failure. handler may or may not be called during async_write,
* but it must be safe for this to happen.
*
* Will return 0 on success.
*
* @param buf buffer to read bytes from
* @param len number of bytes to write
* @param handler Callback to invoke with operation status.
*/
void async_write(char const *, size_t, write_handler handler) {
m_alog.write(log::alevel::devel,"debug_con async_write");
m_write_handler = handler;
}
/// Asyncronous Transport Write (scatter-gather)
/**
* Write a sequence of buffers to the output stream. Call handler to report
* success or failure. handler may or may not be called during async_write,
* but it must be safe for this to happen.
*
* Will return 0 on success.
*
* @param bufs vector of buffers to write
* @param handler Callback to invoke with operation status.
*/
void async_write(std::vector<buffer> const &, write_handler handler) {
m_alog.write(log::alevel::devel,"debug_con async_write buffer list");
m_write_handler = handler;
}
/// Set Connection Handle
/**
* @param hdl The new handle
*/
void set_handle(connection_hdl) {}
/// Call given handler back within the transport's event system (if present)
/**
* Invoke a callback within the transport's event system if it has one. If
* it doesn't, the handler will be invoked immediately before this function
* returns.
*
* @param handler The callback to invoke
*
* @return Whether or not the transport was able to register the handler for
* callback.
*/
lib::error_code dispatch(dispatch_handler handler) {
handler();
return lib::error_code();
}
/// Perform cleanup on socket shutdown_handler
/**
* @param h The `shutdown_handler` to call back when complete
*/
void async_shutdown(shutdown_handler handler) {
handler(lib::error_code());
}
size_t read_some_impl(char const * buf, size_t len) {
m_alog.write(log::alevel::devel,"debug_con read_some");
if (!m_reading) {
m_elog.write(log::elevel::devel,"write while not reading");
return 0;
}
size_t bytes_to_copy = (std::min)(len,m_len-m_cursor);
std::copy(buf,buf+bytes_to_copy,m_buf+m_cursor);
m_cursor += bytes_to_copy;
if (m_cursor >= m_bytes_needed) {
complete_read(lib::error_code());
}
return bytes_to_copy;
}
/// Signal that a requested read is complete
/**
* Sets the reading flag to false and returns the handler that should be
* called back with the result of the read. The cursor position that is sent
* is whatever the value of m_cursor is.
*
* It MUST NOT be called when m_reading is false.
* it MUST be called while holding the read lock
*
* It is important to use this method rather than directly setting/calling
* m_read_handler back because this function makes sure to delete the
* locally stored handler which contains shared pointers that will otherwise
* cause circular reference based memory leaks.
*
* @param ec The error code to forward to the read handler
*/
void complete_read(lib::error_code const & ec) {
m_reading = false;
read_handler handler = m_read_handler;
m_read_handler = read_handler();
handler(ec,m_cursor);
}
private:
timer_handler m_timer_handler;
// Read space (Protected by m_read_mutex)
char * m_buf;
size_t m_len;
size_t m_bytes_needed;
read_handler m_read_handler;
size_t m_cursor;
// transport resources
connection_hdl m_connection_hdl;
write_handler m_write_handler;
shutdown_handler m_shutdown_handler;
bool m_reading;
bool const m_is_server;
bool m_is_secure;
alog_type & m_alog;
elog_type & m_elog;
std::string m_remote_endpoint;
};
} // namespace debug
} // namespace transport
} // namespace websocketpp
#endif // WEBSOCKETPP_TRANSPORT_DEBUG_CON_HPP

View File

@ -0,0 +1,140 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef WEBSOCKETPP_TRANSPORT_DEBUG_HPP
#define WEBSOCKETPP_TRANSPORT_DEBUG_HPP
#include <websocketpp/common/memory.hpp>
#include <websocketpp/logger/levels.hpp>
#include <websocketpp/transport/base/endpoint.hpp>
#include <websocketpp/transport/debug/connection.hpp>
namespace websocketpp {
namespace transport {
namespace debug {
template <typename config>
class endpoint {
public:
/// Type of this endpoint transport component
typedef endpoint type;
/// Type of a pointer to this endpoint transport component
typedef lib::shared_ptr<type> ptr;
/// Type of this endpoint's concurrency policy
typedef typename config::concurrency_type concurrency_type;
/// Type of this endpoint's error logging policy
typedef typename config::elog_type elog_type;
/// Type of this endpoint's access logging policy
typedef typename config::alog_type alog_type;
/// Type of this endpoint transport component's associated connection
/// transport component.
typedef debug::connection<config> transport_con_type;
/// Type of a shared pointer to this endpoint transport component's
/// associated connection transport component
typedef typename transport_con_type::ptr transport_con_ptr;
// generate and manage our own io_service
explicit endpoint()
{
//std::cout << "transport::iostream::endpoint constructor" << std::endl;
}
/// Set whether or not endpoint can create secure connections
/**
* TODO: docs
*
* Setting this value only indicates whether or not the endpoint is capable
* of producing and managing secure connections. Connections produced by
* this endpoint must also be individually flagged as secure if they are.
*
* @since 0.3.0-alpha4
*
* @param value Whether or not the endpoint can create secure connections.
*/
void set_secure(bool) {}
/// Tests whether or not the underlying transport is secure
/**
* TODO: docs
*
* @return Whether or not the underlying transport is secure
*/
bool is_secure() const {
return false;
}
protected:
/// Initialize logging
/**
* The loggers are located in the main endpoint class. As such, the
* transport doesn't have direct access to them. This method is called
* by the endpoint constructor to allow shared logging from the transport
* component. These are raw pointers to member variables of the endpoint.
* In particular, they cannot be used in the transport constructor as they
* haven't been constructed yet, and cannot be used in the transport
* destructor as they will have been destroyed by then.
*
* @param a A pointer to the access logger to use.
* @param e A pointer to the error logger to use.
*/
void init_logging(alog_type *, elog_type *) {}
/// Initiate a new connection
/**
* @param tcon A pointer to the transport connection component of the
* connection to connect.
* @param u A URI pointer to the URI to connect to.
* @param cb The function to call back with the results when complete.
*/
void async_connect(transport_con_ptr, uri_ptr, connect_handler cb) {
cb(lib::error_code());
}
/// Initialize a connection
/**
* Init is called by an endpoint once for each newly created connection.
* It's purpose is to give the transport policy the chance to perform any
* transport specific initialization that couldn't be done via the default
* constructor.
*
* @param tcon A pointer to the transport portion of the connection.
* @return A status code indicating the success or failure of the operation
*/
lib::error_code init(transport_con_ptr) {
return lib::error_code();
}
private:
};
} // namespace debug
} // namespace transport
} // namespace websocketpp
#endif // WEBSOCKETPP_TRANSPORT_DEBUG_HPP

View File

@ -1,7 +1,7 @@
/*
* Copyright 2016 The ThroughNet Project Authors. All rights reserved.
* Copyright 2016 The PeerConnect Project Authors. All rights reserved.
*
* Ryan Lee (ryan.lee at throughnet.com)
* Ryan Lee
*/
#include "control.h"
@ -10,7 +10,7 @@
#include "webrtc/api/test/mockpeerconnectionobservers.h"
namespace tn {
namespace pc {
//
// class PeerControl
@ -32,7 +32,7 @@ PeerControl::PeerControl(const std::string local_id,
}
webrtc::DataChannelInit init;
const std::string data_channel_name = std::string("tn_data_") + remote_id_;
const std::string data_channel_name = std::string("pc_data_") + remote_id_;
if (!CreateDataChannel(data_channel_name, init)) {
LOG(LS_ERROR) << "CreateDataChannel failed";
DeletePeerConnection();
@ -390,4 +390,4 @@ PeerDataChannelObserver::state() const {
}
} // namespace tn
} // namespace pc

View File

@ -1,11 +1,11 @@
/*
* Copyright 2016 The ThroughNet Project Authors. All rights reserved.
* Copyright 2016 The PeerConnect Project Authors. All rights reserved.
*
* Ryan Lee (ryan.lee at throughnet.com)
* Ryan Lee
*/
#ifndef __THROUGHNET_PEER_H__
#define __THROUGHNET_PEER_H__
#ifndef __PEERCONNECT_PEER_H__
#define __PEERCONNECT_PEER_H__
#include <condition_variable>
#include <mutex>
@ -16,7 +16,7 @@
#include "webrtc/api/jsep.h"
#include "webrtc/base/json.h"
namespace tn {
namespace pc {
//
// class PeerObserver
@ -171,6 +171,6 @@ private:
std::mutex send_lock_;
};
} // namespace tn
} // namespace pc
#endif // __THROUGHNET_PEER_H__
#endif // __PEERCONNECT_PEER_H__

View File

@ -1,20 +1,20 @@
/*
* Copyright 2016 The ThroughNet Project Authors. All rights reserved.
* Copyright 2016 The PeerConnect Project Authors. All rights reserved.
*
* Ryan Lee (ryan.lee at throughnet.com)
* Ryan Lee
*/
#include "throughnet.h"
#include "peerconnect.h"
#include "control.h"
#include <string>
#include <locale>
Throughnet::Throughnet()
: Throughnet(""){
#include <string>
#include <locale>
PeerConnect::PeerConnect()
: PeerConnect(""){
}
Throughnet::Throughnet(const std::string setting) {
PeerConnect::PeerConnect(const std::string setting) {
// Log level
#if DEBUG || _DEBUG
rtc::LogMessage::LogToDebug(rtc::LS_ERROR);
@ -31,7 +31,7 @@ Throughnet::Throughnet(const std::string setting) {
// create signal client
if (signal_ == nullptr) {
signal_ = std::make_shared<tn::Signal>();
signal_ = std::make_shared<pc::Signal>();
if (signal_) {
signal_->SetConfig(setting_.signal_uri_);
@ -39,19 +39,19 @@ Throughnet::Throughnet(const std::string setting) {
}
}
Throughnet::~Throughnet() {
PeerConnect::~PeerConnect() {
}
void Throughnet::Run() {
rtc::ThreadManager::Instance()->CurrentThread()->Run();
void PeerConnect::Run() {
rtc::ThreadManager::Instance()->CurrentThread()->Run();
}
void Throughnet::Stop() {
void PeerConnect::Stop() {
rtc::ThreadManager::Instance()->CurrentThread()->Quit();
}
void Throughnet::SignIn(const std::string alias, const std::string id, const std::string password) {
void PeerConnect::SignIn(const std::string alias, const std::string id, const std::string password) {
//
// Check if already signed in
@ -66,7 +66,7 @@ void Throughnet::SignIn(const std::string alias, const std::string id, const std
// Initialize control
//
control_ = std::make_shared<tn::Control>(signal_);
control_ = std::make_shared<pc::Control>(signal_);
control_->RegisterObserver(this, control_);
if (control_.get() == NULL) {
@ -105,17 +105,17 @@ void Throughnet::SignIn(const std::string alias, const std::string id, const std
return;
}
void Throughnet::SignOut() {
void PeerConnect::SignOut() {
signout_ = true;
control_->SignOut();
}
void Throughnet::Connect(const std::string id) {
void PeerConnect::Connect(const std::string id) {
control_->Connect(id);
return;
}
void Throughnet::Disconnect(const std::string id) {
void PeerConnect::Disconnect(const std::string id) {
control_->Disconnect(id);
return;
}
@ -125,27 +125,27 @@ void Throughnet::Disconnect(const std::string id) {
// Send message to destination peer session id
//
void Throughnet::Send(const std::string& id, const char* buffer, const size_t size) {
void PeerConnect::Send(const std::string& id, const char* buffer, const size_t size) {
control_->Send(id, buffer, size);
}
void Throughnet::Send(const std::string& id, const char* message) {
void PeerConnect::Send(const std::string& id, const char* message) {
Send(id, message, strlen(message));
}
void Throughnet::Send(const std::string& id, const std::string& message) {
void PeerConnect::Send(const std::string& id, const std::string& message) {
Send(id, message.c_str(), message.size());
}
bool Throughnet::SyncSend(const std::string& id, const char* buffer, const size_t size) {
bool PeerConnect::SyncSend(const std::string& id, const char* buffer, const size_t size) {
return control_->SyncSend(id, buffer, size);
}
bool Throughnet::SyncSend(const std::string& id, const char* message) {
bool PeerConnect::SyncSend(const std::string& id, const char* message) {
return SyncSend(id, message, strlen(message));
}
bool Throughnet::SyncSend(const std::string& id, const std::string& message) {
bool PeerConnect::SyncSend(const std::string& id, const std::string& message) {
return SyncSend(id, message.c_str(), message.size());
}
@ -153,14 +153,14 @@ bool Throughnet::SyncSend(const std::string& id, const std::string& message) {
std::string Throughnet::CreateRandomUuid() {
std::string PeerConnect::CreateRandomUuid() {
return rtc::CreateRandomUuid();
}
//
// Register Event handler
//
Throughnet& Throughnet::On(std::string event_id, std::function<void(Throughnet*, std::string)> handler) {
PeerConnect& PeerConnect::On(std::string event_id, std::function<void(PeerConnect*, std::string)> handler) {
if (event_id.empty()) return *this;
@ -174,7 +174,7 @@ Throughnet& Throughnet::On(std::string event_id, std::function<void(Throughnet*,
// Register Message handler
//
Throughnet& Throughnet::OnMessage(std::function<void(Throughnet*, std::string, Buffer&)> handler) {
PeerConnect& PeerConnect::OnMessage(std::function<void(PeerConnect*, std::string, Buffer&)> handler) {
message_handler_ = handler;
return *this;
}
@ -183,14 +183,14 @@ Throughnet& Throughnet::OnMessage(std::function<void(Throughnet*, std::string, B
// Signal event handler
//
void Throughnet::OnSignedIn(const std::string id) {
void PeerConnect::OnSignedIn(const std::string id) {
signout_ = false;
if (event_handler_.find("signin") == event_handler_.end()) return;
CallEventHandler("signin", this, id);
}
void Throughnet::OnSignedOut(const std::string id) {
void PeerConnect::OnSignedOut(const std::string id) {
if (!signout_) return;
if (event_handler_.find("signout") == event_handler_.end()) return;
@ -200,74 +200,74 @@ void Throughnet::OnSignedOut(const std::string id) {
control_.reset();
}
void Throughnet::OnPeerConnected(const std::string id) {
void PeerConnect::OnPeerConnected(const std::string id) {
if (event_handler_.find("connect") == event_handler_.end()) return;
CallEventHandler("connect", this, id);
}
void Throughnet::OnPeerDisconnected(const std::string id) {
void PeerConnect::OnPeerDisconnected(const std::string id) {
if (event_handler_.find("disconnect") == event_handler_.end()) return;
CallEventHandler("disconnect", this, id);
}
void Throughnet::OnPeerMessage(const std::string id, const char* buffer, const size_t size) {
void PeerConnect::OnPeerMessage(const std::string id, const char* buffer, const size_t size) {
Buffer buf(buffer, size);
message_handler_(this, id, buf);
}
void Throughnet::OnPeerWritable(const std::string id) {
void PeerConnect::OnPeerWritable(const std::string id) {
if (event_handler_.find("writable") == event_handler_.end()) return;
CallEventHandler("writable", this, id);
}
void Throughnet::OnError(const std::string id, const std::string& reason) {
void PeerConnect::OnError(const std::string id, const std::string& reason) {
if (event_handler_.find("error") == event_handler_.end()) return;
error_reason_ = reason;
CallEventHandler("error", this, id);
}
template<typename ...A>
void Throughnet::CallEventHandler(std::string msg_id, A&& ... args)
{
using eventhandler_t = EventHandler_t<A...>;
using cb_t = std::function<void(A...)>;
const Handler_t& base = *event_handler_[msg_id];
const cb_t& func = static_cast<const eventhandler_t&>(base).callback_;
func(std::forward<A>(args)...);
}
template<typename ...A>
void PeerConnect::CallEventHandler(std::string msg_id, A&& ... args)
{
using eventhandler_t = EventHandler_t<A...>;
using cb_t = std::function<void(A...)>;
const Handler_t& base = *event_handler_[msg_id];
const cb_t& func = static_cast<const eventhandler_t&>(base).callback_;
func(std::forward<A>(args)...);
}
bool Throughnet::ParseSetting(const std::string& setting) {
Json::Reader reader;
Json::Value jsetting;
std::string value;
if (!reader.parse(setting, jsetting)) {
LOG(WARNING) << "Invalid setting: " << setting;
return false;
}
if (rtc::GetStringFromJsonObject(jsetting, "url", &value)) {
setting_.signal_uri_ = value;
}
if (rtc::GetStringFromJsonObject(jsetting, "user_id", &value)) {
setting_.signal_id_ = value;
}
if (rtc::GetStringFromJsonObject(jsetting, "user_password", &value)) {
setting_.signal_password_ = value;
}
bool PeerConnect::ParseSetting(const std::string& setting) {
Json::Reader reader;
Json::Value jsetting;
std::string value;
if (!reader.parse(setting, jsetting)) {
LOG(WARNING) << "Invalid setting: " << setting;
return false;
}
if (rtc::GetStringFromJsonObject(jsetting, "url", &value)) {
setting_.signal_uri_ = value;
}
if (rtc::GetStringFromJsonObject(jsetting, "user_id", &value)) {
setting_.signal_id_ = value;
}
if (rtc::GetStringFromJsonObject(jsetting, "user_password", &value)) {
setting_.signal_password_ = value;
}
return true;
}
std::string Throughnet::tolower(const std::string& str) {
std::locale loc;
std::string lower_str;
for (auto elem : str) {
std::string PeerConnect::tolower(const std::string& str) {
std::locale loc;
std::string lower_str;
for (auto elem : str) {
lower_str += std::tolower(elem, loc);
}
return lower_str;

View File

@ -1,11 +1,11 @@
/*
* Copyright 2016 The ThroughNet Project Authors. All rights reserved.
* Copyright 2016 The PeerConnect Project Authors. All rights reserved.
*
* Ryan Lee (ryan.lee at throughnet.com)
* Ryan Lee
*/
#ifndef __THROUGHNET_THROUGHENT_H__
#define __THROUGHNET_THROUGHENT_H__
#ifndef __PEERCONNECT_PEERCONNECT_H__
#define __PEERCONNECT_PEERCONNECT_H__
#include <string>
#include <map>
@ -14,15 +14,15 @@
#include "controlobserver.h"
#define function_tn [&]
#define function_pc [&]
namespace tn {
namespace pc {
class Control;
class Signal;
}
class Throughnet
: public tn::ControlObserver {
class PeerConnect
: public pc::ControlObserver {
public:
struct Setting {
@ -39,16 +39,16 @@ public:
const size_t size_;
};
using Control = tn::Control;
using Signal = tn::Signal;
using Control = pc::Control;
using Signal = pc::Signal;
using Data = std::map<std::string, std::string>;
//
// APIs
//
static void Run();
static void Stop();
static void Run();
static void Stop();
void SignIn(const std::string alias = "", const std::string id = "", const std::string password = "");
void SignOut();
@ -64,41 +64,41 @@ public:
static std::string CreateRandomUuid();
Throughnet& On(std::string event_id, std::function<void(Throughnet*, std::string)>);
Throughnet& OnMessage(std::function<void(Throughnet*, std::string, Buffer&)>);
PeerConnect& On(std::string event_id, std::function<void(PeerConnect*, std::string)>);
PeerConnect& OnMessage(std::function<void(PeerConnect*, std::string, Buffer&)>);
//
// Member functions
//
explicit Throughnet();
explicit Throughnet(std::string setting);
~Throughnet();
explicit PeerConnect();
explicit PeerConnect(std::string setting);
~PeerConnect();
protected:
// The base type that is stored in the collection.
struct Handler_t {
virtual ~Handler_t() = default;
};
// The derived type that represents a callback.
template<typename ...A>
struct EventHandler_t : public Handler_t {
using cb = std::function<void(A...)>;
cb callback_;
EventHandler_t(cb p_callback) : callback_(p_callback) {}
};
template<typename ...A>
void CallEventHandler(std::string msg_id, A&& ... args);
using EventHandler_1 = EventHandler_t<Throughnet*>;
using EventHandler_2 = EventHandler_t<Throughnet*, std::string>;
using EventHandler_3 = EventHandler_t<Throughnet*, std::string, Data&>;
// The base type that is stored in the collection.
struct Handler_t {
virtual ~Handler_t() = default;
};
// The derived type that represents a callback.
template<typename ...A>
struct EventHandler_t : public Handler_t {
using cb = std::function<void(A...)>;
cb callback_;
EventHandler_t(cb p_callback) : callback_(p_callback) {}
};
template<typename ...A>
void CallEventHandler(std::string msg_id, A&& ... args);
using EventHandler_1 = EventHandler_t<PeerConnect*>;
using EventHandler_2 = EventHandler_t<PeerConnect*, std::string>;
using EventHandler_3 = EventHandler_t<PeerConnect*, std::string, Data&>;
using Events = std::map<std::string, std::unique_ptr<Handler_t>>;
using MessageHandler = std::function<void(Throughnet*, std::string, Buffer&)>;
using MessageHandler = std::function<void(PeerConnect*, std::string, Buffer&)>;
//
// ControlObserver implementation
@ -121,11 +121,11 @@ protected:
Events event_handler_;
MessageHandler message_handler_;
std::shared_ptr<tn::Control> control_;
std::shared_ptr<tn::Signal> signal_;
std::shared_ptr<pc::Control> control_;
std::shared_ptr<pc::Signal> signal_;
std::string error_reason_;
};
#endif // __THROUGHNET_THROUGHENT_H__
#endif // __PEERCONNECT_PEERCONNECT_H__

View File

@ -1,7 +1,7 @@
/*
* Copyright 2016 The ThroughNet Project Authors. All rights reserved.
* Copyright 2016 The PeerConnect Project Authors. All rights reserved.
*
* Ryan Lee (ryan.lee at throughnet.com)
* Ryan Lee
*/
/*
@ -38,7 +38,7 @@
#include "signalconnection.h"
#include "webrtc/base/logging.h"
namespace tn {
namespace pc {
Signal::Signal() :
con_state_(con_closed),
@ -53,7 +53,7 @@ Signal::Signal() :
client_.set_access_channels(websocketpp::log::alevel::fail);
#else
client_.clear_access_channels(websocketpp::log::elevel::all);
client_.clear_access_channels(websocketpp::log::alevel::all);
client_.clear_error_channels(websocketpp::log::alevel::fail);
#endif
// Initialize ASIO
@ -203,7 +203,7 @@ void Signal::SyncClose()
void Signal::Teardown()
{
// TODO: Asyncronous close with Throughnet::Stop()
// TODO: Asyncronous close with PeerConnect::Stop()
SyncClose();
}
@ -399,4 +399,4 @@ Signal::context_ptr Signal::OnTlsInit(websocketpp::connection_hdl conn)
}
} // namespace tn
} // namespace pc

View File

@ -1,7 +1,7 @@
/*
* Copyright 2016 The ThroughNet Project Authors. All rights reserved.
* Copyright 2016 The PeerConnect Project Authors. All rights reserved.
*
* Ryan Lee (ryan.lee at throughnet.com)
* Ryan Lee
*/
/*
@ -30,8 +30,8 @@
*/
#ifndef __THROUGHNET_SIGNAL_H__
#define __THROUGHNET_SIGNAL_H__
#ifndef __PEERCONNECT_SIGNAL_H__
#define __PEERCONNECT_SIGNAL_H__
#include <string>
@ -48,7 +48,7 @@
#include "webrtc/base/json.h"
namespace tn {
namespace pc {
class SignalInterface {
public:
@ -160,6 +160,6 @@ private:
}; // class Signal
} // namespace tn
} // namespace pc
#endif // __THROUGHNET_SIGNAL_H__
#endif // __PEERCONNECT_SIGNAL_H__

View File

@ -1,7 +1,7 @@
/*
* Copyright 2016 The ThroughNet Project Authors. All rights reserved.
* Copyright 2016 The PeerConnect Project Authors. All rights reserved.
*
* Ryan Lee (ryan.lee at throughnet.com)
* Ryan Lee
*/
#include <iostream>
@ -9,83 +9,178 @@
#include <thread>
#include <cassert>
#include "throughnet.h"
#include "peerconnect.h"
using namespace std;
void test_normal();
void test_writable();
int main(int argc, char *argv[]) {
std::cout << "Start test" << std::endl;
std::string server = Throughnet::CreateRandomUuid();
std::string client = Throughnet::CreateRandomUuid();
Throughnet tn1;
Throughnet tn2;
tn1.On("signin", function_tn(Throughnet* tn, string id) {
assert(id == server);
std::cout << "tn1: signedin" << std::endl;
tn2.SignIn(client);
});
tn1.On("connect", function_tn(Throughnet* tn, string id) {
assert(id == client);
std::cout << "tn1: tn2(" << id <<") connected" << std::endl;
});
tn1.On("disconnect", function_tn(Throughnet* tn, string id) {
assert(id == client);
std::cout << "tn1: tn2 disconnected" << std::endl;
});
tn1.On("signout", function_tn(Throughnet* tn, string id) {
assert(id == server);
std::cout << "tn1: signed out" << std::endl;
tn2.SignOut();
});
tn1.OnMessage(function_tn(Throughnet* tn, string id, Throughnet::Buffer& data) {
assert(std::string(data.buf_, data.size_) == "Ping");
assert(id == client);
std::cout << "tn1: a message has been received" << std::endl;
tn->Send(client, "Pong");
});
tn2.On("signin", function_tn(Throughnet* tn, string id) {
assert(id == client);
std::cout << "tn2: signedin" << std::endl;
tn->Connect(server);
});
tn2.On("connect", function_tn(Throughnet* tn, string id) {
assert(id == server);
std::cout << "tn2: tn1(" << id << ") connected" << std::endl;
tn->Send(server, "Ping");
});
tn2.On("disconnect", function_tn(Throughnet* tn, string id) {
assert(id == server);
std::cout << "tn2: tn1 disconnected" << std::endl;
tn1.SignOut();
});
tn2.On("signout", function_tn(Throughnet* tn, string id){
assert(id == client);
std::cout << "tn2: signed out" << std::endl;
Throughnet::Stop();
});
tn2.OnMessage(function_tn(Throughnet* tn, string id, Throughnet::Buffer& data) {
assert(std::string(data.buf_, data.size_) == "Pong");
assert(id == server);
std::cout << "tn2 has received message" << std::endl;
tn->Disconnect(server);
});
tn1.SignIn(server);
Throughnet::Run();
// test_normal();
test_writable();
std::cout << "Exit test" << std::endl;
return 0;
}
void test_normal() {
std::string server = PeerConnect::CreateRandomUuid();
std::string client = PeerConnect::CreateRandomUuid();
PeerConnect pc1;
PeerConnect pc2;
pc1.On("signin", function_pc(PeerConnect* pc, string id) {
assert(id == server);
std::cout << "pc1: signedin" << std::endl;
pc2.SignIn(client);
});
pc1.On("connect", function_pc(PeerConnect* pc, string id) {
assert(id == client);
std::cout << "pc1: pc2(" << id << ") connected" << std::endl;
});
pc1.On("disconnect", function_pc(PeerConnect* pc, string id) {
assert(id == client);
std::cout << "pc1: pc2 disconnected" << std::endl;
});
pc1.On("signout", function_pc(PeerConnect* pc, string id) {
assert(id == server);
std::cout << "pc1: signed out" << std::endl;
pc2.SignOut();
});
pc1.OnMessage(function_pc(PeerConnect* pc, string id, PeerConnect::Buffer& data) {
assert(std::string(data.buf_, data.size_) == "Ping");
assert(id == client);
std::cout << "pc1: a message has been received" << std::endl;
pc->Send(client, "Pong");
});
pc2.On("signin", function_pc(PeerConnect* pc, string id) {
assert(id == client);
std::cout << "pc2: signedin" << std::endl;
pc->Connect(server);
});
pc2.On("connect", function_pc(PeerConnect* pc, string id) {
assert(id == server);
std::cout << "pc2: pc1(" << id << ") connected" << std::endl;
pc->Send(server, "Ping");
});
pc2.On("disconnect", function_pc(PeerConnect* pc, string id) {
assert(id == server);
std::cout << "pc2: pc1 disconnected" << std::endl;
pc1.SignOut();
});
pc2.On("signout", function_pc(PeerConnect* pc, string id){
assert(id == client);
std::cout << "pc2: signed out" << std::endl;
PeerConnect::Stop();
});
pc2.OnMessage(function_pc(PeerConnect* pc, string id, PeerConnect::Buffer& data) {
assert(std::string(data.buf_, data.size_) == "Pong");
assert(id == server);
std::cout << "pc2 has received message" << std::endl;
pc->Disconnect(server);
});
pc1.SignIn(server);
PeerConnect::Run();
}
void test_writable() {
std::string server = PeerConnect::CreateRandomUuid();
std::string client = PeerConnect::CreateRandomUuid();
PeerConnect pc1;
PeerConnect pc2;
pc1.On("signin", function_pc(PeerConnect* pc, string id) {
assert(id == server);
std::cout << "pc1: signedin" << std::endl;
pc2.SignIn(client);
});
pc1.On("connect", function_pc(PeerConnect* pc, string id) {
assert(id == client);
std::cout << "pc1: pc2(" << id << ") connected" << std::endl;
});
pc1.On("disconnect", function_pc(PeerConnect* pc, string id) {
assert(id == client);
std::cout << "pc1: pc2 disconnected" << std::endl;
});
pc1.On("writable", function_pc(PeerConnect* pc, string id){
assert(id == server);
std::cout << "pc1: writable" << std::endl;
});
pc1.On("signout", function_pc(PeerConnect* pc, string id) {
assert(id == server);
std::cout << "pc1: signed out" << std::endl;
pc2.SignOut();
});
pc1.OnMessage(function_pc(PeerConnect* pc, string id, PeerConnect::Buffer& data) {
assert(std::string(data.buf_, data.size_) == "Ping");
assert(id == client);
std::cout << "pc1: a message has been received" << std::endl;
pc->Send(client, "Pong");
});
pc2.On("signin", function_pc(PeerConnect* pc, string id) {
assert(id == client);
std::cout << "pc2: signedin" << std::endl;
pc->Connect(server);
});
pc2.On("connect", function_pc(PeerConnect* pc, string id) {
assert(id == server);
std::cout << "pc2: pc1(" << id << ") connected" << std::endl;
});
pc2.On("disconnect", function_pc(PeerConnect* pc, string id) {
assert(id == server);
std::cout << "pc2: pc1 disconnected" << std::endl;
pc1.SignOut();
});
pc2.On("writable", function_pc(PeerConnect* pc, string id){
assert(id == client);
std::cout << "pc2: writable" << std::endl;
pc->Send(server, "Ping");
});
pc2.On("signout", function_pc(PeerConnect* pc, string id){
assert(id == client);
std::cout << "pc2: signed out" << std::endl;
PeerConnect::Stop();
});
pc2.OnMessage(function_pc(PeerConnect* pc, string id, PeerConnect::Buffer& data) {
assert(std::string(data.buf_, data.size_) == "Pong");
assert(id == server);
std::cout << "pc2 has received message" << std::endl;
pc->Disconnect(server);
});
pc1.SignIn(server);
PeerConnect::Run();
}