Skip to content

Commit

Permalink
Update README; Add comments in server_thread.h
Browse files Browse the repository at this point in the history
  • Loading branch information
gaodunqiao committed Jul 5, 2017
1 parent fdde43c commit e1b57f6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 26 deletions.
55 changes: 29 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,53 +18,56 @@ DispatchThread + Multi WorkerThread
![](http://i.imgur.com/XXfibpV.png)

Now pink support these type of thread:

#### DispatchThread:

DispatchThread is used for listen a port, and get an accept connection. And then dispatch this connection to the worker thread. the usage example is:
DispatchThread is used for listen a port, and get an accept connection. And then
dispatch this connection to the worker thread, you can use different protocol,
now we have support google protobuf protocol. And you just need write the
protocol, and then generate the code. The worker threads will deal with the
protocol analysis, so it simplify your job.

```
Thread *t = new DispatchThread<BadaConn>(9211, 1, reinterpret_cast<WorkerThread<BadaConn> **>(badaThread));
Basic usage example:

```
PikaConnFactory conn_factory;
PikaServerHandle server_handle;
ServerThread *t = new NewDispatchThread(9211, /* server port */
4, /* worker's number */
&conn_factory,
1000, /* cron interval */
1000, /* queue limit */
&server_handle);
t->StartThread();
You can see example/bada.cc for more detail example

#### WorkerThread:

WorkerThread is the working thread, working thread use to communicate with
client, you can use different protocol, now we have support google protobuf
protocol. And you just need write the protocol, and then generate the code. The
WorkerThread will deal with the protocol analysis, so it simplify your job.

the usage example is:

```
class BadaThread : public WorkerThread<BadaConn>
```

You can see example/bada_thread.h example/bada_thread.cc for more detail example
You can see example/mydispatch_srv.cc for more detail


#### HolyThread:

HolyThread just like the redis's main thread, it is the thread that both listen a port and do
the job. When should you use HolyThread and When should you use DispatchThread
combine with WorkerThread, if your job is not so busy, so you can use HolyThread
do the all job. if your job is deal lots of client's request, we suggest you use
WorkerThreads
DispathThread with worker threads.

the usage example is:
Basic usage example:

```
class PinkThread : public HolyThread<PinkHolyConn>
Thread *t = new PinkThread(9211);
t->Start();
PikaConnFactory conn_factory;
PikaServerHandle server_handle;
ServerThread *t = new NewHolyThread(9211, /* server port */
&conn_factory,
1000, /* cron interval */
&server_handle);
t->StartThread();
```

You can see example/holy_test.h example/holy_test.cc for more detail example
You can see example/myholy_srv_chandle.cc example/myholy_srv.cc for more detail

Now we will use pink build our project pika, floyd, zeppelin
Now we will use pink build our project [pika](https://github.com/Qihoo360/pika), [floyd](https://github.com/Qihoo360/floyd), [zeppelin](https://github.com/Qihoo360/zeppelin)

In the future, I will add some thread manager in pink.
34 changes: 34 additions & 0 deletions pink/include/server_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,61 @@ struct PinkFiredEvent;
class ConnFactory;
class WorkerThread;

/*
* ServerHandle will be invoked at appropriate occasion
* in server thread's main loop.
*/
class ServerHandle {
public:
ServerHandle() {}
virtual ~ServerHandle() {}

/*
* CronHandle() will be invoked on every cron_interval elapsed.
*/
virtual void CronHandle() const {}

/*
* FdTimeoutHandle(...) will be invoked after connection timeout.
*/
virtual void FdTimeoutHandle(int fd, const std::string& ip_port) const {
UNUSED(fd);
UNUSED(ip_port);
}

/*
* FdClosedHandle(...) will be invoked before connection closed.
*/
virtual void FdClosedHandle(int fd, const std::string& ip_port) const {
UNUSED(fd);
UNUSED(ip_port);
}

/*
* AccessHandle(...) will be invoked after client fd accept()
* but before handled.
*/
virtual bool AccessHandle(std::string& ip) const {
UNUSED(ip);
return true;
}

/*
* CreateWorkerSpecificData(...) will be invoked in StartThread() routine.
* 'data' pointer should be assigned, we will pass the pointer as parameter
* in every connection's factory create function.
*/
virtual int CreateWorkerSpecificData(void** data) const {
UNUSED(data);
return 0;
}

/*
* DeleteWorkerSpecificData(...) is related to CreateWorkerSpecificData(...),
* it will be invoked in StopThread(...) routine,
* resources assigned in CreateWorkerSpecificData(...) should be deleted in
* this handle
*/
virtual int DeleteWorkerSpecificData(void* data) const {
UNUSED(data);
return 0;
Expand Down Expand Up @@ -87,6 +120,7 @@ class ServerThread : public Thread {

/*
* StartThread will return the error code as pthread_create
* Return 0 if success
*/
virtual int StartThread() override;

Expand Down

0 comments on commit e1b57f6

Please sign in to comment.