Skip to content

Commit

Permalink
Change example projects
Browse files Browse the repository at this point in the history
  • Loading branch information
enpeng xu committed Jul 12, 2012
1 parent c027235 commit bd49c9f
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 121 deletions.
Binary file modified examples/bin/client.pdb
Binary file not shown.
Binary file modified examples/bin/client_d.exe
Binary file not shown.
Binary file modified examples/bin/client_d.ilk
Binary file not shown.
Binary file modified examples/bin/server.pdb
Binary file not shown.
Binary file removed examples/bin/server_d.exe
Binary file not shown.
Binary file removed examples/bin/server_d.ilk
Binary file not shown.
130 changes: 75 additions & 55 deletions examples/client/client.cpp
Original file line number Diff line number Diff line change
@@ -1,49 +1,53 @@
// client.cpp : Defines the entry point for the console application.
//

#include "sglib/iocp/iocp_client.h"
#include "sglib/iocp/iocp_loop.h"
#include "sglib/thread/sglib_thread.h"

#include <windows.h>
#define sleep //Sleep(10)
#define sleep //Sleep(1)


#define RECV_BUF_LEN 512
#define CLIENT_COUNT 2
#define COPY_COUNT 2

#define RECV_BUF_LEN (1024*64+100)

typedef struct {
int pos;
int count;
int id;
char * buf;
int buf_len;
int recv_count;
char reply[2];
FILE * pf;
char buf[RECV_BUF_LEN];
int recv_count;
}session_data_t;

class client_loop : public sglib::net::iocp_loop<client_loop>

class logic_loop : public sglib::net::iocp_loop<logic_loop>
{
friend class sglib::net::iocp_loop<client_loop>;
friend class sglib::net::iocp_loop<logic_loop>;
protected:

void on_connected() {
session_data_t * sdata = new session_data_t;
memset(sdata, 0, sizeof(session_data_t));
sdata->id = _session->get_id();
sdata->buf_len = RECV_BUF_LEN;
sdata->buf = new char [sdata->buf_len];
sdata->id = connections();

std::cout << "\nsession["<< sdata->id<<"] NET_connected";

char path[128];
sprintf(path, "test\\client_session_%d.txt", sdata->id);
sdata->pf = fopen(path, "w");
sprintf(path, "test\\client_session_%d_%d.jpg", sdata->id, sdata->count);
sdata->pf = fopen(path, "wb");
_session->set_session_data(sdata);
_session->recv(sdata->buf, RECV_BUF_LEN);
sleep;
}
void on_disconnected() {
session_data_t * sdata = (session_data_t *)_session->get_session_data();

std::cout << "\nsession["<< sdata->id <<"] NET_disconnected";
if(sdata->pf) // too many files opened, maybe failed
if(sdata->pf)
fclose(sdata->pf);

delete sdata;
delete _session; //!!!

Expand All @@ -52,34 +56,53 @@ class client_loop : public sglib::net::iocp_loop<client_loop>
sleep;
}
void on_send_finished() {
assert(0);
}
session_data_t * sdata = (session_data_t *)_session->get_session_data();

std::cout << "\nsession["<< sdata->id <<"] NET_send_finished";

sdata->recv_count = 0;
_session->recv(sdata->buf, RECV_BUF_LEN);
sleep;
}
void on_recv_finished() {
session_data_t * sdata = (session_data_t *)_session->get_session_data();

std::cout << "\nsession["<< sdata->id <<"] NET_recv_finished";

if(sdata->pf) // too many files opened, maybe failed
fwrite(sdata->buf, 1, _pack.pack_size, sdata->pf);
if (strcmp(sdata->buf, "quit") == 0)
quit_cur_session(); //_session->quit();
else {//continue receive
_session->recv(sdata->buf, sdata->buf_len);
sleep;

if(_pack.pack_size == 1){
if(sdata->pf)
fclose(sdata->pf);
++sdata->count;
if(sdata->count < COPY_COUNT){
char tmp[128];
sprintf(tmp, "test\\client_session_%d_%d.jpg", sdata->id, sdata->count);
sdata->pf = fopen(tmp, "wb");
sdata->pos = 0;
}
else {
quit_cur_session(); //_session->quit();
return ;
}
}
else{
if(sdata->pf) // too many files opened, maybe failed
fwrite(sdata->buf, 1, _pack.pack_size, sdata->pf);
}

sdata->reply[0] = 'o';
sdata->reply[1] = 'k';
_session->send(sdata->reply, 2);
sleep;
}
void on_recv_failed(){
// buf's size is not enough!
session_data_t * sdata = (session_data_t *)_session->get_session_data();
int pack_size = _pack.pack_size;
void on_recv_failed() {

ASSERT(pack_size > sdata->buf_len);
sdata->buf_len = pack_size * 2;
delete []sdata->buf;
sdata->buf = new char[sdata->buf_len] ;
_session->recv(sdata->buf, sdata->buf_len);
}
void on_noop(){

}
void on_protocal_error(){

}
void on_send_error() {
std::cout << "\nsession["<< _session->get_id()<<"] NET_send_error";
sleep;
Expand All @@ -88,12 +111,6 @@ class client_loop : public sglib::net::iocp_loop<client_loop>
std::cout << "\nsession["<< _session->get_id()<<"] NET_recv_error";
sleep;
}
void on_protocal_error(){
std::cout << "\n ******* session["<< _session->get_id()<<"] NET_protocol_error ******";
sleep;
}
void on_noop(){
}
void on_unhandled() {
std::cout << "\nsession["<< _session->get_id()<<"] **** NET_unhandled ! ****";
}
Expand All @@ -102,42 +119,45 @@ class client_loop : public sglib::net::iocp_loop<client_loop>

int main(int argc, char * argv[])
{
int port = 5001;
int port = 5000;

// create the package queue first.
// server or clients will create io threads to read or write data from the queue.
// the server or clients will create io threads to read or write the queue.
// the loop also read & remove items from the queue.
// server and client are the producer of the queue
// so the server or client is the producer of the queue
// the loop is the consumer of the queue
// so the queue should be created first and be deleted at last!
// so the queue should be deleted at last! created first!
sglib::net::msg_queue::init();

sglib::net::iocp_client client("127.0.0.1", port);
client_loop loop;
loop.init();

if (!client.open()) { // 5000 clients
sglib::net::iocp_client client( "127.0.0.1"
, port
, RECV_BUF_LEN // send buffer size
, RECV_BUF_LEN); // send buffer size
if (!client.open()) {
client.close();
return 0;
}
if(false== client.sessions_open(1)){
if(false== client.sessions_open(CLIENT_COUNT)){
client.close();
return 0;
}

int count = 0;
int quit = 0;
logic_loop loop;
loop.init();

int count = 0;
int quit = 0;
while(true){
if(loop.isquit())
break;

if(!loop.peek()){
continue;
}
if(!loop.tick())
break;
}
sglib::net::msg_queue::get_singleton()->quit(); // release consumer & producer threads.
sglib::net::msg_queue::get_singleton()->quit();
client.close();
sglib::net::msg_queue::get_singleton()->deref();
return 0;
Expand Down
Binary file modified examples/iocp.sdf
Binary file not shown.
Loading

0 comments on commit bd49c9f

Please sign in to comment.