Skip to content

Commit

Permalink
add support for TCP
Browse files Browse the repository at this point in the history
  • Loading branch information
baotiao committed Jan 13, 2015
1 parent 1ce763d commit 7ed4536
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 171 deletions.
31 changes: 30 additions & 1 deletion include/tick_conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,36 @@

class TickConn
{
public:
TickConn(int fd);
}
/*
* Set the fd to nonblock && set the flag_ the the fd flag
*/
bool SetNonblock();

private:

int fd_;
int flag_;
/*
* These functions parse the message from client
*/
Status TickReadHeader(rio_t *rio);
Status TickReadCode(rio_t *rio);
Status TickReadPacket(rio_t *rio);

/*
* The Variable need by read the buf,
* We allocate the memory when we start the server
*/
int header_len_;
int32_t r_opcode_;
char* rbuf_;
int32_t rbuf_len_;

char* wbuf_;
int32_t wbuf_len_;
TickThread *thread;
};

#endif
16 changes: 1 addition & 15 deletions include/tick_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
#include "tick_define.h"
#include "status.h"


class TickThread;
class TickEpoll;

class TickServer
{

public:
TickServer();
~TickServer();
Expand All @@ -35,12 +33,6 @@ class TickServer
private:

Status SetBlockType(BlockType type);
/*
* These functions parse the message from client
*/
Status TickReadHeader(rio_t *rio);
Status TickReadCode(rio_t *rio);
Status TickReadPacket(rio_t *rio);

Status BuildObuf();
/*
Expand All @@ -56,14 +48,9 @@ class TickServer
*/
TickEpoll *tickEpoll_;

int header_len_;
int32_t r_opcode_;
char* rbuf_;
int32_t rbuf_len_;


/*
* Here we used auto poll to find the next work thread,
* Here we used auto poll to find the next work thread,
* last_thread_ is the last work thread
*/
int last_thread_;
Expand All @@ -78,5 +65,4 @@ class TickServer

};


#endif
2 changes: 1 addition & 1 deletion include/tick_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class TickThread
/*
* The TickItem queue is the fd queue, receive from master thread
*/
std::queue<TickItem *> conn_queue_;
std::queue<TickItem> conn_queue_;

/*
* The epoll handler
Expand Down
2 changes: 1 addition & 1 deletion include/tick_util.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef __TICK_UTIL_H__
#define __TICK_UTIL_H__

int setnonblocking(int sockfd);
int Setnonblocking(int sockfd);

#endif
96 changes: 95 additions & 1 deletion src/tick_conn.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,98 @@
#include "tick_conn"
#include "tick_conn.h"
#include "tick_util.h"

TickConn::TickConn(int fd) :
fd_(fd)
{
thread_ = NULL;
}

bool Tick::SetNonblock()
{
flag_ = Setnonblocking(fd_);
if (flag_ == -1) {
return false;
}
return true;
}

Status TickConn::TickReadHeader(rio_t *rio)
{
Status s;
char buf[1024];
int32_t integer = 0;
ssize_t nread;
header_len_ = 0;
while (1) {
nread = rio_readnb(rio, buf, COMMAND_HEADER_LENGTH);
// log_info("nread %d", nread);
if (nread == -1) {
if ((errno == EAGAIN && !(flags_ & O_NONBLOCK)) || (errno == EINTR)) {
continue;
} else {
s = Status::IOError("Read command header error");
return s;
}
} else if (nread == 0){
return Status::Corruption("Connect has interrupt");
} else {
break;
}
}
memcpy((char *)(&integer), buf, sizeof(int32_t));
header_len_ = ntohl(integer);
return Status::OK();
}

Status TickServer::TickReadCode(rio_t *rio)
{
Status s;
char buf[1024];
int32_t integer = 0;
ssize_t nread = 0;
r_opcode_ = 0;
while (1) {
nread = rio_readnb(rio, buf, COMMAND_CODE_LENGTH);
if (nread == -1) {
if ((errno == EAGAIN && !(flags_ & O_NONBLOCK)) || (errno == EINTR)) {
continue;
} else {
s = Status::IOError("Read command code error");
return s;
}
} else if (nread == 0){
return Status::Corruption("Connect has interrupt");
} else {
break;
}
}
memcpy((char *)(&integer), buf, sizeof(int32_t));
r_opcode_ = ntohl(integer);
return Status::OK();
}

Status TickServer::TickReadPacket(rio_t *rio)
{
Status s;
int nread = 0;
if (header_len_ < 4) {
return Status::Corruption("The packet no integrity");
}
while (1) {
nread = rio_readnb(rio, (void *)(rbuf_ + COMMAND_HEADER_LENGTH + COMMAND_CODE_LENGTH), header_len_ - 4);
if (nread == -1) {
if ((errno == EAGAIN && !(flags_ & O_NONBLOCK)) || (errno == EINTR)) {
continue;
} else {
s = Status::IOError("Read data error");
return s;
}
} else if (nread == 0) {
return Status::Corruption("Connect has interrupt");
} else {
break;
}
}
rbuf_len_ = nread;
return Status::OK();
}
7 changes: 2 additions & 5 deletions src/tick_item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@
#include <unistd.h>
#include "csapp.h"

TickItem::TickItem(const char* str, int len) :
len_(len)
TickItem::TickItem(int fd) :
fd_(fd)
{
msg_ = (char *)malloc(sizeof(char) * len_);
memcpy(msg_, str, len_);
}

TickItem::~TickItem()
{
free(msg_);
}
28 changes: 0 additions & 28 deletions src/tick_item.h

This file was deleted.

Loading

0 comments on commit 7ed4536

Please sign in to comment.