本地银行
结构体 account.h
#ifndef _ACCOUNT_H__
#define _ACCOUNT_H__
enum OPER{OPEN=1,LOGIN,GETM,SAVEM,MOVEM,PASS,CHECK,DEST,QUIT};
#define CARD_NO_LEN 16
#define PASS_LEN 6
#define NAME_LEN 48
#define ID_LEN 18
#define TEL_LEN 12
typedef struct Account{
char cardNo[CARD_NO_LEN];
char pass[PASS_LEN];
char name[NAME_LEN];
char id[ID_LEN];
char tel[TEL_LEN];
float balance;
}Account;
typedef struct OpenRet{
int status;
char cardNo[CARD_NO_LEN];
}OpenRet;
typedef struct Login{
char cardNo[CARD_NO_LEN];
char pass[PASS_LEN];
}Login;
typedef struct LoginRet{
int status;
struct Account act;
}LoginRet;
typedef struct GetM{
float money;
}GetM;
typedef struct GetMRet{
int status;
float balance;
}GetMRet;
typedef struct SaveM{
float money;
}SaveM;
typedef struct SaveMRet{
int status;
float balance;
}SaveMRet;
typedef struct MoveM{
char destCardNo[CARD_NO_LEN];
float money;
}MoveM;
typedef struct MoveMRet{
int status;
float balance;
}MoveMRet;
typedef struct Pass{
char pass[PASS_LEN];
}Pass;
typedef struct Ret{
int status;
}Ret;
#endif
business.h
#ifndef _BUSINESS_H__
#define _BUSINESS_H__
#include <stdio.h>
#include "msgque.h"
#include "account.h"
size_t business_entry(struct Msg *msg);
#endif
business.c
#include "business.h"
#include "log.h"
#include "open.h"
#include "login.h"
#include "run.h"
size_t business_entry(struct Msg *msg)
{
LOG_ERROR("msgtype:%d\n",msg->type);
static Account *pact = NULL;
size_t size = 0;
if(pact == NULL)
{
switch(msg->type)
{
case OPEN: msg_open();
case LOGIN: msg_login();
default:
break;
}
}
else
{
switch(msg->type)
{
case GETM: msg_getm(struct Msg *msg);
case SAVEM:msg_savem(struct Msg *msg);
case MOVEM:msg_movem(struct Msg *msg);
case PASS:msg_pass(struct Msg *msg);
case DEST:msg_dest(struct Msg *msg);
case CHECK:msg_check(struct Msg *msg);
case QUIT:
pact = NULL;
default:
}
}
return size;
}
login.h
#ifndef _LOGIN_H__
#define _LOGIN_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <fcntl.h>
#include <signal.h>
#include "account.h"
#endif
login.c
#include "login.h"
#include "useroper.h"
static int msgid1;
static int msgid2;
msgid1 = msg_create(PATH,PRO1);
msgid2 = msg_create(PATH,PRO2);
while(1)
{
struct Msg msg={};
msgrcv(msgid1,&msg,sizeof(Msg),7,0);
struct Account account;
Account *account = (Account*)msg->msg;
char filename[100]={".dat"};
sprintf(filename,"/home/zhizhen/2021/01sc/wl/banksys/%d.dat",account->id);
if(access(filename,F_OK) == -1)
{
account->id = -2;
msgsnd(msgid2,&account,sizeof(Msg),0);
continue;
}
int fd = open(filename,O_RDWR,0644);
if(fd < 0)
{
account->id = -1;
msgsnd(msgid2,&account,sizeof(Msg),0);
}
if(read(fd,&Account,sizeof(Account)) < 0)
{
return -1;
}
if(0 == strcmp(Account.pass,accont->pass) && Account.id == account->id)
{
lseek(fd,0,SEEK_SET);
write(fd,&Account,sizeof(Account));
close(fd);#include "login.h"
#include "useroper.h"
static int msgid1;
static int msgid2;
msgid1 = msg_create(PATH,PRO1);
msgid2 = msg_create(PATH,PRO2);
while(1)
{
struct Msg msg={};
msgrcv(msgid1,&msg,sizeof(Msg),7,0);
struct Account account;
Account *account = (Account*)msg->msg;
char filename[100]={".dat"};
sprintf(filename,"/home/zhizhen/2021/01sc/wl/banksys/%d.dat",account->id);
if(access(filename,F_OK) == -1)
{
account->id = -2;
msgsnd(msgid2,&account,sizeof(Msg),0);
continue;
}
int fd = open(filename,O_RDWR,0644);
if(fd < 0)
{
account->id = -1;
msgsnd(msgid2,&account,sizeof(Msg),0);
}
if(read(fd,&Account,sizeof(Account)) < 0)
{
msgsnd(msgid2,&account,sizeof(Msg),0);
}
else
{
account->id = -1;
write(fd,&use,sizeof(Account));
close(fd);
msgsnd(msgid2,&account,sizeof(Msg),0);
}
}
}
客户端 client.h
#ifndef _CLIENT_H__
#define _CLIENT_H__
void client_run(void);
#endif
client.c
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "client.h"
#include "log.h"
#include "msgque.h"
#define PATH "/"
#define PRO1 100
#define PRO2 101
void client_run(void){
pLogFile = stdout;
int msgidw = msg_create(PATH,PRO1);
int msgidr = msg_create(PATH,PRO2);
if(msgidw==-1||msgidr==-1){
exit(-1);
}
Msg msg = {};
size_t msgsz = 0;
int ret = 0;
while(true){
msgsz = read_msg(&msg);
ret = msg_send(msgidw,&msg,msgsz);
if(ret == -1 || msg.type==0){
break;
}
ret = msg_recv(msgidr,&msg,0);
analyze_msg(&msg);
}
}
main.c
#include "client.h"
int main(){
client_run();
return 0;
}
useroper.h
#ifndef _USER_OPER_H__
#define _USER_OPER_H__
#include "msgque.h"
size_t read_msg(struct Msg *msg);
void analyze_msg(struct Msg *msg);
#endif
useroper.c
#include "useroper.h"
#include "account.h"
size_t read_msg(struct Msg *msg){
static int login = 0;
if(login == 0){
int opt = 0;
scanf("%d",&opt);
if(opt!=OPEN&&opt!=LOGIN)
return -1;
msg->type = opt;
switch(opt){
case OPEN:
return sizeof(Account);
case LOGIN:
return sizeof(Login);
}
}else{
int opt = 0;
scanf("%d",&opt);
msg->type = opt+2;
if(msg->type < GETM || msg->type > QUIT)
return -1;
switch(msg->type){
case GETM:
case SAVEM:
case MOVEM:
case PASS:
case CHECK:
case DEST:
case QUIT:
break;
}
}
}
void analyze_msg(struct Msg *msg){
switch(msg->type){
case OPEN:
case LOGIN:
case GETM:
case SAVEM:
case PASS:
case CHECK:
case DEST:
case QUIT:
break;
}
}
msgque.h
#ifndef _MSG_QUE_H__
#define _MSG_QUE_H__
#include <stdio.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include "log.h"
#define MSG_MAX_LEN 1024
typedef struct Msg{
int type;
char msg[MSG_MAX_LEN];
}Msg;
int msg_create(const char *path,int proId);
int msg_send(int msgid,struct Msg *msg,size_t msgsz);
int msg_recv(int msgid,struct Msg *msg,int msgtype);
int msg_remove(int msgid);
#endif
msgque.c
#include <string.h>
#include <errno.h>
#include "msgque.h"
int msg_create(const char *path,int proId){
key_t key = ftok(path,proId);
if(key == -1){
LOG_FATAL("ftok: %s\n",strerror(errno));
return -1;
}
int msgid = msgget(key,IPC_CREAT|0660);
if(msgid == -1){
LOG_FATAL("msgget: %s\n",strerror(errno));
return -1;
}
return msgid;
}
int msg_send(int msgid,struct Msg *msg,size_t msgsz){
int ret = msgsnd(msgid,(const void*)msg,msgsz,0);
if(ret == -1){
LOG_ERROR("msgsnd: %s\n",strerror(errno));
}
return ret;
}
int msg_recv(int msgid,struct Msg *msg,int msgtype){
int ret = msgrcv(msgid,(void *)msg,MSG_MAX_LEN-1,msgtype,0);
if(ret == -1){
LOG_ERROR("msgrcv: %s\n",strerror(errno));
}
return ret;
}
int msg_remove(int msgid){
int ret = msgctl(msgid,IPC_RMID,NULL);
if(ret == -1){
LOG_ERROR("msgctl: %s\n",strerror(errno));
}
return ret;
}
服务器servermain.c
#include "server.h"
int main(){
server_run();
return 0;
}
server.h
#ifndef _SERVER_H__
#define _SERVER_H__
void server_run(void);
#endif
server.c
#include <stdbool.h>
#include <stdlib.h>
#include <errno.h>
#include "server.h"
#include "log.h"
#include "msgque.h"
#include "business.h"
#define PATH "/"
#define PROID1 100
#define PROID2 101
void init(void){
pLogFile = stdout;
}
void server_run(void){
init();
int msgidr = msg_create(PATH,PROID1);
int msgidw = msg_create(PATH,PROID2);
if(msgidr == -1 ||msgidw == -1){
exit(-1);
}
Msg msg = {};
int ret = 0;
size_t msgsz = 0;
while(true){
ret = msg_recv(msgidr,&msg,0);
msgsz = business_entry(&msg);
ret = msg_send(msgidw,&msg,msgsz);
}
}
单链表.h
#ifndef SLINKED_LIST_H__
#define SLINKED_LIST_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct Node{
void *pelem;
struct Node *next;
}SNode;
typedef struct SLinkedList{
struct Node *head;
size_t elemSize;
size_t size;
}*SLinkedList;
#define NODESIZE sizeof(struct Node)
#define LISTSIZE sizeof(struct SLinkedList)
SLinkedList slinked_list_create(size_t elemSize);
bool slinked_list_empty(SLinkedList list);
size_t slinked_list_size(SLinkedList list);
void slinked_list_clear(SLinkedList list);
void slinked_list_destroy(SLinkedList list);
void slinked_list_travel(SLinkedList list,void (*travel)(const void *));
int slinked_list_insert(SLinkedList list,size_t pos,const void *pelem);
int slinked_list_delete(SLinkedList list,size_t pos,void *pelem);
int slinked_list_get(SLinkedList list,size_t pos,void *pelem);
void *slinked_list_index(SLinkedList list,size_t pos);
void *slinked_list_find(SLinkedList list,const void *key,int (*cmp)(const void*,const void*));
int slinked_list_remove(SLinkedList list,const void *key,int (*cmp)(const void*,const void*));
#endif
单链表.c
#include "slinkedlist.h"
SLinkedList slinked_list_create(size_t elemSize){
SLinkedList list = (SLinkedList)malloc(LISTSIZE);
if(list == NULL){
return NULL;
}
list->head = (struct Node*)malloc(NODESIZE);
if(list->head == NULL){
free(list);
return NULL;
}
list->head->next = NULL;
list->elemSize = elemSize;
list->size = 0;
return list;
}
bool slinked_list_empty(SLinkedList list){
return list->size == 0;
}
size_t slinked_list_size(SLinkedList list){
return list->size;
}
void slinked_list_clear(SLinkedList list){
struct Node *node = list->head->next;
struct Node *next = NULL;
while(node != NULL){
next = node->next;
free(node->pelem);
free(node);
node = next;
}
list->head->next = NULL;
list->size = 0;
}
void slinked_list_destroy(SLinkedList list){
slinked_list_clear(list);
free(list->head);
free(list);
}
void slinked_list_travel(SLinkedList list,void (*travel)(const void *)){
struct Node *node = list->head->next;
for(;node!=NULL;node = node->next){
travel(node->pelem);
}
}
static struct Node *slinked_list_get_prev_node(SLinkedList list,size_t pos){
struct Node *node = list->head;
size_t i;
for(i=0;node!=NULL&&i<pos;i++){
node = node->next;
}
return node;
}
int slinked_list_insert(SLinkedList list,size_t pos,const void *pelem){
struct Node *prev = slinked_list_get_prev_node(list,pos);
if(prev == NULL){
return -1;
}
struct Node *node = (struct Node*)malloc(NODESIZE);
if(node == NULL){
return -2;
}
node->pelem = malloc(list->elemSize);
if(node->pelem == NULL){
free(node);
return -3;
}
memcpy(node->pelem,pelem,list->elemSize);
node->next = prev->next;
prev->next = node;
list->size++;
return 0;
}
int slinked_list_delete(SLinkedList list,size_t pos,void *pelem){
struct Node *prev = slinked_list_get_prev_node(list,pos);
if(prev==NULL||prev->next==NULL){
return -1;
}
struct Node *curr = prev->next;
memcpy(pelem,curr->pelem,list->elemSize);
prev->next = curr->next;
--list->size;
free(curr->pelem);
free(curr);
return 0;
}
int slinked_list_get(SLinkedList list,size_t pos,void *pelem){
struct Node *curr = slinked_list_get_prev_node(list,pos+1);
if(curr == NULL){
return -1;
}
memcpy(pelem,curr->pelem,list->elemSize);
return 0;
}
void *slinked_list_index(SLinkedList list,size_t pos){
struct Node *node = slinked_list_get_prev_node(list,pos);
if(node == NULL){
return NULL;
}
return node->pelem;
}
void *slinked_list_find(SLinkedList list,const void *key,int (*cmp)(const void*,const void*)){
struct Node *node = list->head->next;
for(;node!=NULL;node = node->next){
if(cmp(key,node->pelem)==0){
return node->pelem;
}
}
return NULL;
}
int slinked_list_remove(SLinkedList list,const void *key,int (*cmp)(const void*,const void*)){
struct Node *node = list->head->next;
struct Node *prev = list->head;
for(;node!=NULL;prev = node,node = node->next){
if(cmp(key,node->pelem) == 0){
prev->next = node->next;
free(node->pelem);
free(node);
return 0;
}
}
return -1;
}
|