/// <summary> /// Synchronously connects to the remote host. /// </summary> /// <returns>True if the connection attempt completed successfully, otherwise false.</returns> public bool Connect() { Close(); lock (_sync) { State = GlowEndPointState.Connecting; _tcpClient = new TcpClient(); try { _tcpClient.Connect(HostName, TcpPort); _glowReader = new GlowReader(GlowReader_RootReady, GlowReader_KeepAliveRequestReceived); _glowReader.Error += GlowReader_Error; var stream = _tcpClient.GetStream(); stream.BeginRead(_buffer, 0, _buffer.Length, ReceiveCallback, stream); State = GlowEndPointState.ProtocolProbing; var glow = GlowRootElementCollection.CreateRoot(); glow.Insert(new GlowCommand(GlowCommandType.GetDirectory)); Write(glow); return(true); } catch (Exception ex) { State = GlowEndPointState.Error; Console.WriteLine(ex.Message); } } return(false); }
/// <summary> /// Closes the endpoint. /// </summary> public void Close() { lock (_sync) { if (_glowReader != null) { _glowReader.Dispose(); } if (_tcpClient != null) { try { _tcpClient.Close(); } catch { } } _glowReader = null; _tcpClient = null; State = GlowEndPointState.Closed; } }
public GlowEndPoint(string hostName, int tcpPort, int maxPackageLength, int localNumber, byte remoteSlotId = 0, string description = null) { HostName = hostName; TcpPort = tcpPort; MaxPackageLength = maxPackageLength; RemoteSlotId = remoteSlotId; LocalNumber = localNumber; Description = description; _state = GlowEndPointState.Closed; }
void GlowReader_KeepAliveRequestReceived(object sender, FramingReader.KeepAliveRequestReceivedArgs e) { TcpClient tcpClient; State = GlowEndPointState.Connected; lock (_sync) tcpClient = _tcpClient; if (tcpClient != null) { tcpClient.Client.Send(e.Response, e.ResponseLength, SocketFlags.None); } }
void GlowReader_RootReady(object sender, EmberLib.AsyncDomReader.RootReadyArgs e) { var root = e.Root as EmberLib.Glow.GlowContainer; State = GlowEndPointState.Connected; if (root != null) { OnGlowRootReady(new GlowRootReadyArgs(root)); } else { OnNotification(new NotificationArgs(String.Format("Unexpected Ember Root: {0} ({1})", e.Root, e.Root.GetType()))); } }