Exemplo n.º 1
0
        internal virtual Packet ToPacket(IRTSessionInternal session, bool fast)
        {
            Packet p = PooledObjects.PacketPool.Pop();

            p.OpCode  = OpCode;
            p.Data    = Data;
            p.Session = session;

            if (!fast && intent != GameSparksRT.DeliveryIntent.RELIABLE)
            {
                p.Reliable = false;
            }

            if (intent == GameSparksRT.DeliveryIntent.UNRELIABLE_SEQUENCED)
            {
                p.SequenceNumber = session.NextSequenceNumber();
            }

            if (TargetPlayers != null && TargetPlayers.Count > 0)
            {
                p.TargetPlayers = TargetPlayers;
            }

            p.Request = this;
            return(p);
        }
        internal CustomCommand Configure(int opCode, int sender, Stream lps, RTData data, int limit, IRTSessionInternal session, int packetSize)
        {
            ms = PooledObjects.MemoryStreamPool.Pop();
            this.packetSize    = packetSize;
            this.opCode        = opCode;
            this.sender        = sender;
            this.data          = data;
            this.session       = session;
            this.limit         = limit;
            this.limitedStream = null;

            if (lps != null)
            {
                limitedStream = PooledObjects.LimitedPositionStreamPool.Pop();

                for (int i = 0; i < limit; i++)
                {
                    byte read = (byte)lps.ReadByte();
                    ms.WriteByte(read);
                }
                ms.Position = 0;
                limitedStream.Wrap(ms, limit);
            }

            return(this);
        }
Exemplo n.º 3
0
        public FastConnection(string remotehost, int remoteport, IRTSessionInternal session) : base(remotehost, remoteport, session)
        {
#if __WINDOWS__
            client = new DatagramSocket();
            client.MessageReceived += OnSocketMessageReceived;

            Task t = Task.Run(() => {
                ConnectAsync().Wait();

                DoLogin();
            });

            session.Log("FastConnection", GameSparksRT.LogLevel.DEBUG, " local=" + endPoint.LocalHostName + " remote=" + remotehost + ":" + remoteport);
#else
            bool ipv6 = false;

            callback = new AsyncCallback(Recv);

            if (remoteEndPoint.AddressFamily == AddressFamily.InterNetworkV6)
            {
                try {
                    if (Socket.OSSupportsIPv6)
                    {
                        ipv6 = true;
                    }
                } catch (Exception e) {
                    Console.WriteLine("Socket.OSSupportsIPv6: " + e.ToString());
                }

                try {
                    if (Socket.SupportsIPv6)
                    {
                        ipv6 = true;
                    }
                } catch (Exception e) {
                    Console.WriteLine("Socket.SupportsIPv6: " + e.ToString());
                }
            }

            if (ipv6)
            {
                client = new UdpClient(AddressFamily.InterNetworkV6);

                Console.WriteLine("IPv6 on!");
            }
            else
            {
                client = new UdpClient(AddressFamily.InterNetwork);
            }

            client.Connect(remoteEndPoint);

            session.Log("FastConnection", GameSparksRT.LogLevel.DEBUG, " local=" + client.Client.LocalEndPoint + " remote=" + remotehost + ":" + remoteport);

            client.Client.BeginReceive(buffer, 0, GameSparksRT.MAX_UNRELIABLE_MESSAGE_SIZE_BYTES, 0, callback, null);

            DoLogin();
#endif
        }
Exemplo n.º 4
0
        public ReliableWSConnection(string remotehost, int remoteport, IRTSessionInternal session) : base(remotehost, remoteport, session)
        {
            //GameSparks.Core.GameSparksUtil.LogMessageHandler = Console.WriteLine;

            session.Log("ReliableWSConnection", GameSparksRT.LogLevel.DEBUG, "wss://" + remotehost + ":" + remoteport);

            if (session.GetGSInstance() != null)
            {
                socket = session.GetGSInstance().GSPlatform.GetBinarySocket("wss://" + remotehost + ":" + remoteport, OnBinaryMessageReceived, OnClosed, OnOpened, OnError);
            }
            else
            {
                socket = new GameSparksWebSocket();
                socket.Initialize("wss://" + remotehost + ":" + remoteport, OnBinaryMessageReceived, OnClosed, OnOpened, OnError);
            }
            socket.Open();
        }
Exemplo n.º 5
0
        public Connection(string remoteHost, int port, IRTSessionInternal session)
        {
            emptyStream.Wrap(new BinaryWriteMemoryStream());
            this.session = session;

#if __WINDOWS__
            try
            {
                Task <EndpointPair> t = Task.Run <EndpointPair>(() => ResolveDNS(remoteHost, port));

                t.Wait();

                endPoint = t.Result;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex);
            }
#else
            GSInstance instance = session.GetGSInstance();
            ResolveRemoteEndpoint(remoteHost, port);
        }
        internal static IRTCommand GetCommand(int opCode, int sender, int?sequence, Stream stream, IRTSessionInternal session, RTData data, int packetSize)
        {
            long limit = global::GameSparks.RT.Proto.ProtocolParser.ReadUInt32(stream);
            LimitedPositionStream lps = PooledObjects.LimitedPositionStreamPool.Pop();

            try{
                lps.Wrap(stream, limit);
                switch (opCode)
                {
                case OpCodes.LoginResult:
                    return(LoginResult.Deserialize(lps, LoginResult.pool.Pop()));

                case OpCodes.PingResult:
                    return(PingResult.Deserialize(lps, PingResult.pool.Pop()));

                case OpCodes.UDPConnectMessage:
                    return(UDPConnectMessage.Deserialize(lps, UDPConnectMessage.pool.Pop()));

                case OpCodes.PlayerConnectMessage:
                    return(PlayerConnectMessage.Deserialize(lps, PlayerConnectMessage.pool.Pop()));

                case OpCodes.PlayerDisconnectMessage:
                    return(PlayerDisconnectMessage.Deserialize(lps, PlayerDisconnectMessage.pool.Pop()));

                default:
                    if (session.ShouldExecute(sender, sequence))
                    {
                        return(CustomCommand.pool.Pop().Configure(opCode, sender, lps, data, (int)limit, session, packetSize));
                    }

                    return(null);
                }
            } finally {
                lps.SkipToEnd();
                PooledObjects.LimitedPositionStreamPool.Push(lps);
            }
        }
Exemplo n.º 7
0
        public ReliableConnection(string remotehost, int remoteport, IRTSessionInternal session) : base(remotehost, remoteport, session)
        {
#if __WINDOWS__
            client = new StreamSocket();

            client.Control.NoDelay   = true;
            client.Control.KeepAlive = true;
            //client.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted | ChainValidationResult.InvalidName);

            System.Diagnostics.Debug.WriteLine("*** Connecting to server...");

            connected = false;

            Task t = Task.Run(() =>
            {
                connected = ConnectAsync(remotehost, remoteport).Result;

                ConnectCallback();
            });
#else
            bool ipv6 = false;

            this.remotehost = remotehost;

            if (remoteEndPoint.AddressFamily == AddressFamily.InterNetworkV6)
            {
                try
                {
                    if (Socket.OSSupportsIPv6)
                    {
                        ipv6 = true;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Socket.OSSupportsIPv6: " + e.ToString());
                }

                try
                {
                    if (Socket.SupportsIPv6)
                    {
                        ipv6 = true;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Socket.SupportsIPv6: " + e.ToString());
                }
            }

            if (ipv6)
            {
                client = new TcpClient(AddressFamily.InterNetworkV6);

                Console.WriteLine("IPv6 on!");
            }
            else
            {
                client = new TcpClient(AddressFamily.InterNetwork);
            }

            client.NoDelay = true;
            client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
            client.BeginConnect(remoteEndPoint.Address, remoteEndPoint.Port, new AsyncCallback(ConnectCallback), null);
#endif
        }
Exemplo n.º 8
0
 internal void Configure(Packet packet, IRTSessionInternal session)
 {
     this.packet  = packet;
     this.session = session;
 }