Ejemplo n.º 1
0
        private async Task BindChannelAsync(DataFormat dataFormat)
        {
            if (socketListener != null)
            {
                throw new InvalidOperationException("Only one Listner can be attached to this port.");
            }
            try
            {
                this.ConnectionStatus              = ConnectionStatus.Connecting;
                socketListener                     = new StreamSocketListener();
                socketListener.Control.NoDelay     = true;
                socketListener.ConnectionReceived += async(streamSocketListener, streamSocketListenerConnectionReceivedEventArgs) =>
                {
                    ChannelBase channel = await ChannelFactory.BindChannelAsync(dataFormat, this, streamSocketListenerConnectionReceivedEventArgs.Socket).ConfigureAwait(false);

                    activeSessions.Add(channel.SessionId, channel);
                    await channel.Parse("Hello" + Environment.NewLine).ConfigureAwait(false);
                };
                await socketListener.BindServiceNameAsync(port.ToString()).AsTask().ConfigureAwait(false);

                this.ConnectionStatus = ConnectionStatus.Listening;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                this.ConnectionStatus = ConnectionStatus.Failed;
            }
        }
Ejemplo n.º 2
0
        public async Task <ChannelBase> Connect(string remoteServer, string remotePort, DataFormat dataFormat)
        {
            try
            {
                ConnectionStatus             = ConnectionStatus.Connecting;
                hostName                     = new HostName(remoteServer);
                streamSocket                 = new StreamSocket();
                streamSocket.Control.NoDelay = true;
                await streamSocket.ConnectAsync(hostName, remotePort).AsTask().ConfigureAwait(false);

                ConnectionStatus = ConnectionStatus.Connected;

                channel = await ChannelFactory.BindChannelAsync(dataFormat, this, streamSocket).ConfigureAwait(false);
            }
            catch (Exception exception)
            {
                ConnectionStatus = ConnectionStatus.Failed;
                Debug.WriteLine(string.Format("Error receiving data: {0}", exception.Message));
            }
            return(channel);
        }