Added simple server. Added session establishment (only test code). Added sender thread.

This commit is contained in:
lotodore
2007-02-25 23:16:26 +00:00
parent 06ed84cb7b
commit d4855a2ca2
17 changed files with 582 additions and 8 deletions
+85 -2
View File
@@ -20,6 +20,8 @@
#include <net/clientstate.h>
#include <net/clientthread.h>
#include <net/clientdata.h>
#include <net/senderthread.h>
#include <net/netpacket.h>
#include <net/resolverthread.h>
#include <net/clientexception.h>
#include <net/socket_helper.h>
@@ -229,7 +231,7 @@ ClientStateStartConnect::Process(ClientThread &client)
if (IS_VALID_CONNECT(connectResult))
{
client.SetState(ClientStateFinal::Instance());
client.SetState(ClientStateStartSession::Instance());
retVal = MSG_SOCK_CONNECT_DONE;
}
else
@@ -288,7 +290,7 @@ ClientStateConnecting::Process(ClientThread &client)
getsockopt(data.sockfd, SOL_SOCKET, SO_ERROR, (char *)&connectResult, &tmpSize);
if (connectResult != 0)
throw ClientException(ERR_SOCK_CONNECT_FAILED, connectResult);
client.SetState(ClientStateFinal::Instance());
client.SetState(ClientStateStartSession::Instance());
retVal = MSG_SOCK_CONNECT_DONE;
}
else if (selectResult == 0) // timeout
@@ -302,6 +304,87 @@ ClientStateConnecting::Process(ClientThread &client)
//-----------------------------------------------------------------------------
ClientStateStartSession &
ClientStateStartSession::Instance()
{
static ClientStateStartSession state;
return state;
}
ClientStateStartSession::ClientStateStartSession()
{
}
ClientStateStartSession::~ClientStateStartSession()
{
}
int
ClientStateStartSession::Process(ClientThread &client)
{
client.GetSender().Init(client.GetData().sockfd);
client.GetSender().Run();
boost::shared_ptr<NetPacket> packet(new TestNetPacket(10));
client.GetSender().Send(packet);
client.SetState(ClientStateWaitSession::Instance());
return MSG_SOCK_INTERNAL_PENDING;
}
//-----------------------------------------------------------------------------
ClientStateWaitSession &
ClientStateWaitSession::Instance()
{
static ClientStateWaitSession state;
return state;
}
ClientStateWaitSession::ClientStateWaitSession()
{
}
ClientStateWaitSession::~ClientStateWaitSession()
{
}
int
ClientStateWaitSession::Process(ClientThread &client)
{
int retVal;
ClientData &data = client.GetData();
// TODO: use receiver thread.
fd_set readSet;
struct timeval timeout;
FD_ZERO(&readSet);
FD_SET(data.sockfd, &readSet);
timeout.tv_sec = 0;
timeout.tv_usec = CLIENT_WAIT_TIMEOUT_MSEC * 1000;
int selectResult = select(data.sockfd + 1, &readSet, NULL, NULL, &timeout);
if (selectResult > 0) // recv is possible
{
char buf[128];
if (recv(data.sockfd, buf, sizeof(buf), 0) > 0)
{
client.SetState(ClientStateFinal::Instance());
retVal = MSG_SOCK_SESSION_DONE;
}
else
throw ClientException(ERR_SOCK_RECV_FAILED, 0);
}
else
retVal = MSG_SOCK_INTERNAL_PENDING;
return retVal;
}
//-----------------------------------------------------------------------------
ClientStateFinal &
ClientStateFinal::Instance()
{
+12
View File
@@ -20,6 +20,7 @@
#include <net/clientthread.h>
#include <net/clientstate.h>
#include <net/clientdata.h>
#include <net/senderthread.h>
#include <net/clientcallback.h>
#include <net/clientexception.h>
#include <net/socket_msg.h>
@@ -57,6 +58,7 @@ ClientThread::Init(const string &serverAddress, unsigned serverPort, bool ipv6,
void
ClientThread::Main()
{
m_sender.reset(new SenderThread);
SetState(CLIENT_INITIAL_STATE::Instance());
try {
while (!ShouldTerminate())
@@ -69,6 +71,8 @@ ClientThread::Main()
{
m_callback.SignalNetClientError(e.GetErrorId(), e.GetOsErrorCode());
}
GetSender().SignalTermination();
GetSender().Join(100);
}
const ClientData &
@@ -97,3 +101,11 @@ ClientThread::SetState(ClientState &newState)
{
m_curState = &newState;
}
SenderThread &
ClientThread::GetSender()
{
assert(m_sender.get());
return *m_sender;
}
+44
View File
@@ -0,0 +1,44 @@
/***************************************************************************
* Copyright (C) 2007 by Lothar May *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <net/netpacket.h>
NetPacket::~NetPacket()
{
}
//-----------------------------------------------------------------------------
TestNetPacket::TestNetPacket(u_int32_t value)
{
m_data.head.type = 0;
m_data.head.length = sizeof(m_data);
m_data.test = value;
}
TestNetPacket::~TestNetPacket()
{
}
NetPacketHeader *
TestNetPacket::GetData()
{
return (NetPacketHeader *)&m_data;
}
+85
View File
@@ -0,0 +1,85 @@
/***************************************************************************
* Copyright (C) 2007 by Lothar May *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <net/senderthread.h>
#include <net/netpacket.h>
#define SEND_TIMEOUT_MSEC 50
SenderThread::SenderThread()
{
}
SenderThread::~SenderThread()
{
}
void
SenderThread::Init(SOCKET socket)
{
if (!IS_VALID_SOCKET(socket) || IsRunning())
return; // TODO: throw exception
m_socket = socket;
}
void
SenderThread::Send(boost::shared_ptr<NetPacket> packet)
{
boost::mutex::scoped_lock lock(m_outBufMutex);
m_outBuf.push_back(packet);
}
void
SenderThread::Main()
{
boost::shared_ptr<NetPacket> tmpPacket;
while (!ShouldTerminate())
{
if (!tmpPacket.get())
{
boost::mutex::scoped_lock lock(m_outBufMutex);
if (!m_outBuf.empty())
{
tmpPacket = m_outBuf.front();
m_outBuf.pop_front();
}
}
if (tmpPacket.get())
{
fd_set writeSet;
struct timeval timeout;
FD_ZERO(&writeSet);
FD_SET(m_socket, &writeSet);
timeout.tv_sec = 0;
timeout.tv_usec = SEND_TIMEOUT_MSEC * 1000;
int selectResult = select(m_socket + 1, NULL, &writeSet, NULL, &timeout);
if (selectResult > 0) // send is possible
{
send(m_socket, (const char *)tmpPacket->GetData(), tmpPacket->GetData()->length, 0);
tmpPacket.reset();
}
}
else
Msleep(SEND_TIMEOUT_MSEC);
}
}
+73
View File
@@ -0,0 +1,73 @@
/***************************************************************************
* Copyright (C) 2007 by Lothar May *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <net/serverthread.h>
#include <net/socket_helper.h>
ServerThread::ServerThread()
{
}
ServerThread::~ServerThread()
{
}
void
ServerThread::Init()
{
if (IsRunning())
return; // TODO: throw exception
}
void
ServerThread::Main()
{
while (!ShouldTerminate())
{
// Simple hacked server for testing.
SOCKET sockfd;
char buf[1024];
struct sockaddr_storage servaddr, clientaddr;
int sockaddr_size = sizeof(struct sockaddr_in);
int addrFamily = AF_INET;
int addrSize;
sockfd = socket(addrFamily, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.ss_family = addrFamily;
socket_string_to_addr("0.0.0.0", addrFamily, (struct sockaddr *)&servaddr, sockaddr_size);
socket_set_port(7234, addrFamily, (struct sockaddr *)&servaddr, sockaddr_size);
bind(sockfd, (const struct sockaddr *)&servaddr, sockaddr_size);
listen(sockfd, 1);
bzero(&clientaddr, sizeof(clientaddr));
addrSize = sockaddr_size;
SOCKET conn = accept(sockfd, (struct sockaddr *)&clientaddr, &addrSize);
CLOSESOCKET(sockfd);
int ret = recv(conn, buf, sizeof(buf), 0);
send(conn, buf, ret, 0);
CLOSESOCKET(conn);
}
}