Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
H
HTAnticollision
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
马乐
HTAnticollision
Commits
02a36148
Commit
02a36148
authored
Mar 06, 2023
by
马乐
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1.完善登录流程
parent
7b98e668
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
109 additions
and
100 deletions
+109
-100
Signal.kt
.../main/java/com/waytous/anticollision/concurrent/Signal.kt
+0
-46
SessionListener.kt
...ava/com/waytous/anticollision/listener/SessionListener.kt
+21
-0
Error.kt
app/src/main/java/com/waytous/anticollision/tcp/Error.kt
+0
-19
Session.kt
app/src/main/java/com/waytous/anticollision/tcp/Session.kt
+0
-0
SyncMessageListener.kt
...java/com/waytous/anticollision/tcp/SyncMessageListener.kt
+1
-7
SyncParser.kt
...src/main/java/com/waytous/anticollision/tcp/SyncParser.kt
+3
-1
TcpExt.kt
app/src/main/java/com/waytous/anticollision/tcp/TcpExt.kt
+18
-0
TcpManager.kt
...src/main/java/com/waytous/anticollision/tcp/TcpManager.kt
+29
-27
Extensions.kt
...c/main/java/com/waytous/anticollision/utils/Extensions.kt
+37
-0
No files found.
app/src/main/java/com/waytous/anticollision/concurrent/Signal.kt
deleted
100644 → 0
View file @
7b98e668
package
com.waytous.anticollision.concurrent
import
com.blankj.utilcode.util.LogUtils
import
com.waytous.anticollision.BuildConfig
import
com.waytous.anticollision.tcp.Error
import
java.util.concurrent.atomic.AtomicInteger
import
java.util.concurrent.atomic.AtomicReference
import
java.util.concurrent.locks.Condition
import
java.util.concurrent.locks.Lock
class
Signal
(
private
val
lock
:
Lock
,
private
val
count
:
Int
)
{
private
val
condition
:
Condition
=
lock
.
newCondition
()
val
flag
by
lazy
{
AtomicReference
(
Error
.
NOError
)
}
val
retryCount
:
AtomicInteger
=
AtomicInteger
(
count
)
val
mResult
by
lazy
{
AtomicReference
(
Result
.
Timeout
)
}
enum
class
Result
(
val
value
:
Int
){
Invalid
(-
1
),
Success
(
0
),
Timeout
(
1
),
Cancelled
(
2
),
}
fun
taskWait
(
time
:
Long
){
lock
.
lock
()
try
{
condition
.
await
()
}
catch
(
e
:
InterruptedException
)
{
if
(
BuildConfig
.
DEBUG
)
{
LogUtils
.
e
(
"send data error:${e.message}"
)
}
else
{
LogUtils
.
file
(
"send data error:${e.message}"
)
}
}
finally
{
lock
.
unlock
()
}
}
}
\ No newline at end of file
app/src/main/java/com/waytous/anticollision/listener/SessionListener.kt
0 → 100644
View file @
02a36148
package
com.waytous.anticollision.listener
import
com.waytous.anticollision.tcp.Error
import
com.waytous.anticollision.utils.ConnectStatus
interface
SessionListener
{
/**
* 连接状态发生变化
* */
fun
onConnectStatusChanged
(
status
:
ConnectStatus
)
/**
* 用户登录成功
* */
fun
onUserSignIn
()
/**
* 用户登录失败
* */
fun
onUserSignOut
(
error
:
Error
)
}
\ No newline at end of file
app/src/main/java/com/waytous/anticollision/tcp/Error.kt
deleted
100644 → 0
View file @
7b98e668
package
com.waytous.anticollision.tcp
/**
* tcp通信错误定义
* */
enum
class
Error
{
NOError
,
IOError
,
StreamClosed
,
ConnectionRefused
,
Timeout
,
NotConnected
,
UserDisconnected
,
InvalidParam
,
ServerUnknownError
,
JT808EncodeError
,
JT808DecodeError
}
\ No newline at end of file
app/src/main/java/com/waytous/anticollision/tcp/Session.kt
View file @
02a36148
This diff is collapsed.
Click to expand it.
app/src/main/java/com/waytous/anticollision/tcp/SyncMessageListener.kt
View file @
02a36148
...
@@ -9,13 +9,7 @@ interface SyncMessageListener<T> {
...
@@ -9,13 +9,7 @@ interface SyncMessageListener<T> {
* 设备注册
* 设备注册
* @param data
* @param data
* */
* */
fun
onRegistered
(
data
:
T
)
fun
onSignUp
(
data
:
T
)
/**
* 设备鉴权
* @param data
* */
fun
onAuthenticated
(
data
:
T
)
/**
/**
* 平台通用应答
* 平台通用应答
...
...
app/src/main/java/com/waytous/anticollision/tcp/SyncParser.kt
View file @
02a36148
...
@@ -2,6 +2,7 @@ package com.waytous.anticollision.tcp
...
@@ -2,6 +2,7 @@ package com.waytous.anticollision.tcp
import
com.blankj.utilcode.util.LogUtils
import
com.blankj.utilcode.util.LogUtils
import
com.waytous.anticollision.BuildConfig
import
com.waytous.anticollision.BuildConfig
import
com.waytous.anticollision.utils.logd
import
io.github.toggery.jt808.codec.*
import
io.github.toggery.jt808.codec.*
import
io.netty.buffer.ByteBuf
import
io.netty.buffer.ByteBuf
import
io.netty.util.DefaultAttributeMap
import
io.netty.util.DefaultAttributeMap
...
@@ -21,9 +22,10 @@ class SyncParser(private val syncMessageListener: SyncMessageListener<Codec<*>>)
...
@@ -21,9 +22,10 @@ class SyncParser(private val syncMessageListener: SyncMessageListener<Codec<*>>)
fun
parse
(
buf
:
ByteBuf
){
fun
parse
(
buf
:
ByteBuf
){
try
{
try
{
val
message
:
Message
<
Codec
<*>>
=
Message
.
decode
(
buf
,
MessageMetadata
.
outbounds
(),
DefaultAttributeMap
())
val
message
:
Message
<
Codec
<*>>
=
Message
.
decode
(
buf
,
MessageMetadata
.
outbounds
(),
DefaultAttributeMap
())
logd
(
"【解析】:$message"
)
when
(
val
codec
:
Codec
<*>
=
message
.
body
){
when
(
val
codec
:
Codec
<*>
=
message
.
body
){
is
B8001Codec
->
syncMessageListener
.
onCommonResponse
(
codec
)
is
B8001Codec
->
syncMessageListener
.
onCommonResponse
(
codec
)
is
B8100Codec
->
syncMessageListener
.
on
Registered
(
codec
)
is
B8100Codec
->
syncMessageListener
.
on
SignUp
(
codec
)
}
}
LogUtils
.
d
(
message
.
toString
())
LogUtils
.
d
(
message
.
toString
())
}
catch
(
e
:
Exception
)
{
}
catch
(
e
:
Exception
)
{
...
...
app/src/main/java/com/waytous/anticollision/tcp/TcpExt.kt
0 → 100644
View file @
02a36148
package
com.waytous.anticollision.tcp
/**
* tcp通信错误定义
* */
enum
class
Error
(
val
reason
:
String
=
"success"
)
{
NOError
,
IOError
(
"io error"
),
StreamClosed
(
"stream closed"
),
ConnectionRefused
(
"connection refused"
),
Timeout
(
"time out"
),
NotConnected
(
"not connected"
),
UserDisconnected
(
"user disconnected"
),
InvalidParam
(
"invalid param"
),
ServerUnknownError
(
"server unknown error"
),
JT808EncodeError
(
"jt808 codec encode error"
),
JT808DecodeError
(
"jt808 codec decode error"
)
}
app/src/main/java/com/waytous/anticollision/tcp/TcpManager.kt
View file @
02a36148
...
@@ -35,7 +35,7 @@ const val READ_SIZE = 1024
...
@@ -35,7 +35,7 @@ const val READ_SIZE = 1024
* 链接状态
* 链接状态
* @author male
* @author male
* */
* */
enum
class
ConnectStatus
{
internal
enum
class
State
{
/**
/**
* 链接断开
* 链接断开
* */
* */
...
@@ -112,7 +112,7 @@ internal class TcpManager(
...
@@ -112,7 +112,7 @@ internal class TcpManager(
* @param error
* @param error
* */
* */
fun
disconnect
(
error
:
Error
=
Error
.
UserDisconnected
)
{
fun
disconnect
(
error
:
Error
=
Error
.
UserDisconnected
)
{
if
(
connection
.
connectStatus
.
get
()
==
ConnectStatus
.
Disconnected
)
{
if
(
connection
.
connectStatus
.
get
()
==
State
.
Disconnected
)
{
return
return
}
}
receiveExecutor
.
execute
{
receiveExecutor
.
execute
{
...
@@ -127,7 +127,7 @@ internal class TcpManager(
...
@@ -127,7 +127,7 @@ internal class TcpManager(
* @param data
* @param data
* */
* */
fun
send
(
data
:
ByteArray
)
{
fun
send
(
data
:
ByteArray
)
{
if
(
connection
.
connectStatus
.
get
()
==
ConnectStatus
.
Connected
)
{
if
(
connection
.
connectStatus
.
get
()
==
State
.
Connected
)
{
sendExecutors
.
execute
{
connection
.
send
(
data
)
}
sendExecutors
.
execute
{
connection
.
send
(
data
)
}
}
}
}
}
...
@@ -160,8 +160,6 @@ internal class TcpManager(
...
@@ -160,8 +160,6 @@ internal class TcpManager(
* */
* */
class
Connection
{
class
Connection
{
private
val
TAG
=
"Connection"
/**
/**
* 接收线程锁
* 接收线程锁
* */
* */
...
@@ -176,7 +174,12 @@ internal class TcpManager(
...
@@ -176,7 +174,12 @@ internal class TcpManager(
ReentrantLock
()
ReentrantLock
()
}
}
private
lateinit
var
socket
:
Socket
private
val
socket
by
lazy
{
Socket
().
apply
{
keepAlive
=
true
receiveBufferSize
=
BUFFER_SIZE
}
}
/**
/**
* 数据接收流
* 数据接收流
...
@@ -192,7 +195,7 @@ internal class TcpManager(
...
@@ -192,7 +195,7 @@ internal class TcpManager(
* 链接状态
* 链接状态
* */
* */
val
connectStatus
by
lazy
{
val
connectStatus
by
lazy
{
AtomicReference
(
ConnectStatus
.
Disconnected
)
AtomicReference
(
State
.
Disconnected
)
}
}
/**
/**
...
@@ -213,36 +216,32 @@ internal class TcpManager(
...
@@ -213,36 +216,32 @@ internal class TcpManager(
receiveLock
.
lock
()
receiveLock
.
lock
()
sendLock
.
lock
()
sendLock
.
lock
()
var
errorCode
:
Error
=
Error
.
NOError
var
errorCode
:
Error
=
Error
.
NOError
if
(
socket
!=
null
&&
socket
.
isConnected
&&
connectStatus
.
get
()
>
ConnectStatus
.
Disconnected
)
{
if
(
socket
.
isConnected
&&
connectStatus
.
get
()
>
State
.
Disconnected
)
{
errorCode
=
Error
.
NOError
errorCode
=
Error
.
NOError
}
else
if
(
socket
!=
null
)
{
}
else
{
closeSocket
()
closeSocket
()
}
}
connectStatus
.
set
(
ConnectStatus
.
Connecting
)
connectStatus
.
set
(
State
.
Connecting
)
socket
=
Socket
().
apply
{
socket
.
soTimeout
=
receiveInterval
soTimeout
=
receiveInterval
keepAlive
=
true
receiveBufferSize
=
BUFFER_SIZE
}
socket
.
connect
(
socket
.
connect
(
InetSocketAddress
(
host
,
port
),
InetSocketAddress
(
host
,
port
),
if
(
timeout
>
0
)
timeout
else
DEFAULT_TIMEOUT
if
(
timeout
>
0
)
timeout
else
DEFAULT_TIMEOUT
)
)
inputStream
=
socket
.
getInputStream
()
inputStream
=
socket
.
getInputStream
()
outputStream
=
socket
.
getOutputStream
()
outputStream
=
socket
.
getOutputStream
()
connectStatus
.
set
(
ConnectStatus
.
Connected
)
connectStatus
.
set
(
State
.
Connected
)
errorCode
errorCode
}
catch
(
e
:
IOException
)
{
}
catch
(
e
:
IOException
)
{
connectStatus
.
set
(
ConnectStatus
.
Disconnected
)
connectStatus
.
set
(
State
.
Disconnected
)
Error
.
IOError
Error
.
IOError
}
catch
(
e
:
SocketTimeoutException
)
{
}
catch
(
e
:
SocketTimeoutException
)
{
connectStatus
.
set
(
ConnectStatus
.
Disconnected
)
connectStatus
.
set
(
State
.
Disconnected
)
Error
.
Timeout
Error
.
Timeout
}
catch
(
e
:
IllegalArgumentException
)
{
}
catch
(
e
:
IllegalArgumentException
)
{
connectStatus
.
set
(
ConnectStatus
.
Disconnected
)
connectStatus
.
set
(
State
.
Disconnected
)
Error
.
InvalidParam
Error
.
InvalidParam
}
catch
(
e
:
Exception
)
{
}
catch
(
e
:
Exception
)
{
connectStatus
.
set
(
ConnectStatus
.
Disconnected
)
connectStatus
.
set
(
State
.
Disconnected
)
Error
.
ServerUnknownError
Error
.
ServerUnknownError
}
finally
{
}
finally
{
sendLock
.
unlock
()
sendLock
.
unlock
()
...
@@ -289,7 +288,7 @@ internal class TcpManager(
...
@@ -289,7 +288,7 @@ internal class TcpManager(
fun
receive
():
ReceivedResult
{
fun
receive
():
ReceivedResult
{
try
{
try
{
receiveLock
.
lock
()
receiveLock
.
lock
()
if
(
connectStatus
.
get
()
!=
ConnectStatus
.
Connected
)
return
ReceivedResult
(
if
(
connectStatus
.
get
()
!=
State
.
Connected
)
return
ReceivedResult
(
Error
.
NotConnected
,
Error
.
NotConnected
,
0
0
)
)
...
@@ -299,22 +298,25 @@ internal class TcpManager(
...
@@ -299,22 +298,25 @@ internal class TcpManager(
ReceivedResult
(
Error
.
NOError
,
len
)
ReceivedResult
(
Error
.
NOError
,
len
)
}
else
{
}
else
{
if
(
len
==
0
)
{
if
(
len
==
0
)
{
connectStatus
.
set
(
ConnectStatus
.
Disconnected
)
connectStatus
.
set
(
State
.
Disconnected
)
ReceivedResult
(
Error
.
StreamClosed
,
len
)
ReceivedResult
(
Error
.
StreamClosed
,
len
)
}
else
{
}
else
{
cleanup
()
cleanup
()
connectStatus
.
set
(
ConnectStatus
.
Disconnected
)
connectStatus
.
set
(
State
.
Disconnected
)
ReceivedResult
(
Error
.
IOError
,
len
)
ReceivedResult
(
Error
.
IOError
,
len
)
}
}
}
}
}
catch
(
e
:
IOException
)
{
}
catch
(
e
:
IOException
)
{
connectStatus
.
set
(
ConnectStatus
.
Disconnected
)
connectStatus
.
set
(
State
.
Disconnected
)
return
ReceivedResult
(
Error
.
IOError
,
-
1
)
return
ReceivedResult
(
Error
.
IOError
,
-
1
)
}
catch
(
e
:
SocketTimeoutException
)
{
connectStatus
.
set
(
State
.
Disconnected
)
return
ReceivedResult
(
Error
.
Timeout
,
0
)
}
catch
(
e
:
IllegalArgumentException
)
{
}
catch
(
e
:
IllegalArgumentException
)
{
connectStatus
.
set
(
ConnectStatus
.
Disconnected
)
connectStatus
.
set
(
State
.
Disconnected
)
return
ReceivedResult
(
Error
.
InvalidParam
,
0
)
return
ReceivedResult
(
Error
.
InvalidParam
,
0
)
}
catch
(
e
:
Exception
)
{
}
catch
(
e
:
Exception
)
{
connectStatus
.
set
(
ConnectStatus
.
Disconnected
)
connectStatus
.
set
(
State
.
Disconnected
)
return
ReceivedResult
(
Error
.
ServerUnknownError
,
0
)
return
ReceivedResult
(
Error
.
ServerUnknownError
,
0
)
}
finally
{
}
finally
{
receiveLock
.
unlock
()
receiveLock
.
unlock
()
...
@@ -333,7 +335,7 @@ internal class TcpManager(
...
@@ -333,7 +335,7 @@ internal class TcpManager(
receiveLock
.
lock
()
receiveLock
.
lock
()
sendLock
.
lock
()
sendLock
.
lock
()
closeSocket
()
closeSocket
()
connectStatus
.
set
(
ConnectStatus
.
Disconnected
)
connectStatus
.
set
(
State
.
Disconnected
)
}
catch
(
e
:
Exception
)
{
}
catch
(
e
:
Exception
)
{
if
(
BuildConfig
.
DEBUG
)
{
if
(
BuildConfig
.
DEBUG
)
{
LogUtils
.
e
(
"cleanup error:${e.message}"
)
LogUtils
.
e
(
"cleanup error:${e.message}"
)
...
...
app/src/main/java/com/waytous/anticollision/utils/Extensions.kt
View file @
02a36148
package
com.waytous.anticollision.utils
package
com.waytous.anticollision.utils
import
com.blankj.utilcode.util.LogUtils
import
com.waytous.anticollision.BuildConfig
/**
* 连接状态
* */
enum
class
ConnectStatus
{
Disconnected
,
Connected
}
/**
* 登录状态
* */
enum
class
SignInStatus
{
SignOut
,
SignIn
}
fun
logd
(
message
:
String
){
if
(
BuildConfig
.
DEBUG
)
{
if
(
BuildConfig
.
DEBUG
)
{
LogUtils
.
d
(
message
)
}
else
{
LogUtils
.
file
(
message
)
}
}
}
fun
loge
(
message
:
String
){
if
(
BuildConfig
.
DEBUG
)
{
if
(
BuildConfig
.
DEBUG
)
{
LogUtils
.
e
(
message
)
}
else
{
LogUtils
.
file
(
message
)
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment