/// <summary> /// Used to establish a TCP session with a remote host (connection information is sent to the remote host). /// Transmits the local receiver port to the remote host. /// </summary> /// <param name="remoteIpAddress"></param> /// <param name="remoteHandshakePort"></param> /// <param name="localSessionData"></param> public void TransmitSessionInitialization(string remoteIpAddress, int remoteHandshakePort, string remoteHostname, MessageContent.SessionInitMessageContent localSessionData) { if (!_initialized) { _evtRaiser.RaiseEvent("SessionError", "Cannot transmit session initialization to [" + remoteIpAddress + "]: TcpSessionManager has not been initialized" , Niawa.Utilities.InlineSortedListCreator.CreateStrStr("SessionInitMessageContent", localSessionData.ToJson()) , _t2_SRL_ThreadStatus.NodeID , _t2_SRL_ThreadStatus.ParentNodeID); throw new Exception("[" + _description + "-M] Cannot transmit session initialization to [" + remoteIpAddress + "]: TcpSessionManager has not been initialized."); } logger.Info("[" + _description + "-M] Transmitting session initialization [" + remoteIpAddress + "]"); //attempt lock bool tryLock = lockSection.WaitOne(60000); if (!tryLock) throw new TimeoutException("[" + _description + "-M] Could not obtain lock while transmitting session initialization"); try { /*session was already created and receiver is listening*/ //make sure the session exists if (!_tcpSessions.ContainsKey(remoteIpAddress)) { _evtRaiser.RaiseEvent("SessionError", "Cannot transmit session initialization: Cannot find session for address [" + remoteIpAddress + "]" , Niawa.Utilities.InlineSortedListCreator.CreateStrStr("SessionInitMessageContent", localSessionData.ToJson()) , _t2_SRL_ThreadStatus.NodeID , _t2_SRL_ThreadStatus.ParentNodeID); throw new Exception("[" + _description + "-M] Cannot transmit session initialization: Cannot find session for address [" + remoteIpAddress + "]"); } /*transmit message on stand-alone TCP transmitter to remoteIPAddress and remotePort*/ //create transmitter and start transmitting TcpTransmitter transmitter = new TcpTransmitter(remoteIpAddress, remoteHandshakePort, _evtConsumer, _utilsBus, _applicationName + ".TcpSessionManager", _t1_HR_ThreadStatus.NodeID); transmitter.StartTransmitting(_applicationName + ".TcpSessionManager.TransmitSessionInitialization"); //create and send message NiawaNetMessage message = new NiawaNetMessage(_ipAddress, _handshakePort, _hostname, remoteIpAddress, remoteHandshakePort, remoteHostname, Guid.NewGuid(), _applicationName, MessageContent.SessionInitMessageContent.MESSAGE_CONTENT_TYPE, localSessionData.ToJson()); //send message synchrnously transmitter.SendMessageSynchronous(message, 30000); //mark as init sent TcpSession session = _tcpSessions[remoteIpAddress]; session.IsSessionInitSent = true; //shut down transmitter transmitter.StopTransmitting(_applicationName + ".TcpSessionManager.TransmitSessionInitialization", false); transmitter.Dispose(); /* //wait up to 30 seconds for transmitter to start int i1 = 0; while (!transmitter.IsThreadActive && i1 < 3000) { i1++; System.Threading.Thread.Sleep(10); } //send message transmitter.SendMessage(message); //mark as init sent TcpSession session = _tcpSessions[remoteIpAddress]; session.IsSessionInitSent = true; //wait up to 30 seconds for message to send int i = 0; while (transmitter.CountMessagesInBuffer() > 0 && i < 3000) { i++; System.Threading.Thread.Sleep(10); } //shut down transmitter transmitter.StopTransmitting(_applicationName + ".TcpSessionManager.TransmitSessionInitialization", false); //wait up to 30 seconds for transmitter thread to stop int i2 = 0; while (!transmitter.IsStopped && i2 < 3000) { i2++; System.Threading.Thread.Sleep(10); } transmitter.Dispose(); */ logger.Info("[" + _description + "-M] Session initialization sent to remote [" + remoteIpAddress + "]"); //raise event _evtRaiser.RaiseEvent("Session", "Session initialization sent to remote [" + remoteIpAddress + "]" , Niawa.Utilities.InlineSortedListCreator.CreateStrStr("SessionInitMessageContent", localSessionData.ToJson()) , _t2_SRL_ThreadStatus.NodeID , _t2_SRL_ThreadStatus.ParentNodeID); } catch (Exception ex) { logger.Error("[" + _description + "-M] Error while transmitting session initialization [" + remoteIpAddress + "]:" + ex.Message, ex); _evtRaiser.RaiseEvent("Error", "Error while transmitting session initialization [" + remoteIpAddress + "]:" + ex.Message, null, _t2_SRL_ThreadStatus.NodeID, _t2_SRL_ThreadStatus.ParentNodeID); throw ex; } finally { //release lock lockSection.Release(); } }
/// <summary> /// Add a transmitter to the Tcp session. A transmitter will be created with the remote session port supplied. /// </summary> /// <param name="remoteSessionPort"></param> public void AddTransmitter(int remoteSessionPort) { try { if (_transmitter != null) throw new Exception("[" + _sessionIdentifier + "] Cannot add transmitter; transmitter was already added to the session"); _remoteSessionPort = remoteSessionPort; _transmitter = new TcpTransmitter(_remoteIpAddress, _remoteSessionPort, _evtConsumer, _utilsBus, _applicationName + ".TcpSession", _threadStatus.NodeID); } catch (Exception ex) { logger.Error("[" + _sessionIdentifier + "] Error adding transmitter: " + ex.Message, ex); throw ex; } }