예제 #1
0
 internal void AddToRetryList(AipcConnection conn)
 {
     lock (mutex)
     {
         retryList.Add(conn);
     }
 }
예제 #2
0
 internal void AddToRetryList(AipcConnection conn)
 {
     lock (mutex)
     {
         retryList.Add(conn);
     }
 }
예제 #3
0
        private void CallbackBySocketException(AipcConnection connection, SocketException exception, bool isRead)
        {
            int handle = connection.ConnectionAttributes.HandleNumber;

            switch (exception.SocketErrorCode)
            {
            case SocketError.TimedOut:
                connection.MakeCallback("timeout", ASymbol.Create("Timeout"));
                break;

            case SocketError.Interrupted:
                connection.MakeCallback("interrupt", ASymbol.Create("Interrupted"));
                break;

            case SocketError.NoBufferSpaceAvailable:
                connection.MakeCallback(isRead ? "buffread" : "buffwrite",
                                        ASymbol.Create(String.Concat(isRead ? "Read " : "Write ", "buffer is full")));
                this.Close(connection.ConnectionAttributes.HandleNumber);
                break;

            case SocketError.NotInitialized:
            case SocketError.NotConnected:
                this.Close(connection.ConnectionAttributes.HandleNumber);
                break;

            case SocketError.ConnectionReset:
            default:
                connection.MakeCallback("reset", ASymbol.Create("unknown State"));
                this.Close(connection.ConnectionAttributes.HandleNumber);
                break;
            }
        }
예제 #4
0
 private void AddToRoster(AipcConnection connection)
 {
     if (connection != null)
     {
         lock (mutex)
         {
             roster.Add(actualHandleNumber, connection);
         }
     }
 }
예제 #5
0
        public void InitFromListener(ConnectionAttribute connectionAttribute, AipcAttribute aipcAttributes, Socket socket)
        {
            ConnectionAttribute attribute = connectionAttribute.Clone();

            attribute.IsListener = false;
            AipcConnection connection = Create(attribute, aipcAttributes, socket);

            connection.AipcAttributes.Listener = connectionAttribute.HandleNumber;
            connection.MakeCallback("connected", AInteger.Create(connection.AipcAttributes.Listener));
        }
예제 #6
0
        private AipcConnection Lookup(int handle)
        {
            AipcConnection result = null;

            lock (mutex)
            {
                roster.TryGetValue(handle, out result);
            }

            return(result);
        }
예제 #7
0
        public AType SetAttribute(int handle, AType attribute, AType value)
        {
            AipcConnection connection = Lookup(handle);

            if (connection == null)
            {
                return(AInteger.Create(-1));
            }

            return(connection.AipcAttributes.SetAttribute(attribute, value));
        }
예제 #8
0
        public AType GetAttribute(int handle, AType attribute)
        {
            AipcConnection connection = Lookup(handle);

            if (connection == null)
            {
                return(Utils.ANull());
            }

            return(connection.AipcAttributes.GetAttribute(attribute));
        }
예제 #9
0
        public AType Attributes(int handle)
        {
            AipcConnection connection = Lookup(handle);

            if (connection == null)
            {
                return(Utils.ANull());
            }

            return(connection.AipcAttributes.Attributes());
        }
예제 #10
0
        public AType Open(int handle)
        {
            AipcConnection connection = Lookup(handle);

            if (connection == null)
            {
                return(AInteger.Create(-1));
            }

            connection.Open();
            return(AInteger.Create(0));
        }
예제 #11
0
        public AType Destroy(int handle)
        {
            AipcConnection connection = Lookup(handle);

            if (connection == null)
            {
                return(AInteger.Create(-1));
            }

            RemoveFromRoster(handle);
            connection.Destroy();
            return(AInteger.Create(0));
        }
예제 #12
0
        public AType ReadQueueStatus(int handle)
        {
            AipcConnection connection = Lookup(handle);

            if (connection == null)
            {
                return(Utils.ANull());
            }

            return(AArray.Create(ATypes.AInteger,
                                 AInteger.Create(connection.Socket.Poll(10, SelectMode.SelectRead) ? 1 : 0),
                                 AInteger.Create(0)  // the acutal implementation reads the whole message!
                                 ));
        }
예제 #13
0
        public AType WriteQueueStatus(int handle)
        {
            AipcConnection connection = Lookup(handle);

            if (connection == null)
            {
                return(Utils.ANull());
            }

            return(AArray.Create(ATypes.AInteger,
                                 AInteger.Create(connection.WriteBufferContentSize),
                                 AInteger.Create(connection.PartialSent ? 1 : 0)
                                 ));
        }
예제 #14
0
        public AType Send(int handle, AType message)
        {
            AipcConnection connection = Lookup(handle);
            AType          result;

            if (connection == null || !connection.isOpen)
            {
                return(AInteger.Create(-1));
            }

            result = connection.Send(message);

            return(result);
        }
예제 #15
0
        public AType WhatIs(int handle)
        {
            AType          result     = AArray.Create(ATypes.ASymbol);
            AipcConnection connection = Lookup(handle);

            if (connection == null)
            {
                result.Add(ASymbol.Create(""));
                result.Add(ASymbol.Create(""));
            }
            else
            {
                result.Add(ASymbol.Create(connection.ConnectionAttributes.IsListener ? "listener" : "connector"));
                result.Add(connection.ConnectionAttributes.Protocol);
            }

            return(result);
        }
예제 #16
0
        /// <summary>
        /// Creates a new <see cref="AipcAttrbiute"/> with default values.
        /// </summary>
        /// <param name="connection">Connection to set on the new <see cref="AipcAttribute"/>.</param>
        /// <returns>Returns the new <see cref="AipcAttriute"/>  with default values.</returns>
        public static AipcAttribute Create(AipcConnection connection)
        {
            AipcAttribute attributes = new AipcAttribute(connection)
            {
                noDelay = false,
                writePause = false,
                readPause = false,
                readPriority = 0,
                writePriority = 0,
                retry = false,
                clientData = Utils.ANull(),
                debug = false,
                burstMode = false,
                listener = 0,
                readBufsize = 32768,
                writeBufSize = 32768
            };

            return attributes;
        }
예제 #17
0
        /// <summary>
        /// Creates a new <see cref="AipcAttrbiute"/> with default values.
        /// </summary>
        /// <param name="connection">Connection to set on the new <see cref="AipcAttribute"/>.</param>
        /// <returns>Returns the new <see cref="AipcAttriute"/>  with default values.</returns>
        public static AipcAttribute Create(AipcConnection connection)
        {
            AipcAttribute attributes = new AipcAttribute(connection)
            {
                noDelay       = false,
                writePause    = false,
                readPause     = false,
                readPriority  = 0,
                writePriority = 0,
                retry         = false,
                clientData    = Utils.ANull(),
                debug         = false,
                burstMode     = false,
                listener      = 0,
                readBufsize   = 32768,
                writeBufSize  = 32768
            };

            return(attributes);
        }
예제 #18
0
        /// <summary>
        /// Create a new <see cref="AipcAttribute"/> from the existing one.
        /// </summary>
        /// <param name="connection">Connection to set on the new <see cref="AipcAttribute"/>.</param>
        /// <returns>Returns the new <see cref="AipcAttriute"/>.</returns>
        public AipcAttribute CreateNew(AipcConnection connection)
        {
            AipcAttribute attributes = new AipcAttribute(connection)
            {
                noDelay       = this.noDelay,
                writePause    = this.writePause,
                readPause     = this.readPause,
                readPriority  = this.readPriority,
                writePriority = this.writePriority,
                retry         = this.retry,
                clientData    = this.clientData,
                debug         = this.debug,
                burstMode     = this.burstMode,
                listener      = this.listener,
                readBufsize   = this.readBufsize,
                writeBufSize  = this.writeBufSize
            };

            return(attributes);
        }
예제 #19
0
        public AType Close(int handle)
        {
            AipcConnection connection = Lookup(handle);

            if (connection == null)
            {
                return(AInteger.Create(-1));
            }

            if (!connection.ConnectionAttributes.IsListener && connection.AipcAttributes.Listener == 0)
            {
                connection.Reset();
            }
            else
            {
                connection.Close();
            }

            return(AInteger.Create(0));
        }
예제 #20
0
        public AType SyncRead(int handle, AType timeout)
        {
            AipcConnection connection = Lookup(handle);
            AType          result;

            if (connection == null)
            {
                return(Utils.ANull());
            }

            try
            {
                result = connection.SyncRead(timeout);
            }
            catch (NotImplementedException)
            {
                Console.WriteLine("This protocol has no SyncRead");
                result = Utils.ANull();
            }

            return(result);
        }
예제 #21
0
        private void NetworkLoop()
        {
            while (true)
            {
                Thread.Sleep(1);

                List <AipcConnection> connectionList = new List <AipcConnection>();
                List <Socket>         readList       = new List <Socket>();
                List <Socket>         writeList      = new List <Socket>();
                List <Socket>         errorList      = new List <Socket>();

                Dictionary <int, AipcConnection> rosterCopy;

                lock (mutex)
                {
                    rosterCopy = new Dictionary <int, AipcConnection>(roster);
                }

                //collect connections without readpause and writepause?!

                foreach (var item in rosterCopy)
                {
                    if (!item.Value.ConnectionAttributes.IsListener && item.Value.Socket != null)
                    {
                        connectionList.Add(item.Value);
                        readList.Add(item.Value.Socket);

                        if (item.Value.WriteBufferContentSize != 0)
                        {
                            writeList.Add(item.Value.Socket);
                        }
                    }
                }

                if (readList.Count != 0 || writeList.Count != 0)
                {
                    try
                    {
                        Socket.Select(readList, writeList, errorList, 100);
                    }
                    catch (SocketException)
                    {
                        continue;
                    }
                }

                foreach (Socket socket in readList)
                {
                    AipcConnection connection =
                        connectionList.Where <AipcConnection>(conn => conn.Socket == socket).FirstOrDefault <AipcConnection>();

                    if (connection == null || !connection.isOpen)
                    {
                        continue;
                    }

                    try
                    {
                        AType message = connection.Read();
                    }
                    catch (ADAPException)
                    {
                        this.Close(connection.ConnectionAttributes.HandleNumber);
                        connection.MakeCallback("reset", ASymbol.Create("readImport"));
                    }
                    catch (SocketException exception)
                    {
                        writeList.Remove(connection.Socket); // this should only happen at unknown state but...
                        CallbackBySocketException(connection, exception, true);
                    }
                    catch (ObjectDisposedException)
                    {
                        this.Close(connection.ConnectionAttributes.HandleNumber);
                    }
                    catch (NullReferenceException)
                    {
                        this.Close(connection.ConnectionAttributes.HandleNumber);
                    }
                }

                foreach (Socket socket in writeList)
                {
                    AipcConnection connection =
                        connectionList.Where <AipcConnection>(conn => conn.Socket == socket).FirstOrDefault <AipcConnection>();

                    if (connection == null || !connection.isOpen)
                    {
                        continue;
                    }

                    try
                    {
                        connection.Send();
                    }
                    catch (SocketException exception)
                    {
                        CallbackBySocketException(connection, exception, false);
                    }
                    catch (ObjectDisposedException)
                    {
                        this.Close(connection.ConnectionAttributes.HandleNumber);
                    }
                    catch (NullReferenceException)
                    {
                        this.Close(connection.ConnectionAttributes.HandleNumber);
                    }
                }

                HashSet <AipcConnection> actualRetryList;

                lock (mutex)
                {
                    actualRetryList = retryList;
                    retryList       = new HashSet <AipcConnection>();
                }

                foreach (AipcConnection item in actualRetryList)
                {
                    item.Open();
                }
            }
        }
예제 #22
0
 private void AddToRoster(AipcConnection connection)
 {
     if (connection != null)
     {
         lock (mutex)
         {
             roster.Add(actualHandleNumber, connection);
         }
     }
 }
예제 #23
0
        private void CallbackBySocketException(AipcConnection connection, SocketException exception, bool isRead)
        {
            int handle = connection.ConnectionAttributes.HandleNumber;

            switch (exception.SocketErrorCode)
            {
                case SocketError.TimedOut:
                    connection.MakeCallback("timeout", ASymbol.Create("Timeout"));
                    break;
                case SocketError.Interrupted:
                    connection.MakeCallback("interrupt", ASymbol.Create("Interrupted"));
                    break;
                case SocketError.NoBufferSpaceAvailable:
                    connection.MakeCallback(isRead ? "buffread" : "buffwrite",
                                            ASymbol.Create(String.Concat(isRead ? "Read " : "Write ", "buffer is full")));
                    this.Close(connection.ConnectionAttributes.HandleNumber);
                    break;
                case SocketError.NotInitialized:
                case SocketError.NotConnected:
                    this.Close(connection.ConnectionAttributes.HandleNumber);
                    break;
                case SocketError.ConnectionReset:
                default:
                    connection.MakeCallback("reset", ASymbol.Create("unknown State"));
                    this.Close(connection.ConnectionAttributes.HandleNumber);
                    break;
            }
        }
예제 #24
0
        /// <summary>
        /// Create a new <see cref="AipcAttribute"/> from the existing one.
        /// </summary>
        /// <param name="connection">Connection to set on the new <see cref="AipcAttribute"/>.</param>
        /// <returns>Returns the new <see cref="AipcAttriute"/>.</returns>
        public AipcAttribute CreateNew(AipcConnection connection)
        {
            AipcAttribute attributes = new AipcAttribute(connection)
            {
                noDelay = this.noDelay,
                writePause = this.writePause,
                readPause = this.readPause,
                readPriority = this.readPriority,
                writePriority = this.writePriority,
                retry = this.retry,
                clientData = this.clientData,
                debug = this.debug,
                burstMode = this.burstMode,
                listener = this.listener,
                readBufsize = this.readBufsize,
                writeBufSize = this.writeBufSize
            };

            return attributes;
        }
예제 #25
0
 private AipcAttribute(AipcConnection connection)
 {
     this.connection = connection;
 }
예제 #26
0
 private AipcAttribute(AipcConnection connection)
 {
     this.connection = connection;
 }