Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

How to get response from socket server class to windows form

$
0
0

Hi,

am new beee for .net development. currently am working with socket programming, using some online tuturioal i done with my client server Asynchronous program. Intitally i placed the server logic in separate class..

Private Sub OnDataReceived(ByVal ar As IAsyncResult)
        LogCTIEvent("{WRN}{OS} CTICoreMgr-OnDataReceived:  ", String.Format("Started..."))
        'Monitor.Enter(locObj1)
        Try
            Dim state As StateObject = CType(ar.AsyncState, StateObject)
            Dim client As Socket = state.workSocket
            'Check what side answered
            Dim sDatacenter As String = String.Empty
            Dim currSockPack As SocketPacket = Nothing
            For Each oSocPack As SocketPacket In dicSideSocketPack.Values
                If oSocPack.workSocket.Handle = client.Handle Then
                    currSockPack = oSocPack
                    Dim sLoc As String = oSocPack.Side
                    If sLoc = "M" Then
                        sDatacenter = "M"
                        LogCTIEvent("{WRN}{OS} CTICoreMgr-OnDataReceived:  ", String.Format("Received data from side M"))
                        'sendDoneM.Set()
                    ElseIf sLoc = "W" Then
                        sDatacenter = "W"
                        LogCTIEvent("{WRN}{OS} CTICoreMgr-OnDataReceived:  ", String.Format("Received data from side W"))
                        'sendDoneW.Set()
                    End If
                    Exit For
                End If
            Next
            ' Read data from the remote device.
            Dim bytesRead As Integer
            Try
                bytesRead = client.EndReceive(ar)
            Catch e As SocketException
                Dim sErr As String = e.ErrorCode & " " & e.Message
                Dim iErrorCode As Integer = e.ErrorCode
                If iErrorCode = 10054 Then
                    LogCTIEvent("{WRN}{OS} CTICoreMgr-OnDataReceived:  ", String.Format("The SocketServer in data center " & sDatacenter & " forced to disconnect this socket"))
                    'Check if the other side is alive
                Else
                    WriteToErrorLog("ERROR-: " & e.Message, "OnDataReceived", "CTICoreMgr-SocketMgr.vb")
                End If
            Catch ex As Exception
                Dim sErr As String = ex.Message
                WriteToErrorLog("ERROR-: " & ex.Message, "OnDataReceived", "CTICoreMgr-SocketMgr.vb")
            End Try
            If bytesRead > 0 Then
                ' There might be more data, so store the data received so far.
                state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead))
                Dim content As String = state.sb.ToString()
                If content.IndexOf("</ctimessage>") > -1 Then
                    ' All the data has been read from the client
                    'Process the response
                    Dim oMsg As Message = New Message()
                    oMsg.LoadFromString(content)
                    oMsg.ServerLocation = sDatacenter
                    ProcessResponseMessage(oMsg)
                    WaitForData(currSockPack)
                    LogCTIEvent("{WRN}{OS} CTICoreMgr-OnDataReceived:  ", String.Format("Exiting OnDataReceived..."))
                Else
                    ' Not all data received. Get more.
                    m_asynResult = m_CTISocketClient.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, pfnCallBack, state)
                End If
            End If
        Catch ode As ObjectDisposedException
            WriteToErrorLog("ERROR-ObjectDisposedException: " & ode.Message, "OnDataReceived", "CTICoreMgr-SocketMgr.vb")
            Err.Clear()
        Catch se As SocketException
            WriteToErrorLog("ERROR-SocketException: " & se.Message, "OnDataReceived", "CTICoreMgr-SocketMgr.vb")
            Err.Clear()
        Finally
            'Monitor.Exit(locObj1)
        End Try
    End Sub

and based upon the event i capture the log

   Private Sub ProcessResponseMessage(ByVal poMsg As Message)
        Dim oEventArgs As CTIEventArgs = Nothing
        Dim eEventID As enumCTIEventID
        Dim notify As Boolean = False
        Monitor.Enter(locObj2)
        Try
            Select Case poMsg.MsgType
                Case Message.enumMsgTypes.CONNECT
                    If poMsg.RetCode = Message.enumReturnCodes.CONNECT_SUCCESS Then
                        'Send connect success event to CTIMain
                        eEventID = enumCTIEventID.eOnConnection
                        oEventArgs = New CTIEventArgs(eEventID)
                        LogCTIEvent("{WRN}{OS} CTICoreMgr-SocketMgr::ProcessResponseMessage:  ", String.Format("Received CONNECT-SUCCESS response from CTISocketServer for Extension = " & poMsg.Extension & " and Side = " & poMsg.ServerLocation))
                        notify = True
                    ElseIf poMsg.RetCode = Message.enumReturnCodes.CONNECT_FAILURE Then
                        'Failed connection request or Established connection failed. Automatic reconnect may follow.
                        eEventID = enumCTIEventID.eOnConnectionFailure
                        oEventArgs = New CTIEventArgs(eEventID)
                        LogCTIEvent("{WRN}{OS} CTICoreMgr-SocketMgr::ProcessResponseMessage:  ", String.Format("Received CONNECT-FAILURE response from CTISocketServer on Side = " & " and Side = " & poMsg.ServerLocation))
                        notify = True
                    ElseIf poMsg.RetCode = Message.enumReturnCodes.LOGIN_SUCCESS Then
                        'Send login success event to CTIMain
                        eEventID = enumCTIEventID.eAgentStateChange
                        oEventArgs = New CTIEventArgs(eEventID)
                        LogCTIEvent("{WRN}{OS} CTICoreMgr-SocketMgr::ProcessResponseMessage:  ", String.Format("Received LOGIN-SUCCESS message from CTISocketServer for Extension = " & poMsg.Extension & " and Side = " & poMsg.ServerLocation))
                        oEventArgs.Add(KEY_AGENT_EXTENSION, poMsg.Extension)
                        oEventArgs.Add(KEY_AGENT_STATE, CType(CInt(poMsg.AgentState), enumCTIAgentState))
                        oEventArgs.Add(KEY_AGENT_ID, poMsg.AgentID)
                        notify = True
                    ElseIf poMsg.RetCode = Message.enumReturnCodes.LOGIN_FAILURE Then
                        'Send login Failure event to CTIMain
                        ' eEventID = enumCTIEventID.eOnConnectionFailure
                        eEventID = enumCTIEventID.eAgentStateChange
                        oEventArgs = New CTIEventArgs(eEventID)
                        LogCTIEvent("{WRN}{OS} CTICoreMgr-SocketMgr::ProcessResponseMessage:  ", String.Format("Received LOGIN-FAILURE message from CTISocketServer for Extension = " & poMsg.Extension & " and Side = " & poMsg.ServerLocation & ", AgentState = " & poMsg.AgentState))
                        oEventArgs.Add(KEY_AGENT_EXTENSION, poMsg.Extension)
                        oEventArgs.Add(KEY_AGENT_STATE, CType(CInt(poMsg.AgentState), enumCTIAgentState))
                        oEventArgs.Add(KEY_AGENT_ID, poMsg.AgentID)
                        notify = True
                    ElseIf poMsg.RetCode = Message.enumReturnCodes.LOGOUT Then
                        'Send login Failure event to CTIMain
                        ' eEventID = enumCTIEventID.eOnConnectionFailure
                        eEventID = enumCTIEventID.eAgentStateChange
                        oEventArgs = New CTIEventArgs(eEventID)
                        LogCTIEvent("{WRN}{OS} CTICoreMgr-SocketMgr::ProcessResponseMessage:  ", String.Format("Received LOGOUT message from CTISocketServer for Extension = " & poMsg.Extension & ", Side = " & poMsg.ServerLocation & ", AgentState = " & poMsg.AgentState))
                        oEventArgs.Add(KEY_AGENT_EXTENSION, poMsg.Extension)
                        oEventArgs.Add(KEY_AGENT_STATE, CType(CInt(poMsg.AgentState), enumCTIAgentState))
                        oEventArgs.Add(KEY_AGENT_ID, poMsg.AgentID)
                        notify = True
                    End If
                Case Message.enumMsgTypes.DISCONNECT
                    If poMsg.RetCode = Message.enumReturnCodes.DISCONNECT_SUCCESS Then
                        'Send disconnect success event to CTIMain
                        eEventID = enumCTIEventID.eOnConnectionClosed
                        oEventArgs = New CTIEventArgs(eEventID)
                        LogCTIEvent("{WRN}{OS} CTICoreMgr-SocketMgr::ProcessResponseMessage:  ", String.Format("Received DISCONNECT-SUCCESS message from CTISocketServer on side " & " and Side = " & poMsg.ServerLocation))
                        notify = True
                    ElseIf poMsg.RetCode = Message.enumReturnCodes.DISCONNECT_FAILURE Then
                        'Failed disconnect request.
                        'ignore, just log the error
                        LogCTIEvent("{WRN}{OS} CTICoreMgr-SocketMgr::ProcessResponseMessage:  ", String.Format("Received DISCONNECT-FAILURE response from CTISocketServer on side " & " and Side = " & poMsg.ServerLocation & ". Ignoring..."))
                        notify = False
                    End If
                Case Message.enumMsgTypes.HEARTBEAT
                    LogCTIEvent("{WRN}{OS} CTICoreMgr-SocketMgr::ProcessResponseMessage:  ", String.Format("Received HEARTBEAT response from CTISocketServer on side " & " and Side = " & poMsg.ServerLocation))
                    eEventID = enumCTIEventID.eAgentStateChange
                    oEventArgs = New CTIEventArgs(eEventID)
                    oEventArgs.Add(KEY_PG_STATUS, CTI_OK.ToString())
                    oEventArgs.Add(KEY_AGENT_EXTENSION, poMsg.Extension)
                    bConnected = True
                    'notify = True
                Case Message.enumMsgTypes.CTIDATA
                    Dim sData As String = ""
                    If poMsg.RetCode = Message.enumReturnCodes.DATA_RECEIVED Then
                        eEventID = enumCTIEventID.eUpdateCurrentCallStatus
                        oEventArgs = New CTIEventArgs(eEventID)
                        sData = poMsg.CTIData
                        If Not sData.Length = 0 Then
                            oEventArgs.Add(KEY_CURRENT_CALL_DATA, sData)
                            ' oEventArgs.Add(KEY_CURRENT_CALL_CALLID, poMsg.CallID)
                            LogCTIEvent("{WRN}{OS} CTICoreMgr-SocketMgr::ProcessResponseMessage:  ", String.Format("ctidata received from  side " & poMsg.ServerLocation))
                            notify = True
                        Else
                            LogCTIEvent("{WRN}{OS} CTICoreMgr-SocketMgr::ProcessResponseMessage:  ", String.Format("ctidata node in socket message is empty"))
                        End If
                    End If
                Case Message.enumMsgTypes.ENDCALL
                    If poMsg.RetCode = Message.enumReturnCodes.CONNECTION_CLEARED Then
                        eEventID = enumCTIEventID.eCallEndEvent
                        oEventArgs = New CTIEventArgs(eEventID)
                        oEventArgs.Add(KEY_AGENT_ID, poMsg.AgentID)
                        oEventArgs.Add(KEY_AGENT_EXTENSION, poMsg.Extension)
                        notify = True
                    End If
            End Select
            If notify Then
                RaiseEvent PassCTIEvent(oEventArgs)
                oEventArgs = Nothing
            End If
        Catch ex As Exception
            WriteToErrorLog("ERROR: " & ex.Message, "ProcessResponseMessage", "CTICoreMgr-SocketMgr.vb")
            Err.Clear()
            notify = False
        Finally
            Monitor.Exit(locObj2)
        End Try
    End Sub

now i want to pass this response to my windows form UI when i click on "connect button"?

Kindly suggest me how to get this?


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>