255 lines
6.3 KiB
C
255 lines
6.3 KiB
C
#include "transactions.h"
|
|
#include <asm-generic/errno.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
|
|
#define PORT 2223
|
|
#define IP_ADDRESS "127.0.0.1"
|
|
|
|
|
|
packet* CreateServerTransaction(server_transaction *act) {
|
|
|
|
int bufferSize = sizeof(int)*3 + act->argsize;
|
|
|
|
unsigned char* buffer = malloc(bufferSize);
|
|
if( buffer <= 0) {
|
|
return NULL;
|
|
}
|
|
|
|
printf("transaction id: %d, action %d, argsize %d\n", act->id, act->action, act->argsize);
|
|
|
|
memcpy(buffer, &act->id, sizeof(int));
|
|
memcpy(&buffer[sizeof(int)*1], &act->action, sizeof(int));
|
|
memcpy(&buffer[sizeof(int)*2], &act->argsize, sizeof(int));
|
|
memcpy(&buffer[sizeof(int)*3], act->serverArg, act->argsize);
|
|
|
|
packet* p = malloc(sizeof(packet));
|
|
if(p <= 0) {
|
|
return NULL;
|
|
}
|
|
p->packetTpye = SERVER_PACKET;
|
|
p->size = bufferSize;
|
|
p->packet = buffer;
|
|
|
|
|
|
return p;
|
|
}
|
|
packet* CreateUserTransaction(user_transaction *act) {
|
|
int bufferSize = sizeof(int)*3 + act->argsize;
|
|
|
|
unsigned char* buffer = malloc(bufferSize);
|
|
if( buffer <= 0) {
|
|
return NULL;
|
|
}
|
|
|
|
//printf("transaction id: %d, action %d, argsize %d\n", act->id, act->action, act->argsize);
|
|
|
|
memcpy(buffer, &act->id, sizeof(int));
|
|
memcpy(&buffer[sizeof(int)*1], &act->action, sizeof(int));
|
|
memcpy(&buffer[sizeof(int)*2], &act->argsize, sizeof(int));
|
|
memcpy(&buffer[sizeof(int)*3], act->userArg, act->argsize);
|
|
|
|
packet* p = malloc(sizeof(packet));
|
|
if(p <= 0) {
|
|
return NULL;
|
|
}
|
|
p->packetTpye = SERVER_PACKET;
|
|
p->size = bufferSize;
|
|
p->packet = buffer;
|
|
|
|
|
|
return p;
|
|
}
|
|
|
|
void FreePacket(packet* p) {
|
|
free(p->packet);
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
int GetTransaction(user_transaction *act) {
|
|
|
|
|
|
int id;
|
|
int action;
|
|
int argsize;
|
|
void* data;
|
|
|
|
|
|
|
|
act->id = id;
|
|
act->action = action;
|
|
act->argsize = argsize;
|
|
act->userArg = data;
|
|
|
|
|
|
return 0;
|
|
}
|
|
|
|
int ProssesTransaction(user_transaction *act) {
|
|
return 0;
|
|
}
|
|
|
|
#define PACKET_SIZE(p) sizeof(int) + sizeof(int) + p->size
|
|
|
|
int SendServerTransaction(server_transaction *tract, int fd) {
|
|
packet* p = CreateServerTransaction(tract);
|
|
|
|
int packetSize = PACKET_SIZE(p);
|
|
unsigned char* buffer = malloc(packetSize);
|
|
memset(buffer, 0, packetSize);
|
|
unsigned char* sb = buffer;
|
|
memcpy(buffer, &p->packetTpye, sizeof(int));
|
|
memcpy(buffer + sizeof(int), &p->size, sizeof(int));
|
|
memcpy(buffer + sizeof(int) + sizeof(int), p->packet, p->size);
|
|
|
|
printf("buffer :");
|
|
for(int i = 0; i < PACKET_SIZE(p); i++) {
|
|
printf("%u", sb[i]);
|
|
}
|
|
|
|
int r = send(fd,sb, PACKET_SIZE(p),0);
|
|
|
|
printf("\n");
|
|
|
|
free(sb);
|
|
FreePacket(p);
|
|
|
|
if(r == -1) {
|
|
printf("NG\n");
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
int SendUserTransaction(user_transaction* tract, int fd) {
|
|
packet* p = CreateUserTransaction(tract);
|
|
|
|
int packetSize = PACKET_SIZE(p);
|
|
unsigned char* buffer = malloc(packetSize);
|
|
memset(buffer, 0, packetSize);
|
|
unsigned char* sb = buffer;
|
|
memcpy(buffer, &p->packetTpye, sizeof(int));
|
|
memcpy(buffer + sizeof(int), &p->size, sizeof(int));
|
|
memcpy(buffer + sizeof(int) + sizeof(int), p->packet, p->size);
|
|
|
|
printf("buffer :");
|
|
for(int i = 0; i < PACKET_SIZE(p); i++) {
|
|
printf("%u", sb[i]);
|
|
}
|
|
|
|
int r = send(fd,sb, PACKET_SIZE(p),0);
|
|
|
|
printf("\n");
|
|
|
|
free(sb);
|
|
FreePacket(p);
|
|
|
|
if(r == -1) {
|
|
printf("NG\n");
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int RecvServerTransaction(server_transaction *tract, int fd) {
|
|
|
|
|
|
packet tp;
|
|
int sizeOfPacket = 1024;
|
|
unsigned char* buffer = malloc(1024);
|
|
int r = 0;
|
|
|
|
printf("reading buffer");
|
|
r = recv(fd, buffer, sizeof(int)*2,0);
|
|
printf("%d\n",r);
|
|
|
|
if(r > 0) {
|
|
printf("buffer :");
|
|
for(int i = 0; i < r; i++) {
|
|
printf("%u", buffer[i]);
|
|
}
|
|
printf("\n");
|
|
|
|
tp.packetTpye = (int)buffer[0];
|
|
tp.size = (int)buffer[sizeof(int)];
|
|
r = recv(fd, buffer, tp.size,MSG_DONTWAIT);
|
|
tp.packet = malloc(tp.size);
|
|
memcpy(tp.packet, buffer, tp.size);
|
|
|
|
memset(tract, 0, sizeof(server_transaction));
|
|
memcpy(&tract->id, tp.packet, sizeof(int));
|
|
memcpy(&tract->action, tp.packet + sizeof(int), sizeof(int));
|
|
memcpy(&tract->argsize, tp.packet + sizeof(int) + sizeof(int), sizeof(int));
|
|
tract->serverArg = malloc(tract->argsize);
|
|
|
|
memcpy(tract->serverArg, &tp.packet[sizeof(int)*3], tract->argsize);
|
|
if(tract->id != 0) {
|
|
printf("id: %d, action %d, argsize %d\n", tract->id, tract->action, tract->argsize);
|
|
printf("Data :%s\n", (char*)tract->serverArg);
|
|
}
|
|
FreePacket(&tp);
|
|
return 0;
|
|
}
|
|
free(buffer);
|
|
return 1;
|
|
}
|
|
|
|
int RecvUserTransaction(user_transaction *tract, int fd) {
|
|
|
|
packet tp;
|
|
int sizeOfPacket = 1024;
|
|
unsigned char* buffer = malloc(1024);
|
|
int r = 0;
|
|
|
|
printf("reading buffer");
|
|
r = recv(fd, buffer, sizeof(int)*2,MSG_DONTWAIT);
|
|
printf("%d\n",r);
|
|
|
|
if(r > 0) {
|
|
printf("buffer :");
|
|
for(int i = 0; i < r; i++) {
|
|
printf("%u", buffer[i]);
|
|
}
|
|
printf("\n");
|
|
|
|
tp.packetTpye = (int)buffer[0];
|
|
tp.size = (int)buffer[sizeof(int)];
|
|
r = recv(fd, buffer, tp.size,MSG_DONTWAIT);
|
|
tp.packet = malloc(tp.size);
|
|
memcpy(tp.packet, buffer, tp.size);
|
|
|
|
memset(tract, 0, sizeof(server_transaction));
|
|
memcpy(&tract->id, tp.packet, sizeof(int));
|
|
memcpy(&tract->action, tp.packet + sizeof(int), sizeof(int));
|
|
memcpy(&tract->argsize, tp.packet + sizeof(int) + sizeof(int), sizeof(int));
|
|
tract->userArg = malloc(tract->argsize);
|
|
|
|
memcpy(tract->userArg, &tp.packet[sizeof(int)*3], tract->argsize);
|
|
if(tract->id != 0) {
|
|
printf("id: %d, action %d, argsize %d\n", tract->id, tract->action, tract->argsize);
|
|
printf("Data :%s\n", (char*)tract->userArg);
|
|
}
|
|
FreePacket(&tp);
|
|
return 0;
|
|
}
|
|
free(buffer);
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
void FreeUserTransaction(user_transaction* ut) {
|
|
free(ut->userArg);
|
|
memset(ut, 0, sizeof(user_transaction));
|
|
}
|
|
void FreeServerTransaction(server_transaction* st) {
|
|
free(st->serverArg);
|
|
memset(st, 0, sizeof(server_transaction));
|
|
}
|