Commit 29b47103 authored by Hubert Denkmair's avatar Hubert Denkmair
Browse files

add client socket

parent 51a79c5e
#include "RelayServer.h" #include "RelayServer.h"
#include <iostream> #include <iostream>
#include <unistd.h>
#include <TcpServer/TcpSocket.h>
#include <TcpServer/EPoll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
RelayServer::RelayServer() RelayServer::RelayServer()
{ {
...@@ -33,14 +39,41 @@ RelayServer::RelayServer() ...@@ -33,14 +39,41 @@ RelayServer::RelayServer()
int RelayServer::Run() int RelayServer::Run()
{ {
EPoll epoll;
if(!_tcpServer.Listen(9009)) if(!_tcpServer.Listen(9009))
{ {
return -1; return -1;
} }
epoll.AddFileDescriptor(_tcpServer.GetEPoll().GetFileDescriptor(), EPOLLIN|EPOLLPRI|EPOLLERR|EPOLLRDHUP|EPOLLHUP);
_clientSocket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in serv_addr;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(9010);
inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);
if (connect(_clientSocket, (struct sockaddr*)&serv_addr, sizeof(serv_addr))<0)
{
perror("connect to server failed");
return -1;
}
epoll.AddFileDescriptor(_clientSocket, EPOLLIN|EPOLLPRI|EPOLLERR);
while(true) while(true)
{ {
_tcpServer.Poll(1000); epoll.Poll(1000,
[this](const epoll_event& ev)
{
if (ev.data.fd == _clientSocket)
{
OnServerDataReceived(ev);
}
else
{
_tcpServer.Poll(0);
}
return true;
}
);
} }
} }
...@@ -106,3 +139,14 @@ bool RelayServer::OnDataAvailable(TcpSocket &socket) ...@@ -106,3 +139,14 @@ bool RelayServer::OnDataAvailable(TcpSocket &socket)
} }
return true; return true;
} }
bool RelayServer::OnServerDataReceived(const epoll_event &ev)
{
std::array<char, 102400> buf;
if (read(_clientSocket, buf.data(), buf.size()) <= 0)
{
return false;
}
return true;
}
...@@ -12,6 +12,7 @@ class RelayServer ...@@ -12,6 +12,7 @@ class RelayServer
int Run(); int Run();
private: private:
int _clientSocket;
TcpServer _tcpServer; TcpServer _tcpServer;
typedef websocketpp::server<websocketpp::config::core> WebsocketServer; typedef websocketpp::server<websocketpp::config::core> WebsocketServer;
...@@ -21,4 +22,6 @@ class RelayServer ...@@ -21,4 +22,6 @@ class RelayServer
bool OnConnectionEstablished(TcpSocket &socket); bool OnConnectionEstablished(TcpSocket &socket);
bool OnConnectionClosed(TcpSocket &socket); bool OnConnectionClosed(TcpSocket &socket);
bool OnDataAvailable(TcpSocket &socket); bool OnDataAvailable(TcpSocket &socket);
bool OnServerDataReceived(const epoll_event& ev);
}; };
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment