use more inherit features
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -259,3 +259,4 @@ paket-files/
|
||||
# Python Tools for Visual Studio (PTVS)
|
||||
__pycache__/
|
||||
*.pyc
|
||||
/*/*.xml
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
Imports System.IO
|
||||
Imports System.Net
|
||||
Imports Newtonsoft.Json.Linq
|
||||
|
||||
Public MustInherit Class ConnectionManagerBase
|
||||
@@ -29,7 +30,7 @@ Public MustInherit Class ConnectionManagerBase
|
||||
|
||||
Public MustOverride Sub Start()
|
||||
Public MustOverride Sub [Stop]()
|
||||
Public MustOverride Sub Send(empfängerIP As String, cmd As String, content As Object)
|
||||
Protected MustOverride Sub SendData(endPoint As IPEndPoint, data As Byte())
|
||||
|
||||
Public Overridable Sub Send(empfängerIP As String, cmd As String)
|
||||
Send(empfängerIP, cmd, String.Empty)
|
||||
@@ -39,10 +40,25 @@ Public MustInherit Class ConnectionManagerBase
|
||||
Send(empfängerIP, cmd, CObj(info))
|
||||
End Sub
|
||||
|
||||
Protected Sub RaiseRetriveData(senderIP As String, cmd As String, content As Object)
|
||||
Private Sub RaiseRetriveData(senderIP As String, cmd As String, content As Object)
|
||||
RaiseEvent RetriveData(Me, senderIP, cmd, content)
|
||||
End Sub
|
||||
|
||||
Protected Sub ProcessRetrivedData(senderIP As String, buf As Byte())
|
||||
Dim contentstring As String = Text.Encoding.Default.GetString(buf)
|
||||
Dim content As Object = Nothing
|
||||
Dim cmd As String = String.Empty
|
||||
|
||||
Try
|
||||
Dim res = DecodeFromBytes(buf)
|
||||
cmd = res.cmd
|
||||
content = res.content
|
||||
Catch ex As Exception
|
||||
End Try
|
||||
|
||||
RaiseRetriveData(senderIP, cmd, content)
|
||||
End Sub
|
||||
|
||||
Protected Shared Function EncodeToBytes(cmd As String, content As Object, useAssemblyQualifiedName As Boolean) As Byte()
|
||||
Dim ms As New MemoryStream()
|
||||
Dim bw As New BinaryWriter(ms)
|
||||
@@ -82,4 +98,11 @@ Public MustInherit Class ConnectionManagerBase
|
||||
Return (cmd, content)
|
||||
End Function
|
||||
|
||||
Public Sub Send(empfängerIP As String, cmd As String, content As Object)
|
||||
Dim ep As New IPEndPoint(GetIPFromHost(empfängerIP).MapToIPv4, Port)
|
||||
Dim buf As Byte() = EncodeToBytes(cmd, content, UseAssemblyQualifiedName)
|
||||
|
||||
SendData(ep, buf)
|
||||
End Sub
|
||||
|
||||
End Class
|
||||
|
||||
@@ -44,33 +44,20 @@ Public Class TCPManager
|
||||
tcp.ReceiveBufferSize = BufferSize
|
||||
Stream.Read(buf, 0, buf.Length)
|
||||
|
||||
Dim contentstring As String = Text.Encoding.Default.GetString(buf)
|
||||
Dim content As Object = Nothing
|
||||
Dim cmd As String = String.Empty
|
||||
|
||||
Try
|
||||
Dim res = DecodeFromBytes(buf)
|
||||
cmd = res.cmd
|
||||
content = res.content
|
||||
Catch ex As Exception
|
||||
End Try
|
||||
|
||||
tcp.Close()
|
||||
|
||||
RaiseRetriveData(ip, cmd, content)
|
||||
ProcessRetrivedData(ip, buf)
|
||||
End If
|
||||
Loop
|
||||
End Sub
|
||||
|
||||
Public Overrides Sub Send(empfängerIP As String, cmd As String, content As Object)
|
||||
Dim ep As New IPEndPoint(GetIPFromHost(empfängerIP).MapToIPv4, Port)
|
||||
Protected Overrides Sub SendData(ep As IPEndPoint, buf As Byte())
|
||||
Dim tcp As New TcpClient
|
||||
|
||||
tcp.SendBufferSize = BufferSize
|
||||
tcp.Connect(ep)
|
||||
|
||||
Dim stream As NetworkStream = tcp.GetStream()
|
||||
Dim buf As Byte() = EncodeToBytes(cmd, content, UseAssemblyQualifiedName)
|
||||
|
||||
'Send Data
|
||||
stream.Write(buf, 0, buf.Length)
|
||||
|
||||
@@ -12,6 +12,7 @@ Public Class UDPManager
|
||||
Private listenTask As Task = Nothing
|
||||
Private ReadOnly cancelTokenSource As New CancellationTokenSource
|
||||
Private ReadOnly cancelToken As CancellationToken = cancelTokenSource.Token
|
||||
Public ReadOnly Property MaxBufferSize As Integer = &H10000
|
||||
|
||||
Public Sub New(port As Integer)
|
||||
MyBase.New(port)
|
||||
@@ -62,29 +63,18 @@ Public Class UDPManager
|
||||
|
||||
Dim buf As Byte() = receiveTask.Result.Buffer
|
||||
Dim ip As String = receiveTask.Result.RemoteEndPoint.Address.ToString
|
||||
Dim cmd As String = String.Empty
|
||||
Dim content As Object = Nothing
|
||||
|
||||
doExit()
|
||||
|
||||
Try
|
||||
Dim res = DecodeFromBytes(buf)
|
||||
cmd = res.cmd
|
||||
content = res.content
|
||||
Catch ex As Exception
|
||||
End Try
|
||||
|
||||
RaiseRetriveData(ip, cmd, content)
|
||||
ProcessRetrivedData(ip, buf)
|
||||
|
||||
doExit()
|
||||
|
||||
StartInternal()
|
||||
End Sub
|
||||
|
||||
Public Overrides Sub Send(empfängerIP As String, cmd As String, content As Object)
|
||||
Dim ep As New IPEndPoint(GetIPFromHost(empfängerIP).MapToIPv4, Port)
|
||||
Protected Overrides Sub SendData(ep As IPEndPoint, buf As Byte())
|
||||
Dim udp As New UdpClient
|
||||
Dim buf As Byte() = EncodeToBytes(cmd, content, UseAssemblyQualifiedName)
|
||||
|
||||
udp.Connect(ep)
|
||||
udp.Send(buf, buf.Length)
|
||||
udp.Client.Shutdown(SocketShutdown.Both)
|
||||
|
||||
Reference in New Issue
Block a user