contin Posted December 4, 2009 Posted December 4, 2009 (edited) This is my currently "working" attempt at a TCP Client/Server relationship to control Winamp through a GUI on another computer. So far, it is rather basic, and I am looking for codesnippets to increase the response time / make cleaner code + Increase my own knowledge Credit goes to.. G.Sandler (CreatoR's Lab - http://creator-lab.ucoz.ru) - Winamp Automation Library Kip - TCP UDF v3 Server expandcollapse popup#include "TCP.au3" #include "Winamp_Library.au3" Global $a = 0 Global $i = 1 Global $playlist = 0 $hServer = _TCP_Server_Create(6112); A server. Tadaa! Global $trackList _TCP_RegisterEvent($hServer, $TCP_NEWCLIENT, "NewClient"); Whooooo! Now, this function (NewClient) get's called when a new client connects to the server. _TCP_RegisterEvent($hServer, $TCP_DISCONNECT, "Disconnect"); And this,... this will get called when a client disconnects. _TCP_RegisterEvent($hServer, $TCP_RECEIVE, "Received") While 1 Sleep(10) WEnd Func Received($hSocket, $sReceived, $iError); And we also registered this! Our homemade do-it-yourself function gets called when something is received. If $a = 1 Then _playTrack($sReceived) $a = 0 EndIf If $a = 0 Then If $sReceived == "start:" Then _startWinamp() If $sReceived == "next:" Then _nextTrack() If $sReceived == "array:" Then _TCP_Send($hSocket, $trackList[$i][0]) $i = $i + 1 EndIf If $sReceived == "nextSong:" Then If $Playlist <> 1 Then _TCP_Send($hSocket, "notready:") Else If $i == UBound($trackList)-1 Then _TCP_Send($hSocket, "done:") $i = 1 Else _TCP_Send($hSocket, $trackList[$i][0]) $i = $i + 1 EndIf EndIf EndIf If $sReceived == "play:" Then _TCP_Send($hSocket, "track:") $a = 1 EndIf Endif EndFunc Func NewClient($hSocket, $iError); Yo, check this out! It's a $iError parameter! (In case you didn't noticed: It's in every function) _TCP_Send($hSocket, "start:"); Sending: "Bleh!" to the new client. (Yes, that's right: $hSocket is the socket of the new client.) EndFunc Func Disconnect($hSocket, $iError); Damn, we lost a client. Time of death: @Hour & @Min & @Sec :P EndFunc Func _startWinamp() _Winamp_Start(-1, 1) If @error Then MsgBox(16, "Error", StringFormat("There was an error (%d) executing Winamp Player.", @error) & _ @CRLF & @CRLF & "OK ===> EXIT") Exit EndIf Local $aTracksList = _Winamp_GetPlayListToArray() Local $iTotalSongs = UBound($aTracksList)-1 If $iTotalSongs < 1 Then Local $sSelectedSong = FileOpenDialog("Select Song", "C:\", "Media files (*.mp3;*.wav;*.mdi)", 7) If @error Then Exit If StringInStr($sSelectedSong, "|") Then Local $aSelectedSongs = StringSplit($sSelectedSong, "|") $iTotalSongs = 0 For $i = 2 To UBound($aSelectedSongs)-1 _Winamp_AddFile($aSelectedSongs[1] & "\" & $aSelectedSongs[$i]) $iTotalSongs += 1 Next Else _Winamp_AddFile($sSelectedSong) $iTotalSongs = 1 EndIf EndIf If _Winamp_GetCurrentTrackPlayStatus() <> 1 Then _Winamp_Play() EndIf $trackList = _Winamp_GetPlayListToArray() $playlist = 1 EndFunc Func _nextTrack() _Winamp_Next() EndFunc Func _playTrack($i) ConsoleWrite($i) _Winamp_Play($i-1) EndFunc Client expandcollapse popup#include "TCP.au3" #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <GuiListView.au3> Opt("GUIOnEventMode", 1) $hGUI = GUICreate("Music Controller", 500, 500) Global $labelStatus = GUICtrlCreateLabel("There is no life.", 8, 5, 495) Global $arraySpot = 0 Global $switch = 0 Global $sSocket Global $aArray [2][2] = [["1", "To get song list Press Get Songs"], ["2", ""]] Global $hEdit[2] $hEdit[0] = GUICtrlCreateEdit("", 10, 30, 30, 20, $WS_TABSTOP) $hEdit[1] = GUICtrlCreateEdit("", 40, 30, 460, 20, $WS_TABSTOP) $hPlay_Button = GUICtrlCreateButton("Play Track", 10, 60, 80, 30) GUICtrlSetOnEvent(-1, "_Play") $hGet_Button = GUICtrlCreateButton("Get Song List", 100, 60, 80, 30) GUICtrlSetOnEvent(-1, "_Get") $hListView = GUICtrlCreateListView("Song#|Song Name", 10, 100, 480, 380) _GUICtrlListView_SetColumnWidth(-1, 0, 50) _GUICtrlListView_SetColumnWidth(-1, 1, 430) _GUICtrlListView_AddArray($hListView, $aArray) GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit") GUISetState(True, $hGUI) $iIndex = "" $iCurr_Index= "" $hClient = _TCP_Client_Create("x.x.x.x", 6112) _TCP_RegisterEvent($hClient, $TCP_RECEIVE, "Received") _TCP_RegisterEvent($hClient, $TCP_CONNECT, "Connected") _TCP_RegisterEvent($hClient, $TCP_DISCONNECT, "Disconnected") While 1 Sleep(100) $iIndex = _GUICtrlListView_GetSelectedIndices($hListView, False) ; Check it is a new index to prevent flicker If $iIndex <> $iCurr_Index Then For $i = 0 To 1 GUICtrlSetData($hEdit[$i], $aArray[$iIndex][$i]) Next $iCurr_Index = $iIndex EndIf WEnd Func Connected($hSocket, $iError) If Not $iError Then UpdateStatus("Connected") $sSocket = $hSocket Else UpdateStatus("Could not connect, are you sure server is running?") EndIf EndFunc Func Received($hSocket, $sReceived, $iError) UpdateStatus($sReceived) If $sReceived == "start:" Then _TCP_Send($sSocket, "start:") If $sReceived == "notready:" Then UpdateStatus("Not Ready to retrieve songs") EndIf If $sReceived == "done:" Then $switch = 0 _GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($hListView)) _GUICtrlListView_AddArray($hListView, $aArray) EndIf If $switch = 1 Then If $arraySpot > UBound($aArray)-1 Then $iNewDimension = UBound($aArray)+1 ReDim $aArray[$iNewDimension][2] EndIf $aArray[$arraySpot][1] = $sReceived $aArray[$arraySpot][0] = $arraySpot + 1 $arraySpot = $arraySpot + 1 _TCP_Send($sSocket, "nextSong:") EndIf If $switch = 0 Then If $sReceived == "track:" Then _TCP_Send($sSocket, GUICtrlRead($hEdit[0])) EndIf EndFunc Func Disconnected($hSocket, $iError) UpdateStatus("Connection closed or lost") EndFunc Func _Play() $switch = 0 _TCP_Send($sSocket, "play:") EndFunc Func _Get() $switch = 1 UpdateStatus("Retrieving Song List") _TCP_Send($sSocket, "array:") EndFunc Func _Exit() Exit EndFunc Func UpdateStatus($text) GUICtrlSetData($labelStatus, $text) EndFunc Edited December 4, 2009 by contin
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now