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); }
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(); } } }