Skip to content

Commit

Permalink
init NConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyiwu committed Dec 14, 2014
1 parent fab5e7f commit e6c49d5
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 23 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ INCLUDE_DIRECTORIES(
)

LINK_DIRECTORIES(
${PROJECT_SOURCE_DIR}/deps/libevent
${PROJECT_SOURCE_DIR}/deps/libevent/.libs/
)

ADD_SUBDIRECTORY(example)
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ It is just a simple server, not forcussing on performance, but it is **very easy
### start server

```
./husky.demo --port 11257
./threadpoolserver --port 11257
```

or

```
./nonblockingserver --port 11257
```

### GET Request Example
Expand Down
4 changes: 2 additions & 2 deletions example/nonblockingserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ int main(int argc, char** argv)
printf("usage: %s --port 11257 \n", argv[0]);
return EXIT_FAILURE;
}
size_t threadNumber = 4;
size_t queueMaxSize = 256;
//size_t threadNumber = 4;
//size_t queueMaxSize = 256;
int port = atoi(argv[2]);
ReqHandler reqHandler;
NonblockingServer server(port, reqHandler);
Expand Down
146 changes: 127 additions & 19 deletions include/NonblockingServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,159 @@

//deps
#include <event2/event.h>
#include <event2/event_struct.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>

#include "NetUtils.hpp"
#include "IRequestHandler.hpp"
#include "Limonp/Logger.hpp"

namespace Husky
{
class Connection
{
public:
Connection()
{
}
~Connection()
{
}
public:
SocketFd socket_;
using namespace Limonp;

char* readBuffer_;
size_t readBufferSize_;

char* writeBuffer_;
size_t writeBufferSize_;
enum ConnState
{
CONN_INIT,
CONN_READ_REQUEST,
CONN_WAIT_TASK,
CONN_SEND_RESULT,
CONN_CLOSE
};

class NonblockingServer
{
public:
class NConnection
{
public:
NConnection(SocketFd sock, NonblockingServer* server)
: sock_(sock), server_(server)
{
connState_ = CONN_CLOSE;
}
~NConnection()
{
}

void transition()
{
LogDebug("transition");
switch(connState_)
{
case CONN_READ_REQUEST:
LogDebug("CONN_READ_REQUEST");
return;
case CONN_CLOSE:
LogDebug("CONN_CLOSE");
close(sock_);
server_->returnConnection(this);
return;
default:
LogFatal("Unexpected.");
assert(false);
}
}

private:
SocketFd sock_;
NonblockingServer* server_;

char* readBuffer_;
size_t readBufferSize_;

char* writeBuffer_;
size_t writeBufferSize_;

ConnState connState_;
};

NonblockingServer(int port, const IRequestHandler& handler)
: listener_(-1), reqHandler_(handler)
:
listener_(-1),
reqHandler_(handler),
evbase_(NULL)
{
listener_ = CreateAndListenSocket(port);
evutil_make_socket_nonblocking(listener_);
int ret = evutil_make_socket_nonblocking(listener_);
LIMONP_CHECK(ret != -1);

evbase_ = event_base_new();
LIMONP_CHECK(evbase_);
}
~NonblockingServer()
{
if(listener_ != -1)
{
close(listener_);
}
if(evbase_)
{
event_base_free(evbase_);
}
}
void start()
{
registerEvents();
LogInfo("server start. using libevent %s method %s", event_get_version(), event_base_get_method(evbase_));
event_base_dispatch(evbase_);
}
private:
static void listenHandler(SocketFd fd, short which, void *ctx)
{
((NonblockingServer*)ctx)->handleEvent(fd, which);
}

void handleEvent(SocketFd fd, short which)
{
assert(fd == listener_);

sockaddr_in addr;
socklen_t addrlen;
SocketFd clientSock;

while((clientSock = ::accept(fd, (struct sockaddr*)&addr, &addrlen)) != -1)
{
int ret = evutil_make_socket_nonblocking(clientSock);
LIMONP_CHECK(ret != -1);

NConnection* clientConn = createConnection(clientSock);
//TODO delete
clientConn->transition();
//NConnection
}
}

void registerEvents()
{
struct event* listener_event = event_new(
evbase_,
listener_,
EV_READ | EV_PERSIST,
listenHandler,
this
);
event_add(listener_event, NULL);
}

NConnection* createConnection(SocketFd sock)
{
LogDebug("new NConnection");
NConnection* result;
result = new NConnection(sock, this);
return result;
}
void returnConnection(NConnection* conn)
{
LogDebug("del NConnection");
delete conn;
}

SocketFd listener_;
const IRequestHandler& reqHandler_;

struct event_base* evbase_;
};
}

Expand Down

0 comments on commit e6c49d5

Please sign in to comment.