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

Dev #7

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
42 changes: 1 addition & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,3 @@
# sim

C++ network server framework, `nc` and `telnet` friendly.


#demo

#include "sim/sim.h"

class MyHandler : public sim::Handler
{
public:
virtual sim::HandlerState proc(const sim::Request &req, sim::Response *resp){
std::string cmd = req.msg.type();
if(cmd == "ping"){
resp->msg.add("ok");
resp->msg.add("pong");
}else{
resp->msg.add("ok");
resp->msg.add(cmd);
}
return this->resp();
}
};

int main(int argc, char **argv){
const char *ip = "127.0.0.1";
int port = 8800;
sim::Server *serv = sim::Server::listen(ip, port);
if(!serv){
log_fatal("%s", strerror(errno));
exit(0);
}
log_info("server listen on %s:%d", ip, port);

MyHandler handler;
serv->add_handler(&handler);

serv->loop();
return 0;
}


C++ network server framework.
29 changes: 8 additions & 21 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
include ../build_config.mk

CFLAGS +=
OBJS = sim.o message.o decoder.o link.o handler.o server.o \
fde.o log.o app.o config.o
LIB=libsim.a
HEADER_FILES=sim.h message.h decoder.h fde.h link.h handler.h server.h
UTIL_HEADERS=util/thread.h util/strings.h util/log.h util/config.h util/app.h
CFLAGS += -I .
OBJS =
LIBS = util/libutil.a net/libnet.a line/libline.a core/libcore.a

all: ${OBJS}
make clean
$(CXX) ${CFLAGS} test.cpp ${OBJS} ${LIBS}

all: $(OBJS)
mkdir -p $(HEADER_OUTPUT_DIR)/util
ar -cru $(OUTPUT_DIR)/$(LIB) ${OBJS}
cp $(HEADER_FILES) $(HEADER_OUTPUT_DIR)
cp $(UTIL_HEADERS) $(HEADER_OUTPUT_DIR)/util

fde.o: fde.h fde.cpp fde_select.cpp fde_epoll.cpp
${CXX} ${CFLAGS} -c fde.cpp
app.o: util/app.h util/app.cpp
$(CXX) ${CFLAGS} -c util/app.cpp
log.o: util/log.h util/log.cpp
$(CXX) ${CFLAGS} -c util/log.cpp
config.o: util/config.h util/config.cpp
$(CXX) ${CFLAGS} -c util/config.cpp
.cpp.o:
$(CXX) ${CFLAGS} -c $< -o $@

clean:
rm -rf *.o *.a *.out $(OUTPUT_DIR)/$(LIB) $(HEADER_OUTPUT_DIR)
rm -rf *.o *.a *.out

16 changes: 0 additions & 16 deletions src/client/Makefile

This file was deleted.

52 changes: 0 additions & 52 deletions src/client/client.cpp

This file was deleted.

29 changes: 0 additions & 29 deletions src/client/client.h

This file was deleted.

29 changes: 0 additions & 29 deletions src/client/demo.cpp

This file was deleted.

19 changes: 19 additions & 0 deletions src/core/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
include ../../build_config.mk

CFLAGS += -I ../
OBJS = event.o parser.o session.o transport_impl.o server.o

all: ${OBJS}
ar -cru ./libcore.a ${OBJS}

.cpp.o:
$(CXX) ${CFLAGS} -c $< -o $@

test:
make clean
make
$(CXX) ${CFLAGS} test.cpp ${OBJS} util/libutil.a net/libnet.a line/libline.a

clean:
rm -rf *.o *.a *.out

41 changes: 41 additions & 0 deletions src/core/event.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "event.h"
#include "session.h"

Event Event::new_event(Session *sess){
return Event(sess->id(), 0);
}
Event Event::close_event(Session *sess){
return Event(sess->id(), 1);
}
Event Event::read_event(Session *sess){
return Event(sess->id(), 2);
}

Event::Event(){
_type = -1;
}

Event::Event(int id, int type){
_id = id;
_type = type;
}

int Event::id() const{
return _id;
}

bool Event::is_none() const{
return _type == -1;
}

bool Event::is_new() const{
return _type == 0;
}

bool Event::is_close() const{
return _type == 1;
}

bool Event::is_read() const{
return _type == 2;
}
26 changes: 26 additions & 0 deletions src/core/event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef SIM_EVENT_H
#define SIM_EVENT_H

class Session;

class Event
{
public:
static Event new_event(Session *sess);
static Event close_event(Session *sess);
static Event read_event(Session *sess);

Event();
Event(int id, int type);
int id() const;
bool is_none() const;
bool is_new() const;
bool is_close() const;
bool is_read() const; // 收到一个报文

private:
int _id;
int _type;
};

#endif
13 changes: 13 additions & 0 deletions src/core/message.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef SIM_MESSAGE_H
#define SIM_MESSAGE_H

#include "util/buffer.h"

class Message
{
public:
virtual ~Message(){}
virtual int encode(Buffer *buffer) = 0;
};

#endif
37 changes: 37 additions & 0 deletions src/core/parser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "parser.h"

ParseState ParseState::none_state(){
return ParseState(0);
}

ParseState ParseState::ready_state(){
return ParseState(1);
}

ParseState ParseState::error_state(){
return ParseState(-1);
}

ParseState::ParseState(){
_code = 0;
}

ParseState::ParseState(int code){
_code = code;
}

ParseState::~ParseState(){
}

bool ParseState::none() const{
return _code == 0;
}

bool ParseState::ready() const{
return _code == 1;
}

bool ParseState::error() const{
return _code == -1;
}

37 changes: 37 additions & 0 deletions src/core/parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef SIM_PARSER_H
#define SIM_PARSER_H

class Buffer;
class Message;
class ParseState;

class Parser
{
public:
virtual ~Parser(){};
virtual ParseState parse(Buffer *buffer, Message **msg) = 0;
};

/////////////////////////////////////////

class ParseState
{
public:
static ParseState none_state();
static ParseState ready_state();
static ParseState error_state();

ParseState();
~ParseState();

bool none() const;
bool ready() const;
bool error() const;

private:
ParseState(int code);

int _code;
};

#endif
Loading