예제 #1
0
        /// <summary>
        /// Callback Method that is called each time a packet form the connected socket is received
        /// </summary>
        /// <param name="asyn"></param>
        private void ReceiveCallback(IAsyncResult asyn)
        {
            try
            {
                //Get the current packet
                ReceivedSocketPacket receivedSocketPacket = (ReceivedSocketPacket)asyn.AsyncState;

                //Get the packet socket
                Socket client = receivedSocketPacket.workSocket;

                //Get the number of bytes received
                int bytesRead = receivedSocketPacket.workSocket.EndReceive(asyn);

                if (bytesRead > 0)
                {
                    //Copy the read bytes from the packet buffer into the processing buffer
                    lock (thisLock)
                    {
                        messageBufer.Append(Encoding.ASCII.GetString(receivedSocketPacket.buffer, 0, bytesRead));
                    }

                    //Process the message buffer tois  extract all the received messages
                    ProcessMessageBuffer();

                    StartReceivingPackets();
                }
                else
                {
                    StartReceivingPackets();
                }
            }
            catch (ObjectDisposedException)
            {
                //This is good error (when the socket closes an error that states that the deposed object can't be accesed is triggered)
                //Never foud a clean close solution
            }
            catch (SocketException ex)
            {
                Log.Error(C3NET_COMMUNICATOR_LOG, string.Format("ReceiveCallback : {0}", ex.ToString()));
            }
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        private bool StartReceivingPackets()
        {
            try
            {
                //Create the socket packet and assign a socket to it
                ReceivedSocketPacket receivedSocketPacket = new ReceivedSocketPacket();
                receivedSocketPacket.workSocket = connectedSocket;

                //Init the CallBack method that will wait for received messages
                if (asyncReceiveCallBack == null)
                {
                    asyncReceiveCallBack = new AsyncCallback(ReceiveCallback);
                }

                //Start accepting packets
                if (!WasDisconnected)
                {
                    connectedSocket.BeginReceive(receivedSocketPacket.buffer, 0, ReceivedSocketPacket.BufferSize, SocketFlags.None, asyncReceiveCallBack, receivedSocketPacket);
                }

                //Change the connected flag if the socket is connected to the EndPoint
                WasConnected = true;

                return(true);
            }
            catch (ObjectDisposedException)
            {
                //This is good error (when the socket closes an error that states that the deposed object can't be accesed is triggered)
                //Never foud a clean close solution
            }
            catch (Exception ex)
            {
                //If the socket is already closed it's normal for the StartPacketReceiving to fail
                if (!WasDisconnected)
                {
                    //If there is an error when packet receival method is called the communication will be considered down
                    WasDisconnected = true;
                    waitForConnectionTimer.Stop();
                    Disconnect();

                    var win32Exception = ex as Win32Exception;
                    if (win32Exception == null)
                    {
                        win32Exception = ex.InnerException as Win32Exception;
                    }
                    if (win32Exception != null)
                    {
                        int code = win32Exception.ErrorCode;
                        if (code == 10057)
                        {
                            Log.Error(C3NET_COMMUNICATOR_LOG, "Socket was closed.");
                        }
                        else
                        {
                            Log.Error(C3NET_COMMUNICATOR_LOG, string.Format("Start Receiving Packets Failed: {0}", ex.Message));
                        }
                    }
                    else
                    {
                        Log.Error(C3NET_COMMUNICATOR_LOG, string.Format("Start Receiving Packets Failed: {0}", ex.Message));
                    }
                }
            }
            return(false);
        }