Beispiel #1
0
        internal static Stream WrapStream(Stream stream, String hostName)
        {
            if (GSTlsClient.logger != null)
            {
                GSTlsClient.logger("Wrapping");
            }

            TlsClientProtocol tslcp = new TlsClientProtocol(stream, new SecureRandom());

            tslcp.Connect(new GSTlsClient(hostName));
            return(new DuplexTlsStream(tslcp.Stream));
        }
Beispiel #2
0
        private void ConnectCallback(IAsyncResult result)
#endif
        {
            //We can get here is the connection fails and the monotor thread
            // calls close. We should just do nothing at this point
#if __WINDOWS__
            if (client == null || !connected)
            {
                return;
            }

            if (stopped)
            {
                if (connected)
                {
                    client.Dispose();
                }
                return;
            }
#else
            if ((client != null && !client.Connected))
            {
                return;
            }

            if (stopped)
            {
                if (client.Connected)
                {
                    client.Close();
                }
                return;
            }

            clientStream = GSTlsClient.WrapStream(client.GetStream(), remotehost);
#endif

            //Each time a tcp connection is established we re-authenticate
            try {
                LoginCommand loginCmd = new LoginCommand(session.ConnectToken);

                Send(loginCmd);

                Packet p = PooledObjects.PacketPool.Pop();

                PositionStream rss = PooledObjects.PositionStreamPool.Pop();
#if __WINDOWS__
                rss.Wrap(client.InputStream.AsStreamForRead());
#else
                rss.Wrap(clientStream);
#endif

                int bytesRead = 0;

                while ((bytesRead = read(rss, p)) != 0)
                {
                    OnPacketReceived(p, bytesRead);
                    PooledObjects.PacketPool.Push(p);
                    p = PooledObjects.PacketPool.Pop();
                }

                PooledObjects.PacketPool.Push(p);
                PooledObjects.PositionStreamPool.Push(rss);
            }
#if __WINDOWS__
            /*catch (AggregateException exception)
             * {
             *  foreach (Exception ex in exception.InnerExceptions)
             *  {
             *      System.Diagnostics.Debug.WriteLine(ex);
             *  }
             * }*/
#endif
            catch (Exception e)
            {
                if (session != null && !stopped)
                {
                    session.ConnectState = GameSparksRT.ConnectState.Disconnected;
                    session.Log("ReliableConnection", GameSparksRT.LogLevel.DEBUG, e.Message);
                    try{
                        session.OnReady(false);
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex);
                    }
                }
            }
        }