Ejemplo n.º 1
0
 public void Send(byte[] message)
 {
     Trace.WriteLine("message: " + ByteArrayHelper.ByteArrayToTestString(message));
     Trace.WriteLine("sendmsg: " + ByteArrayHelper.ByteArrayToTestString(SendMessage));
     Assert.IsTrue(message.SequenceEqual(SendMessage));
     int length = ReceiveMessage.Length-6;
     byte[] response = new byte[length];
     Array.Copy(ReceiveMessage, 6, response, 0, length);
     var callbackArgs = new AmsSocketResponseArgs()
     {
         Response = response
     };
     OnReadCallBack(this, callbackArgs);
 }
Ejemplo n.º 2
0
        public void Listen()
        {
            if (IsConnected)
            {
                try
                {
                    //First wait for the Ams header (starts new thread)
                    byte[] amsheader = new byte[AmsHeaderHelper.AmsTcpHeaderSize];

                    ListenForHeader(amsheader, (buffer, usertoken) =>
                    { 
                        //If a ams header is received, then read the rest (this is the new thread)
                        try
                        {
                            byte[] response = GetAmsMessage(buffer);

#if DEBUG_AMS
                            Debug.WriteLine("Received bytes: " +
                                    ByteArrayHelper.ByteArrayToTestString(buffer) + ',' +
                                    ByteArrayHelper.ByteArrayToTestString(response));
#endif

                            SynchronizationContext syncContext = usertoken != null ? usertoken : synchronizationContext;

                            var callbackArgs = new AmsSocketResponseArgs() { 
                                Response = response, 
                                Context = syncContext };
                            OnReadCallBack(this, callbackArgs);
                            Listen();
                        }
                        catch (Exception ex)
                        {
                            var callbackArgs = new AmsSocketResponseArgs() { Error = ex };
                            OnReadCallBack(this, callbackArgs);
                        }
                    });                                          
                }
                catch (Exception ex)
                {
                    if (!Object.ReferenceEquals(ex.GetType(), typeof(ObjectDisposedException))) throw;
                }
            }
        }
Ejemplo n.º 3
0
        private void Listen()
        {
            if ((socket != null) && (socket.Connected))
            {
                try
                {
                    //First wait for the Ams header (starts new thread)
                    byte[] amsheader = new byte[AmsHeaderHelper.AmsTcpHeaderSize];
                   
                    SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                    args.SetBuffer(amsheader, 0, AmsHeaderHelper.AmsTcpHeaderSize);
                    args.UserToken = synchronizationContext;
                    args.Completed += (sender, e) => { 
                        //If a ams header is received, then read the rest (this is the new thread)
                        try
                        {
                            byte[] response = GetAmsMessage(e.Buffer);

                            Debug.WriteLine("Received bytes: " +
                                    ByteArrayHelper.ByteArrayToTestString(e.Buffer) + ',' +
                                    ByteArrayHelper.ByteArrayToTestString(response)); 
                            
                            var callbackArgs = new AmsSocketResponseArgs() { 
                                Response = response, 
                                Context = e.UserToken as SynchronizationContext };
                            OnReadCallBack(this, callbackArgs);
                            Listen();
                        }
                        catch (Exception ex)
                        {
                            var callbackArgs = new AmsSocketResponseArgs() { Error = ex };
                            OnReadCallBack(this, callbackArgs);
                        }

                    };
                    if (!socket.ReceiveAsync(args))
                    {
                        //If finished in same thread
                        byte[] response = GetAmsMessage(amsheader);
                        var callbackArgs = new AmsSocketResponseArgs()
                        {
                            Response = response,
                            Context = synchronizationContext
                        };
                        OnReadCallBack(this, callbackArgs);
                        Listen();
                    };
                     
                }
                catch (Exception ex)
                {
                    if (!Object.ReferenceEquals(ex.GetType(), typeof(ObjectDisposedException))) throw;
                }
            }
        }