Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[apps] Fix broken TcpMedium of srt-tunnel in windows #935

Merged
merged 1 commit into from
Nov 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 43 additions & 8 deletions apps/srt-tunnel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,32 @@ class TcpMedium: public Medium

TcpMedium(UriParser u, size_t ch): Medium(u, ch) {}

#endif

#ifdef _WIN32
static int tcp_close(int socket)
{
return ::closesocket(socket);
}

enum { DEF_SEND_FLAG = 0 };

#elif defined(LINUX)
static int tcp_close(int socket)
{
return ::close(socket);
}

enum { DEF_SEND_FLAG = MSG_NOSIGNAL };

#elif defined(BSD) || defined(OSX) || (TARGET_OS_IOS == 1) || (TARGET_OS_TV == 1)
static int tcp_close(int socket)
{
return ::close(socket);
}

enum { DEF_SEND_FLAG = 0 };

#endif

bool IsOpen() override { return m_open; }
Expand All @@ -460,7 +486,7 @@ class TcpMedium: public Medium
lock_guard<mutex> lk(access);
if (m_socket == -1)
return;
::close(m_socket);
tcp_close(m_socket);
m_socket = -1;
}

Expand All @@ -475,12 +501,15 @@ class TcpMedium: public Medium

protected:

// Just models. No options are predicted for now.
void ConfigurePre(int )
void ConfigurePre(int so)
{
#if defined(__APPLE__)
int optval = 1;
setsockopt(so, SOL_SOCKET, SO_NOSIGPIPE, &optval, sizeof(optval));
#endif
}

void ConfigurePost(int)
void ConfigurePost(int /*so*/)
{
}

Expand Down Expand Up @@ -578,14 +607,14 @@ void TcpMedium::CreateListener()

if (stat == -1)
{
close(m_socket);
tcp_close(m_socket);
Error(errno, "bind");
}

stat = listen(m_socket, backlog);
if ( stat == -1 )
{
close(m_socket);
tcp_close(m_socket);
Error(errno, "listen");
}

Expand Down Expand Up @@ -671,7 +700,7 @@ int SrtMedium::ReadInternal(char* buffer, int size)

int TcpMedium::ReadInternal(char* buffer, int size)
{
return read(m_socket, buffer, size);
return ::recv(m_socket, buffer, size, 0);
}

bool SrtMedium::IsErrorAgain()
Expand Down Expand Up @@ -779,7 +808,7 @@ void TcpMedium::Write(ref_t<bytevector> r_buffer)
{
bytevector& buffer = *r_buffer;

int st = ::write(m_socket, buffer.data(), buffer.size());
int st = ::send(m_socket, buffer.data(), buffer.size(), DEF_SEND_FLAG);
if (st == -1)
{
Error(errno, "send");
Expand Down Expand Up @@ -948,6 +977,12 @@ int OnINT_StopService(int)

int main( int argc, char** argv )
{
if (!SysInitializeNetwork())
{
cerr << "Fail to initialize network module.";
return 1;
}

size_t chunk = default_chunk;

set<string>
Expand Down