#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include<semaphore.h>
mg_uint_8 temp_ok[512] = {0};
#define MAX_NETPACK_SIZE 10000
#define MAX_DATA_SIZE 4086
void parse_xxxx_data(mg_uint_8* xxxx_data,int length);
// #include <sys/shm.h>
#define MYPORT 9988
#define BUFFER_SIZE 1024
const mg_uint_8* SERVER_IP = "127.0.0.1";/
int meige_client_socket_fd;
int xxxx_socket_send_data(mg_uint_8* xxxx_data, int data_length);
int xxxx_socket_recv_data(mg_uint_8* xxxx_data);
mg_uint_8 save_data[2048];//什么时候清空呢
void set_first_mark();
int first_mark_flag = 0 ;
void set_second_mark();
static int protocol_test_lenth = 0;
static int protocol_temp_lenth = 0;
void test_str_mark_xxxx(mg_uint_8* xxxx_protocol_data,int ok_length);
int producer = 0; ///是3 的倍数则 通知消费
int cosumer = 0; /// 是 7的倍数 则通知生产
sem_t semProducer;
sem_t semCusmer;
int ring[64];
static mg_uint_8 append_temp_buf[4096] ={0};
static int append_lenth = 0;
int protocol = 0;
void *producer_fun(void *data){
meige_client_socket_fd = socket(AF_INET,SOCK_STREAM, 0);
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(MYPORT); ///服务器端口
servaddr.sin_addr.s_addr = inet_addr(SERVER_IP); ///服务器ip
printf("连接%s:%d\n",SERVER_IP,MYPORT);
if (connect(meige_client_socket_fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
perror("connect");
exit(1);
}
printf("服务器连接成功\n");
const int LEN = 2048;
const int LENEX = 2048; //缓冲区200KB
mg_uint_8 temp[LEN];
mg_uint_8 buf[LENEX];
int _res ; // = recv(_sock, temp, LEN, 0);
int protocol_test = 0 ;
memset(buf,0,LENEX);
int count = 0;
int i ;
while (1) {
_res = 0;
// protocol = 0;
_res = xxxx_socket_recv_data(temp);
if (_res < 0 )
{
printf(" socket error \n ");
break;
}
if (_res > 512 )
{
printf(" socket error \n ");
break;
}
if (!_res) /// -- 1
{
printf(" the connection has been gracefully closed. \n ");
break;
}else {
}
memset(temp,0,LEN);
sleep(1);
}
close(meige_client_socket_fd);
return 0;
}
// szRecNetData 传输过来的数据
// socket 接收到的数据长度
//
mg_uint_8 temp_ok[512]={0};
int TcpDataSplit(mg_uint_8* szRecNetData, int nRecSize){
static mg_uint_8 szLastSaveData[MAX_NETPACK_SIZE];
static mg_uint_8 szLastSaveData_swap[MAX_NETPACK_SIZE];
static int nRemainSize = 0;
static int bFirst = 1;
if (bFirst == 1 )
{
memset(szLastSaveData, 0, sizeof(szLastSaveData));
bFirst = 0;
}
/* 本次接收到的数据拼接到上次数据 */
memcpy( (char*)(szLastSaveData+nRemainSize), szRecNetData, nRecSize );
nRemainSize = nRecSize + nRemainSize;
/* 已经备份数据 */
/* 判断第一个标识 */
for (i=0;i<nRemainSize;i++){
if ( ( szLastSaveData[0] == 0x5e ) ) {
}
}
/* 判断第二个标识 */
//nRemainSize 不断拼接的数据长度
for (i=1;i<nRemainSize;i++){
if ( ( szLastSaveData[i] == 0x5e ) ) {
memcpy(temp_ok,szLastSaveData,i+1);// i 这里是 找到第二个标识的位置 ,可能还剩下的长度为 nRemainSize -(i+1)
//如果满足协议要求,才对余下的数据进行处理,否则不用,直接拼接
/* 余下数据未能组成一个对象,先保存起来 */
// void *memmove( void* dest, const void* src, size_t count );
// szLastSaveData 这里实际已经有数据了,就是截断的问题
//已经拷贝了 i+1 个数据,
//余下 nRemainSize - (i+1);
// 比如 0x5e 0x6e 0x7e 0x8e 0x5e 0x9e 0xae 7 个数据 nRemainSize
// 已经拷贝 0x5e 0x6e 0x7e 0x8e 0x5e 5 个数据 i 为位置 4 5 为数据个数
// 余下 0x9e 0xae 2 个数据 7-5 = 2;
//szLastSaveData_swap
memcpy(szLastSaveData_swap,szLastSaveData,nRemainSize); //总体备份
memset(szLastSaveData,0,MAX_NETPACK_SIZE);
memcpy(szLastSaveData,szLastSaveData_swap+i+1,nRemainSize-i);//个数
//memmove(szLastSaveData, (char*)pDataHead, nRemainSize);
///memset( (mg_uint_8*)( szLastSaveData+nRemainSize), 0, sizeof(szLastSaveData)-nRemainSize );
}
}
return 0;
}
void *cosumer_fun(void *data){
int step=0;
while(1){
sem_wait(&semCusmer);
int data=ring[step];
step++;
step%=64;
printf("consume done: %d\n",data);
sem_post(&semProducer);
}
}
int main()
{
pthread_t producer_fun_id;
pthread_t cosumer_fun_id;
sem_init(&semProducer,0,0);
sem_init(&semCusmer,0,0);
int ret;
ret = pthread_create(&producer_fun_id,NULL,producer_fun,NULL);
if (ret) {
}
ret = pthread_create(&cosumer_fun_id,NULL,cosumer_fun,NULL);
if (ret) {
}
pthread_join(producer_fun_id,NULL);
pthread_join(cosumer_fun_id,NULL);
return 0;
}
int parse_xxxx_data_test(mg_uint_8* xxxx_data,int length){
int i ;
int ret ;
ret = 0;
memmove(append_temp_buf+append_lenth,xxxx_data,length);
append_lenth = append_lenth +length;
for (i=0;i<append_lenth;i++){
if ( ( append_temp_buf[0] == 0x5e ) ) {
}
}
for (i=1;i<append_lenth;i++){
if ( ( append_temp_buf[i] == 0x5e ) ) {
memcpy(temp_ok,append_temp_buf,i+1);
memset(append_temp_buf,0,4096);//by carl test
set_second_mark();
append_lenth = 0;
ret = 1;
}
}
return 1;
}
void set_first_mark(){
first_mark_flag = 1;
}
void set_second_mark(){
first_mark_flag = 0;
}
int xxxx_socket_send_data(mg_uint_8* xxxx_data, int data_length){
int nwrite;
mg_uint_8 infoReq[2048];
mg_uint_8 cntBuf[16];
nwrite = send(meige_client_socket_fd, xxxx_data, data_length, 0);
if ( nwrite > 0 )
{
// printf("xxxx_socket_send_data OK %d \n",nwrite);
}
else
{
// printf("xxxx_socket_send_data faield %d \n",nwrite);
}
return nwrite;
}
int xxxx_socket_recv_data(mg_uint_8* xxxx_data){
int nwrite;
mg_uint_8 infoReq[2048];
mg_uint_8 cntBuf[16];
nwrite = recv(meige_client_socket_fd, xxxx_data, sizeof(xxxx_data), 0);
if ( nwrite > 0 )
{
// printf("xxxx_socket_send_data OK %d \n",nwrite);
}
else
{
// printf("xxxx_socket_send_data faield %d \n",nwrite);
}
return nwrite;
}