-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.h
66 lines (51 loc) · 1.49 KB
/
common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef CS_SOCKET_COMMON
#define CS_SOCKET_COMMON
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <pthread.h>
#include <fcntl.h>
#define TRUE 1
#define FALSE 0
#define CHUNK_SIZE 2048
#define WAIT_TIME 1000
#define SERVER -1
#define DEBUG
#ifdef DEBUG
#define debug(args...) printf(args); printf("\n")
#else
#define debug(args...) /*no debug*/
#endif
typedef enum {INT, STRING, BYTE, ACTION} PackageType;
typedef enum {INCOMING_FILE, ACCEPT_FILE, CLIENT_NOT_EXIST} ActionType;
typedef struct {
int from; // from and to shuold only used when sending client->server or client->client
int to;
int size; // size of data, not with headers
PackageType type;
} Header;
typedef struct {
Header header;
char* data;
} Package;
Package* readPackage(int fd);
int sendPackage(int fd, Package* package);
int sendPackageAndFree(int fd, Package* package);
Package* newPackage(int size);
void freePackage(Package* package);
int getInt(Package* package);
char* getStr(Package* package);
ActionType getAction(Package* package);
Package* pack(const char* d, int size, PackageType type, int from, int to);
Package* intPack(int n, int from, int to);
Package* strPack(const char* str, int from, int to);
Package* actionPack(ActionType action, int from, int to);
Package* bytePack(const char* byte, int size, int from, int to);
// file operations
int doesFileExist(const char *filename);
#endif