📄 echo.h
字号:
/*
* Openmysee
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef __ECHO_H_INCLUDED
#define __ECHO_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include <syslog.h>
#include <string.h>
#include <time.h>
#include <dirent.h>
#include "md5.h"
#include "ProtocolDefine.h"
#include "StructDefine.h"
#include "ErrorDefine.h"
#include "util.h"
#define AUTH_HEADER 12
#define NORMAL_HEADER 5
#define MAX_REPSP 10 // Max number of reported sp for cp
#define MAX_NEARPEER 10
#define MAX_CHANNEL 1024 /* max number of Channel */
#define MAX_CONN 75 /* connect timeout, after MAX_CONN reconnect */
#define MAX_CLIENT 3072 // max number of client
#define MAX_QUEUE 4194304 /* max BlockData malloced for a channel */
#define MAX_FILEINPUT 1024
#define MIN_IPADDR_LEN 8
#define IPADDR_LEN 16 /*****IP地址最大长度*****/
#define CHNLURL_LEN 128 /*****频道的标志URL*****/
#define DEFAULT_BLOCK 16384 // default size of Media Block
#define TYPE_LIVE 0
#define TYPE_ORDER 1
#define MAX_LINE 1024
#define MAX_MSG_SIZE 17000
#define MAX_PENDING_JOB 3
#define MAX_TRANSFER_IDLE 60
#define FLAG_SERVER 0
#define FLAG_CLIENT 1
#define MAX_DATA 65536 /* max size of message */
#define MAX_BLOCK_SIZE 65000
#define MIN_BLOCK_SIZE 0
#define LIVE_PREFIX "/channel/"
#define ORDER_PREFIX "/data/"
#define PLIST_PREFIX "/playlist/"
#define PROG_PREFIX "/program/"
#define BLOCK_PER_FILE 163840
#define MAX_TYPE 2
#define TYPE_P2PC 0
#define TYPE_P2PS 1
#define TYPE_CS 0
#define TYPE_CP 1
#define MAX_JOB 10
#ifdef __CP_SOURCE
#define IDENTITY "CP"
#endif
#ifdef __SP_SOURCE
#define IDENTITY "SP"
#endif
#ifndef IDENTITY
#define IDENTITY "PP"
#endif
enum TS_INDEX
{
EDUTS = 0,
CNCTS = 1,
UNITS = 2,
TELTS = 3,
// add new TS here!!
};
enum CHTYPE
{
T_ORDER = 0,
T_LIVE = 1,
T_PLIST = 2
};
#define MAX_TS 4
struct Message
{
int len;
char type;
char buffer[MAX_DATA];
};
struct MList
{
struct Channel *m_lists[MAX_FILEINPUT];
int m_startID[MAX_FILEINPUT]; // start block id for each chan
int m_totalchannel; // total number of channels
int m_cursampleid; // cur sample file id
int m_lastmaxid;
};
struct MediaData
{
int start;
unsigned int len;
int dlen;
char *data;
char progname[CHNLURL_LEN];
#ifdef __CP_SOURCE
char channel_name[CHNLURL_LEN];
#endif
};
typedef int (*TimerFunc) (void *, void *);
struct Timertask
{
unsigned int time;
TimerFunc process;
void *entity;
void *data;
struct Timertask *next;
};
struct TSMessage
{
unsigned int len;
unsigned char type;
unsigned int authcode1:24;
unsigned int authcode2;
char buffer[MAX_DATA];
};
struct LiveChannelInfo
{
unsigned int maxID;
struct Session *dataSource; // source of data, a connection to SP or uplevel CP
unsigned int total;
struct SPUpdate s;
unsigned int max_queue;
float bitrate;
// struct BlockData *blocks;
int max_channel;
int cur_channel;
struct MediaData *media;
int isSave;
#ifdef __CP_SOURCE
char *bitflag; // for CP, it is pending status, for SP, it is keysample status
unsigned char *indisk;
unsigned int numofsp;
struct NormalAddress SPLIST[256];
#endif
#ifdef __SP_SOURCE
unsigned int startid;
struct MList *mlist;
FILE *input[MAX_FILEINPUT];
FILE *keyfile;
unsigned int numinput;
unsigned int numblocks;
unsigned int userid;
float limitedBitRate;
unsigned int status;
time_t updated;
#endif
};
struct Channel
{
int type;
int ref;
char fname[CHNLURL_LEN];
char channel_name[CHNLURL_LEN];
char channel_md5[MD5_LEN+1];
int maxblocksize;
int numclient;
int numjob; // number of jobs
unsigned int ctime;
#ifdef __SP_SOURCE
int numofnp[MAX_TS];
#endif
#ifdef __CP_SOURCE
time_t last_nearpeer;
#endif
long long downsize; // recv from SP for CP, file size for SP;
long long upsize; // send to NP
FILE *db;
struct LiveChannelInfo *pcinfo;
struct Edge *PeerHead;
struct Channel *next;
struct Channel *lnext;
};
struct Session
{
int type;
int socket;
#ifdef __CP_SOURCE
unsigned char npcp; //TYPE_CP
unsigned char flag;
unsigned short first;
unsigned int sock_flag; /* saved for restore after connection succed */
float version;
long long totalup;
struct PeerInfoWithAddr addr;
// struct CorePeerInfo *c;
// struct TransferInfo *t;
#endif
unsigned int host;
unsigned int port;
unsigned int time_sec; //time of creation
unsigned int last_transferblock; //time of last send or recv a block
char *buf;
unsigned int start;
unsigned int off;
unsigned int numjob;
struct JobDes *head;
struct Channel *pc;
struct Edge *header;
struct Session *next;
};
struct ServerDesc
{
int flag;
int sock;
int type;
int cur;
int max;
int maxid;
unsigned int port;
// char buf[MAX_DATA];
unsigned int start;
unsigned int off;
struct Session *head;
int (*init) (int listnum);
int (*process) (int listnum);
int (*closure) (int listnum);
};
struct Edge
{
struct Channel *head;
struct Session *me;
struct Edge *cnext;
struct Edge *enext;
};
struct JobDes
{
struct Session *p;
struct Channel *pc;
struct JobDes *prev;
struct JobDes *next;
int blockid;
int start;
int len;
char buffer[MAX_MSG_SIZE];
};
struct cachetype
{
struct cachetype *next;
};
struct DBRec
{
char md5pass[MD5_LEN];
char *cname;
float bitrate;
int isSave;
};
struct logrec
{
unsigned int id;
unsigned int keysample;
};
#ifdef DEBUG
#ifdef HAVE_SYSLOG
#define OPENLOG openlog (IDENTITY, LOG_PID, LOG_USER)
#define PDEBUG(fmt, args...) syslog(LOG_INFO, "(%s,%d)" fmt, __FILE__, __LINE__, ## args)
#define CLOSELOG closelog ()
#else /* HAVE_SYSLOG */
#define PDEBUG(fmt, args...) fprintf(stderr, IDENTITY ": (%s,%d)" fmt, __FILE__, __LINE__, ## args)
#endif /* HAVE_SYSLOG */
#endif
#ifndef OPENLOG
#define OPENLOG do {} while (0)
#endif
#ifndef CLOSELOG
#define CLOSELOG do {} while (0)
#endif
#ifndef PDEBUG
#define PDEBUG(a,...) do {} while (0)
#endif
#define PINFO(fmt, args...) fprintf(stderr, IDENTITY ": (%s,%d)" fmt, __FILE__, __LINE__, ## args)
#define IPADDR(a) (a >> 24), ((a)>>16) & 0xff, ((a)>>8) & 0xff, (a) & 0xff
#ifdef HAVE_MYSQL
#include <mysql/mysql.h>
#define MAX_QUERY 1024
#endif
#define isSet(buf,i) ((buf[i>>3] & (0x80>>(i&7))) != 0)
#define setBit(buf,i) do {buf[i>>3] |= (0x80>>(i&7));}while (0)
#define clrBit(buf,i) do {buf[i>>3] &= ~(0x80>>(i&7));}while (0)
#define FIX_MAGIC 1134224000
inline struct JobDes *newJob ();
void addJob (struct Session *q, struct Channel *pc, struct JobDes *n);
inline struct JobDes *findEnoughBuffer (struct Session *q, struct Channel *pc, int size);
void deleteJob (struct Session *q, struct Channel *pc, int *job, int num);
int processJobs (struct Session *q);
void deleteAll (struct Session *q);
void deleteChannel (struct Session *q, struct Channel *pc);
int newChannel (struct Channel *pc, struct NormalAddress *client, int n, int type);
struct Channel *newLiveChannel (char *name, struct Session *source, char *cmd5, float bitrate, int maxblocksize);
void freeAllChannel ();
inline int hash_str (unsigned char *str, int len);
int saveBlock (struct Channel *c, char *buf, struct Session *p);
int locate_by_id (struct Channel *c, unsigned int id, char *buf, int max);
struct Channel *findChannel (char *name, int len);
int freeChannel (struct Channel **phash, struct Channel **plist, int *count, struct Channel *p);
void freeLiveChannel (struct Channel *pc, void *p);
int free_livechannel (struct Channel *pc);
struct Channel *getChannel (struct Channel **phash, char *name, int len);
struct Edge *newEdge (struct Channel *head, struct Session *me);
int delEdge (struct Edge *e);
int process_P2P_REQUEST_real (struct Session *p, struct Channel *pc, int id);
void freeMedia (struct Channel *pc);
#ifdef __CP_SOURCE
void addMedia (struct Channel *pc, int start, int len, int dlen, char *data, char *progname, char *channel_name);
#endif
#ifdef __SP_SOURCE
void addMedia (struct Channel *pc, int start, int len, int dlen, char *data, char *progname);
#endif
int isHit (struct Channel *pc, int blockid);
void sendHitMedia (struct Session *p, struct Channel *pc, int i, int id, int copymd5);
void sendMedia (struct Session *p, struct Channel *pc);
struct Channel *getProgrambymd5 (char *name, int len);
int locateprog_by_id (struct Channel *pc, unsigned int id, char *buf, int max);
void freeAllProgram ();
struct Channel *newOrder (char *md5);
int locate_order_by_id (struct Channel *c, uint32_t id, char *buf, int max);
struct Channel *findOrder (char *name, int len);
void freeAllOrder ();
void apply_hash (struct Channel **phash, void apply (struct Channel *,void *), void *p);
void apply_list (struct Channel *plist, void apply (struct Channel *, void *), void *p);
int timer_remove (void *entity, void *data);
void sendIdMedia (struct Session *p, struct Channel *pc, int id, int copymd5);
int writeMessage (struct Session *p, struct Channel *pc, char *ptr);
#endif //__ECHO_H_INCLUDED
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -