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

Skip to content

Commit 3bd626c

Browse files
committed
客户端的处理代码都已经结束了
1 parent e094737 commit 3bd626c

File tree

6 files changed

+179
-13
lines changed

6 files changed

+179
-13
lines changed

PlayServer/EdoyunPlayerServer.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class CEdoyunPlayerServer : public CBusiness
1313
CEdoyunPlayerServer(unsigned count):CBusiness()
1414
{
1515
m_count = count;
16+
1617
}
1718
~CEdoyunPlayerServer()
1819
{
@@ -33,6 +34,10 @@ class CEdoyunPlayerServer : public CBusiness
3334
{
3435
int sock = 0;
3536
int ret = 0;
37+
ret = SetConnectedcallback(&CEdoyunPlayerServer::Connected, this, std::placeholders::_1);
38+
ERR_RETURN(ret, -1);
39+
ret = SetRecvcallback(&CEdoyunPlayerServer::Recived, this, std::placeholders::_1, std::placeholders::_2);
40+
ERR_RETURN(ret, -2)
3641
ret = m_epoll.Create(m_count);
3742
if (ret < 0)
3843
{
@@ -48,19 +53,22 @@ class CEdoyunPlayerServer : public CBusiness
4853
ret = m_pool.AddTask(&CEdoyunPlayerServer::ThreadFunc, this);
4954
if (ret < 0) return -3;
5055
}
56+
sockaddr_in addrin;
5157
while (m_epoll != -1)
5258
{
53-
ret = proc->RecvFd(sock);
59+
ret = proc->RecvSocket(sock,&addrin);
5460
if (ret < 0 || sock == 0)
5561
{
5662
break;
5763
}
5864
CSocketBase* pClient = new CSocket(sock);
5965
if (pClient == NULL) continue;
66+
ret = pClient->Init(CSockParam(&addrin, SOCK_ISIP));
67+
WARN_CONTINUE(ret);
6068
ret = m_epoll.Add(sock, EpollData((void*)pClient));
6169
if (m_connnectedcallback)
6270
{
63-
(*m_connnectedcallback)();
71+
(*m_connnectedcallback)(pClient);
6472
}
6573
WARN_CONTINUE(ret);
6674
}
@@ -91,7 +99,7 @@ class CEdoyunPlayerServer : public CBusiness
9199
WARN_CONTINUE(ret);
92100
if (m_recvcallback)
93101
{
94-
(*m_recvcallback)();
102+
(*m_recvcallback)(pClient, data);
95103
}
96104
}
97105

@@ -109,10 +117,22 @@ class CEdoyunPlayerServer : public CBusiness
109117
}
110118
return 0;
111119
}
120+
121+
122+
int Connected(CSocketBase* pClient)
123+
{
124+
return 0;
125+
}
126+
127+
int Recived(CSocketBase* pClient, const Buffer& data)
128+
{
129+
return 0;
130+
}
112131
private:
113132
CEpoll m_epoll;
114133
CThreadPool m_pool;
115134
std::map<int, CSocketBase*> m_mapClients;
116135
unsigned m_count;
117136

137+
118138
};

PlayServer/Function.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
#pragma once
22
#include<functional>
3-
3+
#include<sys/types.h>
4+
#include<unistd.h>
5+
class CSocketBase;
6+
class Buffer;
7+
//虚函数和模板函数不能通用:虚函数特性和模板函数特性不能同时存在
8+
//但是一个模板类可以有虚函数
49
class CFunctionBase
510
{
611
public:
712
CFunctionBase() {}
813
virtual ~CFunctionBase() {}
9-
virtual int operator()() = 0;
14+
virtual int operator()() { return -1; }
15+
virtual int operator()(CSocketBase*) { return -1; }
16+
virtual int operator()(CSocketBase*, const Buffer&) { return -1; }
1017

1118
private:
1219

@@ -44,8 +51,11 @@ class CFunction : public CFunctionBase
4451
{
4552
return m_binder();
4653
}
47-
4854

4955
private:
5056

51-
};
57+
};
58+
59+
60+
61+

PlayServer/Process.h

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#pragma once
22
#include"Function.h"
33
#include <sys/stat.h>
4+
#include <netinet/in.h>
5+
46

57

68

@@ -74,8 +76,6 @@ class CProcess
7476
msg.msg_control = cmsg;
7577
msg.msg_controllen = cmsg->cmsg_len;
7678
int ret = sendmsg(pipes[1], &msg, 0);
77-
if (ret < 0) return ret;
78-
7979
delete cmsg;
8080
return ret;
8181
}
@@ -107,6 +107,71 @@ class CProcess
107107
return -2;
108108
}
109109
fd = *(int*)CMSG_DATA(cmsg);
110+
delete cmsg;
111+
return 0;
112+
}
113+
114+
115+
int SendSocket(int fd, const sockaddr_in* addrin)
116+
{
117+
//在主进程完成
118+
msghdr msg;
119+
iovec iov[2];
120+
char buf[2][10] = { "sllo","senher" };
121+
iov[0].iov_base = (void*)addrin;
122+
iov[0].iov_len = sizeof(sockaddr_in);
123+
iov[1].iov_base = buf[1];
124+
iov[1].iov_len = sizeof(buf[1]);
125+
msg.msg_iov = iov;
126+
msg.msg_iovlen = 2;
127+
128+
cmsghdr* cmsg = new cmsghdr;
129+
memset(cmsg, 0, CMSG_LEN(sizeof(int)));
130+
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
131+
cmsg->cmsg_level = SOL_SOCKET;
132+
cmsg->cmsg_type = SCM_RIGHTS;
133+
*(int*)CMSG_DATA(cmsg) = fd;
134+
msg.msg_control = cmsg;
135+
msg.msg_controllen = cmsg->cmsg_len;
136+
int ret = sendmsg(pipes[1], &msg, 0);
137+
if (ret < 0)
138+
{
139+
delete cmsg;
140+
return ret;
141+
}
142+
delete cmsg;
143+
return ret;
144+
}
145+
146+
int RecvSocket(int& fd, sockaddr_in* addrin)
147+
{
148+
msghdr msg;
149+
iovec iov[2];
150+
char buf[][10] = { "","" };
151+
iov[0].iov_base = addrin;
152+
iov[0].iov_len = sizeof(addrin);
153+
iov[1].iov_base = buf[1];
154+
iov[1].iov_len = sizeof(buf[1]);
155+
msg.msg_iov = iov;
156+
msg.msg_iovlen = 2;
157+
158+
cmsghdr* cmsg = new cmsghdr();
159+
if (cmsg == NULL) return -1;
160+
memset(cmsg, 0, CMSG_LEN(sizeof(int)));
161+
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
162+
cmsg->cmsg_level = SOL_SOCKET;
163+
cmsg->cmsg_type = SCM_RIGHTS;
164+
msg.msg_control = cmsg;
165+
msg.msg_controllen = CMSG_LEN(sizeof(int));
166+
ssize_t ret = recvmsg(pipes[0], &msg, 0);
167+
if (ret == -1)
168+
{
169+
delete cmsg;
170+
return -2;
171+
}
172+
fd = *(int*)CMSG_DATA(cmsg);
173+
delete cmsg;
174+
return 0;
110175
}
111176
static int SwitchDeamon()
112177
{

PlayServer/Server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ int CServer::ThreadFunc()
8383
CSocketBase* pClient = NULL;
8484
ret = m_server->Link(&pClient);
8585
if (ret != 0) continue;
86-
ret = m_process.SendFD(*pClient);
86+
ret = m_process.SendSocket(*pClient, *pClient);
8787
if (ret != 0)
8888
{
8989
TRACEE("send client %d failed", (int)*pClient);

PlayServer/Server.h

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,52 @@
55
#include"Loggere.h"
66

77

8+
9+
10+
11+
12+
template<typename _FUNCTION, typename... _ARGS>
13+
class CConnetFunction : public CFunctionBase
14+
{
15+
public:
16+
17+
CConnetFunction(_FUNCTION func, _ARGS... args)
18+
:m_binder(std::forward<_FUNCTION>(func), std::forward<_ARGS>(args)...)
19+
{
20+
21+
}
22+
typename std::_Bindres_helper<int, _FUNCTION, _ARGS...>::type m_binder;
23+
virtual ~CConnetFunction() {}
24+
25+
virtual int operator()(CSocketBase* pClient)
26+
{
27+
return m_binder(pClient);
28+
}
29+
30+
};
31+
32+
33+
34+
template<typename _FUNCTION, typename... _ARGS>
35+
class CRecvFunction : public CFunctionBase
36+
{
37+
public:
38+
39+
CRecvFunction(_FUNCTION func, _ARGS... args)
40+
:m_binder(std::forward<_FUNCTION>(func), std::forward<_ARGS>(args)...)
41+
{
42+
43+
}
44+
45+
virtual ~CRecvFunction() {}
46+
virtual int operator()(CSocketBase* pClient, const Buffer& data)
47+
{
48+
return m_binder(pClient, data);
49+
}
50+
typename std::_Bindres_helper<int, _FUNCTION, _ARGS...>::type m_binder;
51+
52+
};
53+
854
class CBusiness
955
{
1056
public:
@@ -18,21 +64,31 @@ class CBusiness
1864
template<typename _FUNCTION_, typename ..._ARGS_>
1965
int SetConnectedcallback(_FUNCTION_ func, _ARGS_... args)
2066
{
21-
m_connnectedcallback = new CFunction<_FUNCTION_, _ARGS_...>(func, args...);
67+
m_connnectedcallback = new CConnetFunction<_FUNCTION_, _ARGS_...>(func, args...);
2268
if (m_connnectedcallback == NULL) return -1;
2369
return 0;
2470

2571
}
2672

27-
2873
template<typename _FUNCTION_, typename ..._ARGS_>
2974
int SetRecvcallback(_FUNCTION_ func, _ARGS_... args)
3075
{
31-
m_recvcallback = new CFunction<_FUNCTION_, _ARGS_...>(func, args...);
76+
m_recvcallback = new CRecvFunction<_FUNCTION_, _ARGS_...>(func, args...);
3277
if (m_recvcallback == NULL) return -1;
3378
return 0;
3479

3580
}
81+
82+
83+
//template<typename _FUNCTION_, typename ..._ARGS_>
84+
//int SetRecvcallback(_FUNCTION_ func, _ARGS_... args)
85+
//{
86+
// m_connnectedcallback = new CConnetFunction<_FUNCTION_, _ARGS_...>(func, args...);
87+
// //m_connnectedcallback = new CConnetFunction<_FUNCTION_, _ARGS_...>(func, args...);
88+
// if (m_recvcallback == NULL) return -1;
89+
// return 0;
90+
91+
//}
3692
protected:
3793

3894
CFunctionBase* m_connnectedcallback;

PlayServer/Socket.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ class CSockParam
7575
strcpy(m_addr_un.sun_path, path);
7676
this->arrt = attr;
7777
}
78+
//网络套接字的初始化
79+
CSockParam(const sockaddr_in* addrin, int attr)
80+
{
81+
this->arrt = attr;
82+
memcpy(&m_addr_in, addrin, sizeof(addrin));
83+
84+
}
7885
~CSockParam(){}
7986
CSockParam(const CSockParam& data)
8087
{
@@ -163,6 +170,14 @@ class CSocketBase
163170
}
164171
virtual operator int() { return m_socket; }
165172
virtual operator int() const{ return m_socket; }
173+
virtual operator const sockaddr_in*() const
174+
{
175+
return &m_param.m_addr_in;
176+
}
177+
virtual operator sockaddr_in* ()
178+
{
179+
return &m_param.m_addr_in;
180+
}
166181
protected:
167182
//套接字描述符 -1
168183
int m_socket;

0 commit comments

Comments
 (0)