2016-08-3116:44:09

server 端

/*******************************************************************
author:xuqing
date:2016-5-20
about the io thread
the init part must be less than 1000,just the real_part_count
THREAD_DEAL_COUNT is the count one thread can deal with
if the real_part_count is 10;
the THREAD_DEAL_COUNT is 5
we just need 2 thread to deal the all io write
success return 0
error return 负数
sucess 有意义 正数
***********************************************************************/
#include "stdafx.h"
#include "rts_server.h"
#include "memory_part.h"
#pragma comment(lib,"ws2_32.lib") #define THREAD_DEAL_COUNT 5
HANDLE h_complete_point;
extern memory_part *temp_memory[]; int socket_init()
{
RTS_DIARY_RET_ERROR_VAR;
WSAData wsaData;
if (WSAStartup(MAKEWORD(, ), &wsaData) != )
{
printf("WSAStartup失败!\n");
RTS_DIARY_ERROR_GOTO;
}
if (LOBYTE(wsaData.wVersion) != || HIBYTE(wsaData.wVersion) != )
{
printf("版本不对!\n");
RTS_DIARY_ERROR_GOTO;
}
return ;
ret_error:
if (i_ret) return i_ret;
return ; } int write_memory(char *record)
{
RTS_DIARY_RET_ERROR_VAR;
char str_cust_id[];
get_custid2data(record, str_cust_id);
int int_cust_id = atoi(str_cust_id);
int part_index = int_cust_id % MAX_PART;
memory_part *save_part = temp_memory[part_index];
if (save_part == NULL) RTS_DIARY_ERROR_GOTO;
SqQueue *save_queue = save_part->queue_offset;
if (save_queue == NULL) RTS_DIARY_ERROR_GOTO; if ((save_queue->rear + ) % MAX_RECORD_OF_PART == save_queue->front)
{
//write memory wait!!!
Sleep();
}
i_ret=save_data2memory(part_index, record);
if (i_ret != ) RTS_DIARY_ERROR_GOTO; return ; ret_error:
if (i_ret) return i_ret;
return ; } DWORD WINAPI ServerWorkerThread(LPVOID CompletionPort)
{
RTS_DIARY_RET_ERROR_VAR;
HANDLE ComPort = (HANDLE)CompletionPort;
DWORD BytesTransferred;
//LPOVERLAPPED Overlapped;
LPPER_HANDLE_DATA PerHandleData;
LPPER_IO_OPERATION_DATA PerIoData;
DWORD SendBytes, RecvBytes;
DWORD Flags;
int recv_int;
char str[] = "";
while (TRUE)
{
//等待完成端口上SOCKET的完成 阻塞
printf( "等待完成端口上SOCKET的完成!\n");
if ((GetQueuedCompletionStatus(ComPort,&BytesTransferred, (PULONG_PTR)&PerHandleData,(LPOVERLAPPED *)&PerIoData, INFINITE)) == NULL)
{
continue;
}
recv_int = BytesTransferred;
printf("BytesTransferred work:%d\n", BytesTransferred);
//检查是否有错误产生
if (BytesTransferred == &&(PerIoData->OperationType == RECV_POSTED ||PerIoData->OperationType == SEND_POSTED))
{
//关闭SOCKET
printf("%d SOCKET关闭\n", PerHandleData->sock);
closesocket(PerHandleData->sock);
free(PerHandleData);
free(PerIoData);
continue;
}
//为请求服务
if (PerIoData->OperationType == RECV_POSTED)
{
printf( "接收处理\n");
memcpy(str, PerIoData->Buff, recv_int);
str[recv_int] = '\0';
printf("%d SOCKET :%s\n", PerHandleData->sock, str);
//although it is multithread but they don't visit the same memory,we have use the queue structure to avoid it
write_memory(str);
ZeroMemory(PerIoData->Buff, );
strcpy(PerIoData->Buff, str);
Flags = ;
ZeroMemory((LPVOID)&(PerIoData->Overlapped), sizeof(OVERLAPPED));
PerIoData->DataBuff[].len = ;
PerIoData->DataBuff[].buf = PerIoData->Buff;
PerIoData->OperationType = SEND_POSTED;
WSASend(PerHandleData->sock, PerIoData->DataBuff,, &SendBytes, , &(PerIoData->Overlapped), NULL);
printf("the send byte is %d\n", SendBytes);
}
else //if(PerIoData->OperationType == SEND_POSTED)
{
printf( "发送处理!\n");
Flags = ;
ZeroMemory((LPVOID)&(PerIoData->Overlapped), sizeof(OVERLAPPED));
ZeroMemory(PerIoData->Buff, );
PerIoData->DataBuff[].len = ;
PerIoData->DataBuff[].buf = PerIoData->Buff;
PerIoData->OperationType = RECV_POSTED;
WSARecv(PerHandleData->sock, PerIoData->DataBuff,, &RecvBytes, &Flags, &(PerIoData->Overlapped), NULL);
//recv_int = RecvBytes;
printf("the recv byte is %d\n", RecvBytes);
}
}
return ;
ret_error:
if (i_ret) return i_ret;
return ; } DWORD WINAPI write_disk(LPVOID p)
{
RTS_DIARY_RET_ERROR_VAR;
Sleep();
//循环处理不同的part 一个线程可以负责处理多个不同的part
memory_part *write_part;
SqQueue *queue;
int i = (int)p;
while (true)
{
write_part = temp_memory[i];
queue = write_part->queue_offset;
if (write_part == NULL || queue == NULL) continue;
printf("rear : %d, front :%d", queue->rear, queue->front);
while (true)
{
if (queue->front != (queue->rear))
{
printf("while front %d\n", queue->front);
printf("hello\n");
save_data2disk(+i);
}
else
{
//if the read thread pointer==write thread pointer
//the read thread must sleep 500
Sleep();
break;
}
}
i = i + ;
i = i % THREAD_DEAL_COUNT + (int)p;
}
return ;
ret_error:
if (i_ret) return i_ret;
return ; } int start_rts_server()
{
RTS_DIARY_RET_ERROR_VAR;
LPPER_HANDLE_DATA perHandleData;
LPPER_IO_OPERATION_DATA ioperdata;
SYSTEM_INFO siSys;
SOCKET sockListen;
struct sockaddr_in addrLocal;
int nRet = ;
DWORD nThreadID;
SOCKET sockAccept;
DWORD dwFlags;
DWORD dwRecvBytes;
int nReuseAddr = ;
int ret_code = ;
int real_part_count = ;
printf("初始环境...!\n");
if (socket_init() != ) RTS_DIARY_ERROR_GOTO;
init_memory_part(real_part_count);
//创建一个IO完成端口
printf("创建一个IO完成端口!\n");
h_complete_point = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, , );
if (h_complete_point == INVALID_HANDLE_VALUE)
{
printf("创建IO完成端口失败!\n");
RTS_DIARY_ERROR_GOTO;
}
//获取CPU数目
GetSystemInfo(&siSys);
for (int i = ; i<(int)siSys.dwNumberOfProcessors * ; i++)//NumberOfProcessors
{
HANDLE hThread;
hThread = CreateThread(NULL, , ServerWorkerThread, (LPVOID)h_complete_point, , &nThreadID);
printf("创建工作者线程 ---->%d!\n", i);
CloseHandle(hThread);
}
//创建监听SOCKET
printf("创建监听SOCKET!\n");
sockListen = WSASocket(AF_INET, SOCK_STREAM, , NULL, , WSA_FLAG_OVERLAPPED);
if (sockListen == SOCKET_ERROR)
{
printf("WSASocket错误!\n");
RTS_DIARY_ERROR_GOTO;
} if (setsockopt(sockListen, SOL_SOCKET, SO_REUSEADDR, (const char *)&nReuseAddr, sizeof(int)) != )
{
printf("setsockopt错误!\n");
RTS_DIARY_ERROR_GOTO;
}
addrLocal.sin_family = AF_INET;
addrLocal.sin_addr.s_addr = htonl(INADDR_ANY);
addrLocal.sin_port = htons();
if (bind(sockListen, (struct sockaddr *)&addrLocal, sizeof(sockaddr_in)) != )
{
printf("bind错误\n");
i_ret = WSAGetLastError();
RTS_DIARY_ERROR_GOTO;
}
//准备监听
printf("准备监听\n");
if (listen(sockListen, ) != )
{
printf("listen错误\n");
RTS_DIARY_ERROR_GOTO;
}
int client_addr = sizeof(SOCKADDR_IN);
int index_part = ;
// the thread to save data into the disk
for (int i = ; i < real_part_count / THREAD_DEAL_COUNT;i++)
{
index_part = THREAD_DEAL_COUNT*i;
HANDLE write_handle = CreateThread(NULL, , write_disk,(LPVOID)index_part, , NULL);
} //main thread to get client accept
while (true)
{
//阻塞
sockAccept = WSAAccept(sockListen, NULL, NULL, NULL, );
perHandleData = (LPPER_HANDLE_DATA)malloc(sizeof(PER_HANDLE_DATA));
if (perHandleData == NULL) continue;
printf("socket number ---> %d 接入!\n", sockAccept);
perHandleData->sock = sockAccept;
if (CreateIoCompletionPort((HANDLE)sockAccept, h_complete_point, (ULONG_PTR)perHandleData, ) == NULL)
{
free(perHandleData);
printf("iocp client!\n");
continue;
}
ioperdata = (LPPER_IO_OPERATION_DATA)malloc(sizeof(PER_IO_OPERATION_DATA));
memset(&(ioperdata->Overlapped), , sizeof(OVERLAPPED));
(ioperdata->DataBuff[]).len = ;
(ioperdata->DataBuff[]).buf = ioperdata->Buff;
ioperdata->OperationType = RECV_POSTED;
if (ioperdata == NULL)
{
free(perHandleData);
continue;
}
printf("投递接收操作!\n");
dwFlags = ;
int b=WSARecv(perHandleData->sock, &ioperdata->DataBuff[], , &dwRecvBytes, &dwFlags, &(ioperdata->Overlapped), NULL);
printf("the recv byte main():%d \n", dwRecvBytes);
if (b == SOCKET_ERROR)
{
ret_code = WSAGetLastError();
if (ret_code= WSA_IO_PENDING)
{
printf("was recv sucess %d\n", WSAGetLastError()); }
else
{
printf("was recv error!\n");
RTS_DIARY_ERROR_GOTO;
}
}
}
ret_error:
if (i_ret) return i_ret;
closesocket(sockListen);
return ;
}

client 端

#include "stdafx.h"
#include "rts_client.h"
#pragma comment(lib,"ws2_32.lib")
SOCKET sockClient;
struct sockaddr_in addrServer;
char buf[1024];
int n = 0;
int socket_init()
{
WSAData wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("WSAStartup失败!\n");
return -1;
} if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
printf("版本不对!\n");
WSACleanup();
return -1;
}
return 0;
} DWORD WINAPI recv_thread(LPVOID par)
{
while (true)
{
if (recv(sockClient, buf, 1024, 0) <= 0)
{
printf("recv失败,可能连接断开!\n");
//break;
goto theend;
}
printf("服务器应答:%s", buf);
memset(buf, 0, 1024); Sleep(200);
} theend:
WSACleanup();
getchar();
return 0;
} int client_start()
{
int i_ret_error = 0;
ff2028_gset_ctx_t gset_st;
ff2028_gset_ctx_t *p_gset_st=&gset_st;
unsigned char tag[4];
i_ret_error = ff2028_gset_ctx_init(p_gset_st);
if (i_ret_error < 0)
{
printf("error-> %s,%s,%d\n", __FILE__, __FUNCTION__, __LINE__);
return i_ret_error - __LINE__;
}
if (socket_init() != 0)
goto theend; sockClient = socket(AF_INET, SOCK_STREAM, 0);
if (sockClient == INVALID_SOCKET)
{
printf("socket 失败!\n");
WSACleanup();
goto theend;
}
memset(&addrServer, 0, sizeof(sockaddr_in));
addrServer.sin_family = AF_INET;
addrServer.sin_addr.s_addr = inet_addr("127.0.0.1");
addrServer.sin_port = htons(9090);
printf("连接服务器...\n");
if (connect(sockClient, (const struct sockaddr *)&addrServer, sizeof(sockaddr)) != 0)
{
printf("connect 失败\n");
WSACleanup();
goto theend;
}
memset(buf, 0, 1024);
sprintf(buf, "1005,5443543546546,,6566,4564645646546,t435435345353453534535 --->%d\r\n", n);
tag[0] = 100;
i_ret_error = ff2028_gset_add_app_data(p_gset_st, tag, 1, (unsigned char *)buf, strlen(buf));
if (i_ret_error < 0)
{
printf("error-> %s,%s,%d\n", __FILE__, __FUNCTION__, __LINE__);
return i_ret_error - __LINE__;
}
ff2028_gset_add_app_data_end(p_gset_st);
//HANDLE receive = CreateThread(NULL, 0, recv_thread, NULL, 0, NULL);
while (true)
{
sprintf(buf, "1005,5443543546546,,6566,4564645646546,t435435345353453534535 --->%d\r\n", n);
if (send(sockClient, buf, strlen(buf), 0) <= 0)
{
printf( "send失败,可能连接断开!\n");
//break;
goto theend;
}
memset(buf, 0, 1024); //接收服务端应答
if (recv(sockClient, buf, 1024, 0) <= 0)
{
printf("recv失败,可能连接断开!\n");
//break;
goto theend;
}
printf("服务器应答:%s", buf);
memset(buf, 0, 1024);
Sleep(200);
n++;
if (n == 20)
break;
}
theend:
WSACleanup();
return 0; }

iocp 小例子的更多相关文章

  1. springmvc入门的第一个小例子

    今天我们探讨一下springmvc,由于是初学,所以简单的了解一下 springmvc的流程,后续会持续更新... 由一个小例子来简单的了解一下 springmvc springmvc是spring框 ...

  2. java即时通信小例子

    学习java一段时间了,今天写来一个即时通信的小例子练手在其过程中也学到了一些知识拿出来和大家分享,请路过的各位大神多多赐教... 好了下面讲一下基本的思路: 首先,编写服务器端的程序,简单点说吧就是 ...

  3. Runtime的几个小例子(含Demo)

    一.什么是runtime(也就是所谓的“运行时”,因为是在运行时实现的.)           1.runtime是一套底层的c语言API(包括很多强大实用的c语言类型,c语言函数);  [runti ...

  4. bootstrap 模态 modal 小例子

    bootstrap 模态 modal  小例子 <html> <head> <meta charset="utf-8" /> <title ...

  5. INI配置文件分析小例子

    随手写个解析INI配置字符串的小例子 带测试 #include <iostream> #include <map> #include <string> #inclu ...

  6. JavaScript小例子:复选框全选

    JavaScript小例子:复选框全选 这只是一个小例子,很简单,但是这个功能还是很常用的: 实现后效果如图: JavaScript代码: <script type="text/jav ...

  7. 【zTree】 zTree使用的 小例子

    使用zTree树不是第一次了  但是 还是翻阅着之前做的 对照着 使用起来比较方便  这里就把小例子列出来   总结一下使用步骤 这样方便下次使用起来方便一点 使用zTree树的步骤: 1.首先  在 ...

  8. js小例子(标签页)

    运用js写的一个小例子,实现点击不同的标签出现不同的内容: <!DOCTYPE html> <html> <head> <meta chaset=" ...

  9. sbrk与brk的使用小例子

    sbrk() 和 brk() - Unix的系统函数   sbrk()和brk() 系统的底层会维护一个位置,通过位置的移动完成内存的分配和回收.映射内存时 以一个内存页作为基本单位.   void* ...

随机推荐

  1. 用CSS制作伪标签云

    performance testing stress testing conformance testing acceptane testing smoke testing regression te ...

  2. mysql 实现 start with

    自己写service----> 传入map(idsql,rssql,prior)   idsql 查询id   rssql 查询结果集    调用 以下方法 @param ids 要查询的起始 ...

  3. 关于去除Eclipse对JavaScript的验证

    关于去除Eclipse对JavaScript的验证 在我们使用大量JavaScript作为一些UI或其他组件来使用时,很多情况下,明明引用的这些JavaScript是可以正常使用的,但Eclipse却 ...

  4. DEV GridControl小结

    1. 如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 2. 如何新增一条记录 (1).gridView.Ad ...

  5. 【BZOJ】3436: 小K的农场

    3436: 小K的农场 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 938  Solved: 417[Submit][Status][Discuss ...

  6. linux应用程序开发-进程通信(IPC)

    IPC why: 1.数据传输 2.资源共享 目的: 3.通知事件 4.进程控制 发展: 1.UNIX进程间通信 2.基于SYStem V 3.POSIX 方式分类: 1.pipe(管道) FIFO( ...

  7. 基本shell命令使用笔记

    linux常用命令: (ls:列出目录内容) -a/-all 列出所有文件,包括隐藏文件 -l 使用长格式显示详细信息,包括rwx (mkdir:建立目录) -p/-parents 若要建立目录的上层 ...

  8. c/c++中两颗璀璨的明珠

    1.指针: 函数指针做函数参数 回调函数 语法现象 意义 实现什么效果 (1).间接赋值成立的三个条件 a.两个变量 b.建立关联 c. *p-> (2).函数指针做函数参数 a.调用的角度去理 ...

  9. MFC编程入门之二十四(常用控件:列表框控件ListBox)

    前面两节讲了比较常用的按钮控件,并通过按钮控件实例说明了具体用法.本文要讲的是列表框控件(ListBox)及其使用实例. 列表框控件简介 列表框给出了一个选项清单,允许用户从中进行单项或多项选择,被选 ...

  10. go并发

    Go语言从语言层面上就支持了并发,这与其他语言大不一样,不像以前我们要用Thread库 来新建线程,还要用线程安全的队列库来共享数据. 以下是我入门的学习笔记. 首先,并行!=并发, 两者是不同的,可 ...