internal void AddToRetryList(AipcConnection conn) { lock (mutex) { retryList.Add(conn); } }
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; } }
private void AddToRoster(AipcConnection connection) { if (connection != null) { lock (mutex) { roster.Add(actualHandleNumber, connection); } } }
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)); }
private AipcConnection Lookup(int handle) { AipcConnection result = null; lock (mutex) { roster.TryGetValue(handle, out result); } return(result); }
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)); }
public AType GetAttribute(int handle, AType attribute) { AipcConnection connection = Lookup(handle); if (connection == null) { return(Utils.ANull()); } return(connection.AipcAttributes.GetAttribute(attribute)); }
public AType Attributes(int handle) { AipcConnection connection = Lookup(handle); if (connection == null) { return(Utils.ANull()); } return(connection.AipcAttributes.Attributes()); }
public AType Open(int handle) { AipcConnection connection = Lookup(handle); if (connection == null) { return(AInteger.Create(-1)); } connection.Open(); return(AInteger.Create(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)); }
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! )); }
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) )); }
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); }
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); }
/// <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; }
/// <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); }
/// <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); }
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)); }
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); }
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(); } } }
/// <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; }
private AipcAttribute(AipcConnection connection) { this.connection = connection; }