Inheritance: Fun.FunapiDecodedTransport
    private FunapiTransport GetNewTransport(TransportProtocol protocol)
    {
        FunapiTransport transport = null;
        FunEncoding encoding = with_protobuf_ ? FunEncoding.kProtobuf : FunEncoding.kJson;

        if (FunapiConfig.IsValid)
        {
            transport = FunapiConfig.CreateTransport(protocol, encoding);
        }

        if (transport == null)
        {
            if (protocol == TransportProtocol.kTcp)
            {
                transport = new FunapiTcpTransport(kServerIp, (ushort)(with_protobuf_ ? 8022 : 8012), encoding);
                transport.AutoReconnect = true;
                //transport.EnablePing = true;
                //transport.DisableNagle = true;

                //((FunapiTcpTransport)transport).SetEncryption(EncryptionType.kIFunEngine2Encryption);
            }
            else if (protocol == TransportProtocol.kUdp)
            {
                transport = new FunapiUdpTransport(kServerIp, (ushort)(with_protobuf_ ? 8023 : 8013), encoding);

                // Please set the same encryption type as the encryption type of server.
                //((FunapiUdpTransport)transport).SetEncryption(EncryptionType.kIFunEngine2Encryption);
            }
            else if (protocol == TransportProtocol.kHttp)
            {
                transport = new FunapiHttpTransport(kServerIp, (ushort)(with_protobuf_ ? 8028 : 8018), false, encoding);

                // Send messages using WWW class
                //((FunapiHttpTransport)transport).UseWWW = true;

                // Please set the same encryption type as the encryption type of server.
                //((FunapiHttpTransport)transport).SetEncryption(EncryptionType.kIFunEngine2Encryption);
            }
        }

        if (transport != null)
        {
            transport.StartedCallback += new TransportEventHandler(OnTransportStarted);
            transport.StoppedCallback += new TransportEventHandler(OnTransportClosed);
            transport.FailureCallback += new TransportEventHandler(OnTransportFailure);

            // Connect timeout.
            transport.ConnectTimeoutCallback += new TransportEventHandler(OnConnectTimeout);
            transport.ConnectTimeout = 10f;

            // If you prefer use specific Json implementation other than Dictionary,
            // you need to register json accessors to handle the Json implementation before FunapiNetwork::Start().
            // E.g., transport.JsonHelper = new YourJsonAccessorClass

            // Adds extra server list
            // Use HostHttp for http transport.
            //transport.AddServerList(new List<HostAddr>{
            //    new HostAddr("127.0.0.1", 8012), new HostAddr("127.0.0.1", 8012),
            //    new HostAddr("127.0.0.1", 8013), new HostAddr("127.0.0.1", 8018)
            //});
        }

        return transport;
    }
Example #2
0
        public static FunapiTransport CreateTransport(TransportProtocol protocol,
                                                       FunEncoding encoding = FunEncoding.kJson,
                                                       EncryptionType encryption = EncryptionType.kDefaultEncryption)
        {
            if (data_ == null)
            {
                DebugUtils.Log("There's no config data. You should call FunapiConfig.Load first.");
                return null;
            }

            string str_protocol;
            if (protocol == TransportProtocol.kTcp)
                str_protocol = "tcp";
            else if (protocol == TransportProtocol.kUdp)
                str_protocol = "udp";
            else if (protocol == TransportProtocol.kHttp)
                str_protocol = "http";
            else
            {
                DebugUtils.Log("CreateTransport - Invalid protocol. protocol: {0}", protocol);
                return null;
            }

            string str_ip = string.Format("{0}_server_ip", str_protocol);
            string str_port = string.Format("{0}_server_port", str_protocol);
            if (!data_.ContainsKey(str_ip) || !data_.ContainsKey(str_port))
            {
                DebugUtils.Log("CreateTransport - Can't find values with '{0}'", str_protocol);
                return null;
            }

            string hostname_or_ip = data_[str_ip] as string;
            UInt16 port = Convert.ToUInt16(data_[str_port]);
            if (hostname_or_ip.Length <= 0 || port == 0)
            {
                DebugUtils.Log("CreateTransport - Invalid value. ip:{0} port:{1} encoding:{2}",
                               hostname_or_ip, port, encoding);
                return null;
            }

            if (protocol == TransportProtocol.kTcp)
            {
                FunapiTcpTransport transport = new FunapiTcpTransport(hostname_or_ip, port, encoding);

                if (data_.ContainsKey("disable_nagle"))
                    transport.DisableNagle = (bool)data_["disable_nagle"];

                if (encryption != EncryptionType.kDefaultEncryption)
                    transport.SetEncryption(encryption);

                return transport;
            }
            else if (protocol == TransportProtocol.kUdp)
            {
                FunapiUdpTransport transport = new FunapiUdpTransport(hostname_or_ip, port, encoding);

                if (encryption != EncryptionType.kDefaultEncryption)
                    transport.SetEncryption(encryption);

                return transport;
            }
            else if (protocol == TransportProtocol.kHttp)
            {
                bool with_https = false;
                if (data_.ContainsKey("http_with_secure"))
                    with_https = (bool)data_["http_with_secure"];

                FunapiHttpTransport transport = new FunapiHttpTransport(hostname_or_ip, port, with_https, encoding);

                if (encryption != EncryptionType.kDefaultEncryption)
                    transport.SetEncryption(encryption);

                return transport;
            }

            return null;
        }