Example #1
0
        //protected Byte[] AsyncWaitForData()
        //{
        //    TCPReceiver tcpReceiver = new TCPReceiver(this.receiveBufferSize);

        //    SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs();
        //    socketArgs.UserToken = tcpReceiver;
        //    socketArgs.SetBuffer(tcpReceiver.ReceiveBuffer, 0, this.receiveBufferSize);
        //    socketArgs.Completed += new EventHandler<SocketAsyncEventArgs>(OnIOComplete);

        //    bool isSyncSend = tcpClientSocket.ReceiveAsync(socketArgs);

        //    tcpReceiver.WaitHandle = new AutoResetEvent(false);
        //    if (isSyncSend)
        //        OnReceive(socketArgs);

        //    if (!tcpReceiver.WaitHandle.WaitOne(new TimeSpan(this.receiveTimeout)))
        //        throw new USSDTcpSendTimeoutException("Socket receive timeout");

        //    return tcpReceiver.ReceiveData;
        //}

        //protected void OnIOComplete(object sender, SocketAsyncEventArgs e)
        //{
        //    switch (e.LastOperation)
        //    {
        //        case SocketAsyncOperation.Receive:
        //            OnReceive(e);
        //            break;
        //        case SocketAsyncOperation.Send:
        //            OnSend(e);
        //            break;
        //        default:
        //            throw new ArgumentException("The last operation completed on the socket was not a receive or send");
        //    }
        //}

        protected void OnReceiveComplete(object sender, SocketAsyncEventArgs e)
        {
            TCPReceiver tcpReceiver = e.UserToken as TCPReceiver;

            if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
            {
                tcpReceiver.AddReceiveData(e.Buffer, e.BytesTransferred);
                //if (tcpReceiver.ReceiveData.Contains(this.packetTerminator))
                //{
                // remove the terminator
                //tcpReceiver.ReceiveData = tcpReceiver.ReceiveData.Where(b => b != this.packetTerminator).ToArray();
                if (tcpReceiver.WaitHandle != null)
                {
                    tcpReceiver.WaitHandle.Set();
                }
                //}
            }
            else
            {
                Boolean autoReconnectWhenConnectionIsLost = AutoReconnectWhenConnectionIsLost;
                OnServerDisconnected(tcpClientSocket, ref autoReconnectWhenConnectionIsLost);
                AutoReconnectWhenConnectionIsLost = autoReconnectWhenConnectionIsLost;
                ConnectToServer(true);
            }
        }
Example #2
0
        protected void OnReceiveDataFromServer(IAsyncResult asyncResult)
        {
            if (listenerState != ListenerStates.Running)
            {
                return;
            }

            TCPReceiver tcpReceiver = asyncResult.AsyncState as TCPReceiver;
            SocketError errorCode;
            int         bytesReceived = tcpClientSocket.EndReceive(asyncResult, out errorCode);

            byte[] tmp = new byte[bytesReceived];
            tmp = tcpReceiver.ReceiveBuffer;
            string payload = Encoding.ASCII.GetString(tmp);

            //LogManager.LogStatus(" ... " + payload);

            if (!tcpClientSocket.Connected)
            {
                listenerState = ListenerStates.Stopped;
                Boolean autoReconnectWhenConnectionIsLost = AutoReconnectWhenConnectionIsLost;
                OnServerDisconnected(tcpClientSocket, ref autoReconnectWhenConnectionIsLost);
                AutoReconnectWhenConnectionIsLost = autoReconnectWhenConnectionIsLost;
                ConnectToServer(true);
                return;
            }

            if (bytesReceived != 0)
            {
                byte[] dataChunk    = new byte[] { };
                byte[] dataOverflow = new byte[] { };

                tcpReceiver.AddReceiveData(tmp, bytesReceived);

                string recev = Encoding.ASCII.GetString(tcpReceiver.ReceiveData);

                while (tcpReceiver.ReceiveData.Contains((byte)255))
                {
                    for (int x = 0; x < tcpReceiver.ReceiveData.Length; x++)
                    {
                        if (tcpReceiver.ReceiveData[x] == (byte)255)
                        {
                            dataChunk = new byte[x];
                            Buffer.BlockCopy(tcpReceiver.ReceiveData, 0, dataChunk, 0, dataChunk.Length);
                            dataOverflow = new byte[tcpReceiver.ReceiveData.Length - dataChunk.Length - 1];
                            Buffer.BlockCopy(tcpReceiver.ReceiveData, x + 1, dataOverflow, 0, dataOverflow.Length);

                            tcpReceiver.ReceiveData = dataOverflow;

                            if (dataChunk.Length > 0)
                            {
                                if (dataChunk[0] == 60)
                                {
                                    string tempDAta = Encoding.ASCII.GetString(dataChunk);
                                    OnDataReceived(dataChunk);
                                }
                                else
                                {
                                    Console.WriteLine("*** PROBLEM WITH PDU ****");
                                }
                            }

                            break;
                        }
                    }
                }


                //tcpReceiver.AddReceiveData(tcpReceivere.Buffer, e.BytesTransferred);
                //if (tcpReceiver.ReceiveData.Contains((byte)255))
                //{
                //// remove the terminator
                //int terminatorIndex = 0;
                //Byte[] dataChunk = new Byte[] { };
                //Byte[] dataOverflow = new Byte[] { };
                //Boolean completePacket = false;
                //while (terminatorIndex <= tcpReceiver.ReceiveData.Length)
                //{

                //    if (tcpReceiver.ReceiveData[terminatorIndex].Equals(this.packetTerminator))
                //    {
                //        completePacket = true;

                //        Byte[] copyBuffer = new Byte[tcpReceiver.ReceiveData.Length - dataChunk.Length-1];
                //        Buffer.BlockCopy(tcpReceiver.ReceiveData, terminatorIndex, copyBuffer, 0, tcpReceiver.ReceiveData.Length - dataChunk.Length-1);
                //        dataOverflow = copyBuffer;
                //        break;
                //    }
                //    else
                //    {
                //        Byte[] copyBuffer = new Byte[dataChunk.Length + 1];
                //        Buffer.BlockCopy(tcpReceiver.ReceiveData, 0, copyBuffer, 0, copyBuffer.Length);
                //        dataChunk = copyBuffer;

                //        copyBuffer = new Byte[tcpReceiver.ReceiveData.Length - dataChunk.Length];
                //        Buffer.BlockCopy(tcpReceiver.ReceiveData, terminatorIndex, copyBuffer, 0, tcpReceiver.ReceiveData.Length - dataChunk.Length);
                //        dataOverflow = copyBuffer;
                //    }

                //    terminatorIndex++;
                //}

                //if (completePacket)
                //{
                //    Byte[] copyBuffer = new Byte[Math.Max(tcpReceiver.ReceiveData.Length - dataChunk.Length, 0)];
                //    if (copyBuffer.Length > 0)
                //        Buffer.BlockCopy(tcpReceiver.ReceiveData, terminatorIndex, copyBuffer, 0, Math.Max(tcpReceiver.ReceiveData.Length - dataChunk.Length, 0));

                //    OnDataReceived(dataChunk);

                //    tcpReceiver.ReceiveData = copyBuffer;
                //    tcpReceiver.ReceiveData = dataOverflow.Where(b => b != (byte)255).ToArray();
                //}
                //// Remove thye terminator
                //tcpReceiver.ReceiveData = tcpReceiver.ReceiveData.Where(b => b != (byte)255).ToArray();
                ////OnDataReceived(tcpReceiver.ReceiveData);
                ////tcpReceiver.Clear();

                if (tcpReceiver.WaitHandle != null)
                {
                    tcpReceiver.WaitHandle.Set();
                }
                //}
            }

            //if (listenerState == ListenerStates.Running)
            //{
            //    if (listenerState != ListenerStates.Running) return;

            //    if (tcpReceiver.ReceiveData.Length != 0 && tcpReceiver.ReceiveBuffer.Contains(this.packetTerminator))
            //        OnDataReceived(tcpReceiver.ReceiveData);
            //    tcpClientSocket.BeginReceive(tcpReceiver.ReceiveBuffer, 0, receiveBufferSize, SocketFlags.None, new AsyncCallback(OnReceiveDataFromServer), tcpReceiver);
            //}
        }