Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
M
mediasoup
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
聂卓夫
mediasoup
Commits
cbd4017d
提交
cbd4017d
authored
12月 19, 2015
作者:
Iñaki Baz Castillo
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Channel: more API
上级
dc129757
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
126 行增加
和
44 行删除
+126
-44
Request.js
lib/Channel/Request.js
+16
-9
index.js
lib/Channel/index.js
+7
-14
Room.js
lib/Room.js
+2
-4
Request.h
worker/include/Channel/Request.h
+8
-4
Request.cpp
worker/src/Channel/Request.cpp
+78
-8
Loop.cpp
worker/src/Loop.cpp
+15
-5
没有找到文件。
lib/Channel/Request.js
浏览文件 @
cbd4017d
'use strict'
;
'use strict'
;
const
random
String
=
require
(
'random-string
'
);
const
random
Number
=
require
(
'random-number
'
);
const
logger
=
require
(
'../logger'
)(
'Channel:Request'
);
const
logger
=
require
(
'../logger'
)(
'Channel:Request'
);
const
randomNumberGenerator
=
randomNumber
.
generator
(
{
min
:
10000
,
max
:
99999
,
integer
:
true
});
class
Request
class
Request
{
{
constructor
(
method
,
data
)
constructor
(
method
,
data
)
{
{
logger
.
debug
(
'constructor() [method:%s]'
,
method
);
logger
.
debug
(
'constructor() [method:%s]'
,
method
);
this
.
_id
=
random
String
({
length
:
8
}).
toLowerCase
();
this
.
_id
=
random
NumberGenerator
();
this
.
_method
=
method
;
this
.
_method
=
method
;
this
.
_data
=
data
||
{};
this
.
_data
=
data
||
{};
}
}
...
@@ -18,20 +25,20 @@ class Request
...
@@ -18,20 +25,20 @@ class Request
{
{
return
JSON
.
stringify
(
return
JSON
.
stringify
(
{
{
request
:
this
.
_metho
d
,
id
:
this
.
_i
d
,
id
:
this
.
_i
d
,
method
:
this
.
_metho
d
,
data
:
this
.
_data
data
:
this
.
_data
});
});
}
}
get
metho
d
()
get
i
d
()
{
{
return
this
.
_
metho
d
;
return
this
.
_
i
d
;
}
}
get
i
d
()
get
metho
d
()
{
{
return
this
.
_
i
d
;
return
this
.
_
metho
d
;
}
}
get
data
()
get
data
()
...
...
lib/Channel/index.js
浏览文件 @
cbd4017d
...
@@ -31,21 +31,15 @@ class Channel extends EventEmitter
...
@@ -31,21 +31,15 @@ class Channel extends EventEmitter
// No recvBuffer
// No recvBuffer
if
(
!
this
.
_recvBuffer
)
if
(
!
this
.
_recvBuffer
)
{
{
// TODO: remove
// logger.debug('recvBuffer was empty');
this
.
_recvBuffer
=
buffer
;
this
.
_recvBuffer
=
buffer
;
}
}
else
else
{
{
// TODO: remove
// logger.debug('recvBuffer was not empty [length:%s]', this._recvBuffer.length);
this
.
_recvBuffer
=
Buffer
.
concat
([
this
.
_recvBuffer
,
buffer
],
this
.
_recvBuffer
.
length
+
buffer
.
length
);
this
.
_recvBuffer
=
Buffer
.
concat
([
this
.
_recvBuffer
,
buffer
],
this
.
_recvBuffer
.
length
+
buffer
.
length
);
if
(
this
.
_recvBuffer
.
length
>
MESSAGE_MAX_SIZE
)
if
(
this
.
_recvBuffer
.
length
>
MESSAGE_MAX_SIZE
)
{
{
logger
.
error
(
'recvBuffer full, discarding all the data in it'
);
logger
.
error
(
'recvBuffer
is
full, discarding all the data in it'
);
// Reset the recvBuffer and exit
// Reset the recvBuffer and exit
this
.
_recvBuffer
=
null
;
this
.
_recvBuffer
=
null
;
...
@@ -100,9 +94,6 @@ class Channel extends EventEmitter
...
@@ -100,9 +94,6 @@ class Channel extends EventEmitter
if
(
!
this
.
_recvBuffer
.
length
)
if
(
!
this
.
_recvBuffer
.
length
)
{
{
// TODO: remove
// logger.debug('no more data in the buffer');
this
.
_recvBuffer
=
null
;
this
.
_recvBuffer
=
null
;
return
;
return
;
}
}
...
@@ -197,12 +188,10 @@ class Channel extends EventEmitter
...
@@ -197,12 +188,10 @@ class Channel extends EventEmitter
_processMessage
(
json
)
_processMessage
(
json
)
{
{
logger
.
debug
(
'_processMessage()'
);
// TODO: remove
// TODO: remove
logger
.
debug
(
'received json: %o'
,
json
);
logger
.
debug
(
'received json: %o'
,
json
);
// If a Response retrieve its associated Request
// If a Response
,
retrieve its associated Request
if
(
json
.
id
)
if
(
json
.
id
)
{
{
let
sent
=
this
.
_pendingSent
.
get
(
json
.
id
);
let
sent
=
this
.
_pendingSent
.
get
(
json
.
id
);
...
@@ -220,7 +209,11 @@ class Channel extends EventEmitter
...
@@ -220,7 +209,11 @@ class Channel extends EventEmitter
}
}
else
else
{
{
sent
.
reject
(
json
.
data
);
let
error
=
new
Error
(
json
.
reason
);
// Add .status to the Error instance
error
.
status
=
json
.
status
;
sent
.
reject
(
error
);
}
}
}
}
}
}
...
...
lib/Room.js
浏览文件 @
cbd4017d
...
@@ -28,7 +28,6 @@ class Room extends EventEmitter
...
@@ -28,7 +28,6 @@ class Room extends EventEmitter
bar
:
'œæ€😤😮😱Ω∑©'
bar
:
'œæ€😤😮😱Ω∑©'
};
};
// setInterval(() => {
this
.
_channel
.
request
(
'createRoom'
,
data
)
this
.
_channel
.
request
(
'createRoom'
,
data
)
.
then
(()
=>
.
then
(()
=>
{
{
...
@@ -36,15 +35,14 @@ class Room extends EventEmitter
...
@@ -36,15 +35,14 @@ class Room extends EventEmitter
this
.
_status
=
STATUS
.
OPEN
;
this
.
_status
=
STATUS
.
OPEN
;
this
.
emit
(
'open'
);
this
.
emit
(
'open'
,
data
);
})
})
.
catch
((
error
)
=>
.
catch
((
error
)
=>
{
{
logger
.
error
(
'"createRoom" command failed [
error:%s]'
,
error
);
logger
.
error
(
'"createRoom" command failed [
status:%s, error:"%s"]'
,
error
.
status
,
error
.
message
);
this
.
close
(
error
);
this
.
close
(
error
);
});
});
// }, 20);
}
}
close
(
error
)
close
(
error
)
...
...
worker/include/Channel/Request.h
浏览文件 @
cbd4017d
...
@@ -13,18 +13,22 @@ namespace Channel
...
@@ -13,18 +13,22 @@ namespace Channel
class
Request
class
Request
{
{
public
:
public
:
static
Channel
::
Request
*
Factory
(
Channel
::
UnixStreamSocket
*
channel
,
Json
::
Value
&
msg
);
static
Channel
::
Request
*
Factory
(
Channel
::
UnixStreamSocket
*
channel
,
Json
::
Value
&
json
);
public
:
public
:
Request
(
Channel
::
UnixStreamSocket
*
channel
,
std
::
string
&
id
,
std
::
string
&
method
,
Json
::
Value
&
data
);
Request
(
Channel
::
UnixStreamSocket
*
channel
,
unsigned
int
id
,
std
::
string
&
method
,
Json
::
Value
&
data
);
virtual
~
Request
();
virtual
~
Request
();
void
Reply
(
int
status
);
// void Accept(Json::Value data = Json::Value(Json::nullValue));
void
Accept
();
void
Accept
(
Json
::
Value
&
data
);
void
Reject
(
unsigned
int
status
,
std
::
string
&
reason
);
void
Reject
(
unsigned
int
status
,
const
char
*
reason
=
nullptr
);
public
:
public
:
// Passed by argument:
// Passed by argument:
Channel
::
UnixStreamSocket
*
channel
=
nullptr
;
Channel
::
UnixStreamSocket
*
channel
=
nullptr
;
std
::
string
id
;
unsigned
int
id
;
std
::
string
method
;
std
::
string
method
;
Json
::
Value
data
;
Json
::
Value
data
;
// Others:
// Others:
...
...
worker/src/Channel/Request.cpp
浏览文件 @
cbd4017d
...
@@ -7,27 +7,55 @@ namespace Channel
...
@@ -7,27 +7,55 @@ namespace Channel
{
{
/* Static methods. */
/* Static methods. */
Channel
::
Request
*
Request
::
Factory
(
Channel
::
UnixStreamSocket
*
channel
,
Json
::
Value
&
msg
)
Channel
::
Request
*
Request
::
Factory
(
Channel
::
UnixStreamSocket
*
channel
,
Json
::
Value
&
json
)
{
{
MS_TRACE
();
MS_TRACE
();
unsigned
int
id
;
std
::
string
method
;
Json
::
Value
data
;
// TODO: TMP
// TODO: TMP
Json
::
FastWriter
fastWriter
;
Json
::
FastWriter
fastWriter
;
fastWriter
.
dropNullPlaceholders
();
fastWriter
.
dropNullPlaceholders
();
fastWriter
.
omitEndingLineFeed
();
fastWriter
.
omitEndingLineFeed
();
MS_DEBUG
(
"JSON msg: %s"
,
fastWriter
.
write
(
msg
).
c_str
());
MS_DEBUG
(
"JSON msg: %s"
,
fastWriter
.
write
(
json
).
c_str
());
// Check fields.
if
(
json
[
"id"
].
isUInt
())
{
id
=
json
[
"id"
].
asUInt
();
}
else
{
MS_ERROR
(
"json has no numeric .id field"
);
return
nullptr
;
}
if
(
json
[
"method"
].
isString
())
{
method
=
json
[
"method"
].
asString
();
}
else
{
MS_ERROR
(
"json has no string .method field"
);
// TODO: Check keys.
return
nullptr
;
}
std
::
string
id
=
msg
[
"id"
].
asString
();
if
(
json
[
"data"
].
isObject
())
std
::
string
method
=
msg
[
"method"
].
asString
();
data
=
json
[
"data"
];
else
data
=
Json
::
Value
(
Json
::
objectValue
);
return
new
Request
(
channel
,
id
,
method
,
msg
[
"data"
]
);
return
new
Request
(
channel
,
id
,
method
,
data
);
}
}
/* Instance methods. */
/* Instance methods. */
Request
::
Request
(
Channel
::
UnixStreamSocket
*
channel
,
std
::
string
&
id
,
std
::
string
&
method
,
Json
::
Value
&
data
)
:
Request
::
Request
(
Channel
::
UnixStreamSocket
*
channel
,
unsigned
int
id
,
std
::
string
&
method
,
Json
::
Value
&
data
)
:
channel
(
channel
),
channel
(
channel
),
id
(
id
),
id
(
id
),
method
(
method
),
method
(
method
),
...
@@ -41,7 +69,46 @@ namespace Channel
...
@@ -41,7 +69,46 @@ namespace Channel
MS_TRACE
();
MS_TRACE
();
}
}
void
Request
::
Reply
(
int
status
)
void
Request
::
Accept
()
{
MS_TRACE
();
static
Json
::
Value
no_data
(
Json
::
nullValue
);
Accept
(
no_data
);
}
void
Request
::
Accept
(
Json
::
Value
&
data
)
{
MS_TRACE
();
if
(
this
->
replied
)
{
MS_ERROR
(
"already replied"
);
return
;
}
this
->
replied
=
true
;
Json
::
Value
json
;
json
[
"id"
]
=
this
->
id
;
json
[
"status"
]
=
200
;
if
(
data
.
isObject
())
json
[
"data"
]
=
data
;
this
->
channel
->
Send
(
json
);
}
void
Request
::
Reject
(
unsigned
int
status
,
std
::
string
&
reason
)
{
MS_TRACE
();
Reject
(
status
,
reason
.
c_str
());
}
void
Request
::
Reject
(
unsigned
int
status
,
const
char
*
reason
)
{
{
MS_TRACE
();
MS_TRACE
();
...
@@ -58,6 +125,9 @@ namespace Channel
...
@@ -58,6 +125,9 @@ namespace Channel
json
[
"id"
]
=
this
->
id
;
json
[
"id"
]
=
this
->
id
;
json
[
"status"
]
=
status
;
json
[
"status"
]
=
status
;
if
(
reason
)
json
[
"reason"
]
=
reason
;
this
->
channel
->
Send
(
json
);
this
->
channel
->
Send
(
json
);
}
}
}
}
worker/src/Loop.cpp
浏览文件 @
cbd4017d
...
@@ -94,16 +94,26 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
...
@@ -94,16 +94,26 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
{
{
MS_TRACE
();
MS_TRACE
();
char
ch
=
request
->
id
.
back
();
if
(
request
->
id
<
25000
)
{
request
->
Accept
();
}
else
if
(
request
->
id
<
50000
)
{
Json
::
Value
data
;
if
(
ch
>
'm'
)
data
[
"jojojo"
]
=
"🐮🐷🐣😡"
;
request
->
Accept
(
data
);
}
else
if
(
request
->
id
<
75000
)
{
{
// jsonResponse["data"] = "🐮🐷🐣😡";
std
::
string
reason
=
"this is a std::string reason"
;
request
->
Reply
(
200
);
request
->
Reject
(
400
,
reason
);
}
}
else
else
{
{
request
->
Re
ply
(
500
);
request
->
Re
ject
(
500
,
"this is a char* reason"
);
}
}
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论