目前,客戶端發送的消息是這樣的:更好的socket通信系統
Public Function checkMD5(ByVal userID As Integer, ByVal gameID As Integer, ByVal file As String, ByVal fileFull As String) As String
Dim make As New CMakeMSG
Dim md5 As New CMD5
make.append("checkfileMD5")
make.append(userID)
make.append(containerID)
make.append(file)
make.append(md5.GenerateFileHash(fileFull))
Return SocketSendAndReceiveMSG(make.makestring)
End Function
服務器可能會收到這樣的事情:
checkfileMD5-MSGDelimit0-12-MSGDelimit1-54-MSGDelimit2-filename.txt-MSGDelimit3-*md5hash*
它然後讀出:
Private _message As String
Public Function handleMessage() As String
Dim brokenMessage As New ArrayList
brokenMessage = breakDown() 'Split to ArrayList
If brokenMessage(0) = "checkfileMD5" Then
Try
If brokenMessage.Count > 5 Then
Return "0-structureMessedUp"
End If
Return CompareFileMD5(brokenMessage(1), brokenMessage(2), brokenMessage(3), brokenMessage(4))
Catch ex As Exception
Return "0-structureMessedUp"
End Try
End If
End Function
所以它所做的就是將收到的信息和spli將其轉換爲使用-MSGDelimit-作爲分隔符的數組。所以在這種情況下,CompareFileMD5()
函數將收到12,54,filename.txt,*md5hash*
。並基於它可以返回到客戶端,無論MD5是否匹配。
當然,它工作,但它感覺sl and,服務器上的代碼變得非常混亂。
這裏的相關性較低的功能,從上面的代碼(懷疑的問題,但你永遠不知道):
Private Function breakDown() As ArrayList
Try
Dim theArray As New ArrayList
Dim copymsg As String = _message
Dim counter As Integer = 0
Do Until Not copymsg.Contains("-MSGDelimit")
Dim found As String
found = copymsg.Substring(0, copymsg.IndexOf("-MSGDelimit" & counter & "-"))
theArray.Add(found)
copymsg = copymsg.Replace(found & "-MSGDelimit" & counter & "-", "")
counter += 1
Loop
theArray.Add(copymsg)
Return theArray
Catch ex As Exception
Module1.msg(ex.Message)
End Try
End Function
Private Function CompareFileMD5(ByVal userID As Integer, ByVal gameID As Integer, ByVal filename As String, ByVal source As String) As String
Try
Dim tryFindFile As String = Module1.filedatabase.findfile(userID, gameID, filename)
If Not tryFindFile = "notFound" Then
Dim fileFull As String = tryFindFile & "\" & filename
Dim md5 As New CMD5
If md5.GenerateFileHash(fileFull) = source Then
Return "Match"
Else
Return "NoMatch"
End If
Else
Return "notFound"
End If
Catch ex As Exception
Module1.msg("0")
Return "0"
End Try
End Function
那麼,該如何處理的更好/清潔/更專業的建議嗎?
感謝您的回覆,您能否詳細說明''可能是發送數據的客戶端版本'',因爲我不明白這句話。目前JSON看起來相當不錯。 – natli 2012-02-22 16:22:51
@natli:對不起,這並沒有幫助我在句子中留下了一個字。我編輯並添加了澄清信息。 – 2012-02-22 16:30:19
啊,現在有道理!非常感謝,看起來我有一些工作要做;) – natli 2012-02-22 16:39:19