Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eec5c3a

Browse files
committed
添加笔记
1 parent 265c69a commit eec5c3a

File tree

137 files changed

+507349
-30
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+507349
-30
lines changed

PlayServer/Crypto.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "Crypto.h"
2+
#include "openssl/md5.h"
3+
#include<vector>
4+
Buffer Crypto::MD5(const Buffer& text)
5+
{
6+
Buffer result;
7+
std::vector<unsigned char> data;
8+
data.resize(16);
9+
MD5_CTX md5;
10+
MD5_Init(&md5);
11+
MD5_Update(&md5, text, text.size());
12+
MD5_Final(data.data(), &md5);
13+
char temp[3] = "";
14+
for (size_t i = 0; i < data.size(); i++)
15+
{
16+
snprintf(temp, sizeof(temp), "%02x",
17+
data[i] & 0xFF);
18+
result += temp;
19+
}
20+
return result;
21+
}

PlayServer/Crypto.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include"Public.h"
2+
class Crypto
3+
{
4+
public:
5+
static Buffer MD5(const Buffer& text);
6+
};

PlayServer/DataBaseHelper.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class _Table_ {
6262
//TODO:参数进行优化
6363
virtual Buffer Modify(const _Table_& values)
6464
= 0;
65-
virtual Buffer Query() = 0;
65+
virtual Buffer Query(const Buffer& condition = "") = 0;
6666
//创建一个基于表的对象
6767
virtual PTable Copy()const = 0;
6868
virtual void ClearFieldUsed() = 0;
@@ -82,6 +82,7 @@ enum {
8282
SQL_CONDITION = 4//查询条件列
8383
};
8484
enum {
85+
NONE = 0,
8586
NOT_NULL = 1,
8687
DEFAULT = 2,
8788
UNIQUE = 4,
@@ -138,7 +139,15 @@ class _Field_
138139
unsigned Attr;
139140
Buffer Default;
140141
Buffer Check;
142+
public:
141143
public:
142144
//操作条件
143145
unsigned Condition;
146+
union {
147+
bool Bool;
148+
int Integer;
149+
double Double;
150+
Buffer* String;
151+
}Value;
152+
int nType;
144153
};

PlayServer/EdoyunPlayerServer.h

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,38 @@
22
#include"Server.h"
33
#include<map>
44
#include"Loggere.h"
5+
#include"HttpParser.h"
6+
#include "HttpParser.h"
7+
#include "Crypto.h"
8+
#include "MysqlClient.h"
9+
#include "jsoncpp/json.h"
10+
11+
DECLARE_TABLE_CLASS(edoyunLogin_user_mysql, _mysql_table_)
12+
DECLARE_MYSQL_FIELD(TYPE_INT, user_id, NOT_NULL | PRIMARY_KEY | AUTOINCREMENT, "INTEGER", "", "", "")
13+
DECLARE_MYSQL_FIELD(TYPE_VARCHAR, user_qq, NOT_NULL, "VARCHAR", "(15)", "", "") //QQ号
14+
DECLARE_MYSQL_FIELD(TYPE_VARCHAR, user_phone, DEFAULT, "VARCHAR", "(11)", "'18888888888'", "") //手机
15+
DECLARE_MYSQL_FIELD(TYPE_TEXT, user_name, NOT_NULL, "TEXT", "", "", "") //姓名
16+
DECLARE_MYSQL_FIELD(TYPE_TEXT, user_nick, NOT_NULL, "TEXT", "", "", "") //昵称
17+
DECLARE_MYSQL_FIELD(TYPE_TEXT, user_wechat, DEFAULT, "TEXT", "", "NULL", "")
18+
DECLARE_MYSQL_FIELD(TYPE_TEXT, user_wechat_id, DEFAULT, "TEXT", "", "NULL", "")
19+
DECLARE_MYSQL_FIELD(TYPE_TEXT, user_address, DEFAULT, "TEXT", "", "", "")
20+
DECLARE_MYSQL_FIELD(TYPE_TEXT, user_province, DEFAULT, "TEXT", "", "", "")
21+
DECLARE_MYSQL_FIELD(TYPE_TEXT, user_country, DEFAULT, "TEXT", "", "", "")
22+
DECLARE_MYSQL_FIELD(TYPE_INT, user_age, DEFAULT | CHECK, "INTEGER", "", "18", "")
23+
DECLARE_MYSQL_FIELD(TYPE_INT, user_male, DEFAULT, "BOOL", "", "1", "")
24+
DECLARE_MYSQL_FIELD(TYPE_TEXT, user_flags, DEFAULT, "TEXT", "", "0", "")
25+
DECLARE_MYSQL_FIELD(TYPE_REAL, user_experience, DEFAULT, "REAL", "", "0.0", "")
26+
DECLARE_MYSQL_FIELD(TYPE_INT, user_level, DEFAULT | CHECK, "INTEGER", "", "0", "")
27+
DECLARE_MYSQL_FIELD(TYPE_TEXT, user_class_priority, DEFAULT, "TEXT", "", "", "")
28+
DECLARE_MYSQL_FIELD(TYPE_REAL, user_time_per_viewer, DEFAULT, "REAL", "", "", "")
29+
DECLARE_MYSQL_FIELD(TYPE_TEXT, user_career, NONE, "TEXT", "", "", "")
30+
DECLARE_MYSQL_FIELD(TYPE_TEXT, user_password, NOT_NULL, "TEXT", "", "", "")
31+
DECLARE_MYSQL_FIELD(TYPE_INT, user_birthday, NONE, "DATETIME", "", "", "")
32+
DECLARE_MYSQL_FIELD(TYPE_TEXT, user_describe, NONE, "TEXT", "", "", "")
33+
DECLARE_MYSQL_FIELD(TYPE_TEXT, user_education, NONE, "TEXT", "", "", "")
34+
DECLARE_MYSQL_FIELD(TYPE_INT, user_register_time, DEFAULT, "DATETIME", "", "LOCALTIME()", "")
35+
DECLARE_TABLE_CLASS_EDN()
36+
537

638

739
#define ERR_RETURN(ret, err) if(ret!= 0){TRACEE("ret = %d errno = %d message = [%s]", ret, errno, strerror(errno)); return err;}
@@ -17,6 +49,12 @@ class CEdoyunPlayerServer : public CBusiness
1749
}
1850
~CEdoyunPlayerServer()
1951
{
52+
if (m_db) {
53+
CDatabaseClient* db = m_db;
54+
m_db = NULL;
55+
db->Close();
56+
delete db;
57+
}
2058
m_epoll.Close();
2159
m_pool.Close();
2260
for (auto it : m_mapClients)
@@ -121,18 +159,127 @@ class CEdoyunPlayerServer : public CBusiness
121159

122160
int Connected(CSocketBase* pClient)
123161
{
162+
//简单打印客户端的信息
163+
sockaddr_in* paddr = *pClient;
164+
TRACEI("client connnected addr %s port %d", inet_ntoa(paddr->sin_addr), paddr->sin_port);
124165
return 0;
125166
}
126167

127168
int Recived(CSocketBase* pClient, const Buffer& data)
128169
{
170+
TRACEI("接收到数据!");
171+
//TODO:主要业务,在此处理
172+
//HTTP 解析
173+
int ret = 0;
174+
Buffer response = "";
175+
ret = HttpParser(data);
176+
TRACEI("HttpParser ret=%d", ret);
177+
//验证结果的反馈
178+
if (ret != 0) {//验证失败
179+
TRACEE("http parser failed!%d", ret);
180+
}
181+
response = MakeResponse(ret);
182+
ret = pClient->Send(response);
183+
if (ret != 0) {
184+
TRACEE("http response failed!%d [%s]", ret, (char*)response);
185+
}
186+
else {
187+
TRACEI("http response success!%d", ret);
188+
}
129189
return 0;
130190
}
191+
int HttpParser(const Buffer& data)
192+
{
193+
CHttpParser parser;
194+
size_t size = parser.Parser(data);
195+
if (size == 0 || (parser.Errno() != 0)) {
196+
TRACEE("size %llu errno:%u", size, parser.Errno());
197+
return -1;
198+
}
199+
if (parser.Method() == HTTP_GET) {
200+
//get 处理
201+
UrlParser url("https://192.168.1.100" + parser.Url());
202+
int ret = url.Parser();
203+
if (ret != 0) {
204+
TRACEE("ret = %d url[%s]", ret, "https://192.168.1.100" + parser.Url());
205+
return -2;
206+
}
207+
Buffer uri = url.Uri();
208+
TRACEI("**** uri = %s", (char*)uri);
209+
if (uri == "login") {
210+
//处理登录
211+
Buffer time = url["time"];
212+
Buffer salt = url["salt"];
213+
Buffer user = url["user"];
214+
Buffer sign = url["sign"];
215+
TRACEI("time %s salt %s user %s sign %s", (char*)time, (char*)salt, (char*)user, (char*)sign);
216+
//数据库的查询
217+
edoyunLogin_user_mysql dbuser;
218+
Result result;
219+
Buffer sql = dbuser.Query("user_name=\"" + user + "\"");
220+
ret = m_db->Exec(sql, result, dbuser);
221+
if (ret != 0) {
222+
TRACEE("sql=%s ret=%d", (char*)sql, ret);
223+
return -3;
224+
}
225+
if (result.size() == 0) {
226+
TRACEE("no result sql=%s ret=%d", (char*)sql, ret);
227+
return -4;
228+
}
229+
if (result.size() != 1) {
230+
TRACEE("more than one sql=%s ret=%d", (char*)sql, ret);
231+
return -5;
232+
}
233+
auto user1 = result.front();
234+
Buffer pwd = *user1->Fields["user_password"]->Value.String;
235+
TRACEI("password = %s", (char*)pwd);
236+
//登录请求的验证
237+
const char* MD5_KEY = "*&^%$#@b.v+h-b*g/h@n!h#n$d^ssx,.kl<kl";
238+
Buffer md5str = time + MD5_KEY + pwd + salt;
239+
Buffer md5 = Crypto::MD5(md5str);
240+
TRACEI("md5 = %s", (char*)md5);
241+
if (md5 == sign) {
242+
return 0;
243+
}
244+
return -6;
245+
}
246+
}
247+
else if (parser.Method() == HTTP_POST) {
248+
//post 处理
249+
}
250+
return -7;
251+
}
252+
Buffer MakeResponse(int ret) {
253+
Json::Value root;
254+
root["status"] = ret;
255+
if (ret != 0) {
256+
root["message"] = "登录失败,可能是用户名或者密码错误!";
257+
}
258+
else {
259+
root["message"] = "success";
260+
}
261+
Buffer json = root.toStyledString();
262+
Buffer result = "HTTP/1.1 200 OK\r\n";
263+
time_t t;
264+
time(&t);
265+
tm* ptm = localtime(&t);
266+
char temp[64] = "";
267+
strftime(temp, sizeof(temp), "%a, %d %b %G %T GMT\r\n", ptm);
268+
Buffer Date = Buffer("Date: ") + temp;
269+
Buffer Server = "Server: Edoyun/1.0\r\nContent-Type: text/html; charset=utf-8\r\nX-Frame-Options: DENY\r\n";
270+
snprintf(temp, sizeof(temp), "%d", json.size());
271+
Buffer Length = Buffer("Content-Length: ") + temp + "\r\n";
272+
Buffer Stub = "X-Content-Type-Options: nosniff\r\nReferrer-Policy: same-origin\r\n\r\n";
273+
result += Date + Server + Length + Stub + json;
274+
TRACEI("response: %s", (char*)result);
275+
return result;
276+
}
131277
private:
132278
CEpoll m_epoll;
133279
CThreadPool m_pool;
134280
std::map<int, CSocketBase*> m_mapClients;
135281
unsigned m_count;
282+
CDatabaseClient* m_db;
136283

137284

138285
};

PlayServer/HttpParser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class UrlParser
8282
//ĬÈÏ·µ»Ø80
8383
int Port()const { return m_port; }
8484
void SetUrl(const Buffer& url);
85+
const Buffer Uri()const { return m_uri; }
8586
private:
8687
Buffer m_url;
8788
Buffer m_protocol;

PlayServer/MysqlClient.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "MysqlClient.h"
22
#include <sstream>
3+
#include "Loggere.h"
34
int CMysqlClient::Connect(const KeyValue& args)
45
{
56
if (m_bInit)return -1;
@@ -11,7 +12,8 @@ int CMysqlClient::Connect(const KeyValue& args)
1112
atoi(args.at("port")),
1213
NULL, 0);
1314
if ((ret == NULL) && (mysql_errno(&m_db) !=0)) {
14-
printf("%s %s\n", __FUNCTION__, mysql_errno(&m_db));
15+
printf("%s %d %s\n", __FUNCTION__, mysql_errno(&m_db), mysql_error(&m_db));
16+
TRACEE("%d %s", mysql_errno(&m_db), mysql_error(&m_db));
1517
mysql_close(&m_db);
1618
bzero(&m_db, sizeof(m_db));
1719
return -3;
@@ -94,7 +96,7 @@ int CMysqlClient::Close()
9496
bool CMysqlClient::IsConnected()
9597
{
9698
return m_bInit;
97-
}
99+
}
98100
_mysql_table_::_mysql_table_(const _mysql_table_ & table)
99101
{
100102
Database = table.Database;
@@ -207,15 +209,19 @@ Buffer _mysql_table_::Modify(const _Table_& values)
207209
printf("sql = %s\n", (char*)sql);
208210
return sql;
209211
}
210-
Buffer _mysql_table_::Query()
212+
Buffer _mysql_table_::Query(const Buffer& condition)
211213
{
212214
Buffer sql = "SELECT ";
213-
for (size_t i = 0; i < FieldDefine.size();i++)
215+
for (size_t i = 0; i < FieldDefine.size(); i++)
214216
{
215217
if (i > 0)sql += ',';
216-
sql += '`' + FieldDefine[i]->Name + "`";
218+
sql += '`' + FieldDefine[i]->Name + "` ";
217219
}
218-
sql += " FROM " + (Buffer)*this + ";";
220+
sql += " FROM " + (Buffer)*this + " ";
221+
if (condition.size() > 0) {
222+
sql += " WHERE " + condition;
223+
}
224+
sql += ";";
219225
printf("sql = %s\n", (char*)sql);
220226
return sql;
221227
}

PlayServer/MysqlClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class _mysql_table_ :
6767
virtual Buffer Delete(const _Table_&values);
6868
//TODO:参数进行优化
6969
virtual Buffer Modify(const _Table_&values);
70-
virtual Buffer Query();
70+
virtual Buffer Query(const Buffer& condition = "");
7171
//创建一个基于表的对象
7272
virtual PTable Copy()const;
7373
virtual void ClearFieldUsed();

PlayServer/PlayServer.vcxproj

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,13 @@
7777
<ItemGroup>
7878
<ClCompile Include="HttpParser.cpp" />
7979
<ClCompile Include="http_parser.c" />
80+
<ClCompile Include="jsoncpp\json_reader.cpp" />
81+
<ClCompile Include="jsoncpp\json_value.cpp" />
82+
<ClCompile Include="jsoncpp\json_writer.cpp" />
8083
<ClCompile Include="Loggere.cpp" />
8184
<ClCompile Include="main.cpp" />
8285
<ClCompile Include="MysqlClient.cpp" />
86+
<ClCompile Include="Crypto.cpp" />
8387
<ClCompile Include="Server.cpp" />
8488
<ClCompile Include="Sqlite3Client.cpp" />
8589
<ClCompile Include="sqlite3\sqlite3.c" />
@@ -91,8 +95,20 @@
9195
<ClInclude Include="EdoyunPlayerServer.h" />
9296
<ClInclude Include="HttpParser.h" />
9397
<ClInclude Include="http_parser.h" />
98+
<ClInclude Include="jsoncpp\allocator.h" />
99+
<ClInclude Include="jsoncpp\assertions.h" />
100+
<ClInclude Include="jsoncpp\config.h" />
101+
<ClInclude Include="jsoncpp\forwards.h" />
102+
<ClInclude Include="jsoncpp\json.h" />
103+
<ClInclude Include="jsoncpp\json_features.h" />
104+
<ClInclude Include="jsoncpp\json_tool.h" />
105+
<ClInclude Include="jsoncpp\reader.h" />
106+
<ClInclude Include="jsoncpp\value.h" />
107+
<ClInclude Include="jsoncpp\version.h" />
108+
<ClInclude Include="jsoncpp\writer.h" />
94109
<ClInclude Include="Loggere.h" />
95110
<ClInclude Include="MysqlClient.h" />
111+
<ClInclude Include="Crypto.h" />
96112
<ClInclude Include="Public.h" />
97113
<ClInclude Include="Server.h" />
98114
<ClInclude Include="Socket.h" />
@@ -105,9 +121,13 @@
105121
<ClInclude Include="Thread.h" />
106122
<ClInclude Include="ThreadPool.h" />
107123
</ItemGroup>
124+
<ItemGroup>
125+
<None Include="jsoncpp\.WeDrive" />
126+
<None Include="jsoncpp\json_valueiterator.inl" />
127+
</ItemGroup>
108128
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
109129
<Link>
110-
<LibraryDependencies>pthread;m;dl;mysqlclient;</LibraryDependencies>
130+
<LibraryDependencies>pthread;m;dl;mysqlclient;crypto;</LibraryDependencies>
111131
</Link>
112132
</ItemDefinitionGroup>
113133
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

0 commit comments

Comments
 (0)