Exemplo n.º 1
0
        public void First_WaitForData()
        {
            try
            {
                if (m_pfnCallBack == null)
                {
                    m_pfnCallBack = new AsyncCallback(OnDataReceived);
                }
                Communications.SocketPacket theSocPkt = new Communications.SocketPacket();
                theSocPkt.m_currentSocket = m_Globals.m_communications.m_clientSocket;

                // Start listening to the data asynchronously.
                int offset = 0;
                m_result = m_Globals.m_communications.m_clientSocket.BeginReceive(
                    theSocPkt.dataBuffer,
                    offset,
                    theSocPkt.dataBuffer.Length,
                    SocketFlags.None,
                    m_pfnCallBack,
                    theSocPkt
                    );
            }
            catch (SocketException se)
            {
                MessageBox.Show(se.Message);
            }
        }
Exemplo n.º 2
0
 public void WaitForData(System.Net.Sockets.Socket soc)
 {
     try
     {
         if (pfnWorkerCallBack == null)
         {
             // Specify the call back function which is to be
             // invoked when there is any write activity by the
             // connected client
             pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
         }
         Communications.SocketPacket theSocPkt = new Communications.SocketPacket();
         theSocPkt.m_currentSocket = soc;
         // Start receiving any data written by the connected client
         // asynchronously
         int offset = 0;
         soc.BeginReceive(
             theSocPkt.dataBuffer,
             offset,
             theSocPkt.dataBuffer.Length,
             SocketFlags.None,
             pfnWorkerCallBack,
             theSocPkt
             );
     }
     catch (SocketException se)
     {
         MessageBox.Show(se.Message);
     }
 }
Exemplo n.º 3
0
        public void OnDataReceived(IAsyncResult asyn)
        {
            try
            {
                Communications.SocketPacket socketData = (Communications.SocketPacket)asyn.AsyncState;

                int iRx = 0;
                // Complete the BeginReceive() asynchronous call by EndReceive() method
                // which will return the number of characters written to the stream
                // by the client
                iRx = socketData.m_currentSocket.EndReceive(asyn);
                char[] chars          = new char[iRx + 1];
                System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
                int           charLen = d.GetChars(socketData.dataBuffer, 0, iRx, chars, 0);
                System.String szData  = new System.String(chars);

                EndPoint remoteEndPoint = socketData.m_currentSocket.RemoteEndPoint;

                if (LastEndPointStarted == null)
                {
                    LastEndPointStarted = remoteEndPoint;
                }

                if (LastEndPointStarted == remoteEndPoint)
                {   // My attempts to defeat concurrent thread write collisions.  I was seeing 2 end points trying to write
                    // into the socket and that concatenated the bits at the bit level, so no byte received was even remotely
                    // correct.  I put in this equality test to force the last end point we started working with to be the
                    // winner.  The loser's message is ignored.
                    ReceivedMsg.AppendText(szData);

                    //when we are sure we received the entire message
                    //pass the Object received into parameter
                    //after removing the flag indicating the end of message
                    String EOMDelimiter = m_Globals.m_communications.EOMDelimiter;
                    if (ReceivedMsg.GetText().Contains(EOMDelimiter))
                    {
                        LastEndPointStarted = null;
                        String SanitizedMessage = Regex.Replace(ReceivedMsg.GetText(), EOMDelimiter, "");
                        ProcessCommMessage(SanitizedMessage, socketData.m_currentSocket.RemoteEndPoint);
                    }
                }

                // Continue the waiting for data on the Socket
                WaitForData(socketData.m_currentSocket);
            }
            catch (ObjectDisposedException)
            {
                System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
            }
            catch (SocketException se)
            {
                MessageBox.Show(se.Message);
            }
        }
Exemplo n.º 4
0
        public void OnDataReceived(IAsyncResult asyn)
        {
            try
            {
                Communications.SocketPacket theSockId = (Communications.SocketPacket)asyn.AsyncState;
                int    iRx            = theSockId.m_currentSocket.EndReceive(asyn);
                char[] chars          = new char[iRx + 1];
                System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
                int           charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
                System.String szData  = new System.String(chars);

                ReceivedMsg.AppendText(szData);

                // If we finished receiving the response (EventData object) from the Broker
                // then we will recover it.
                String EOMDelimiter = m_Globals.m_communications.EOMDelimiter;
                if (ReceivedMsg.GetText().Contains(EOMDelimiter))
                {
                    // Remove the flag "ENDOFMESSAGE"
                    String SanitizedMessage = Regex.Replace(ReceivedMsg.GetText(), EOMDelimiter, "");
                    AppendToRichTextBoxHistoryResponses(SanitizedMessage);
                    ProcessCommMessage(SanitizedMessage);


                    ReceivedMsg.SetText("");
                }

                First_WaitForData();
            }
            catch (ObjectDisposedException)
            {
                System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
            }
            catch (SocketException se)
            {
                MessageBox.Show(se.Message);
            }
        }