コード例 #1
0
ファイル: Client.cs プロジェクト: ninjapretzel/ExServer
        /// <summary> Common constructor logic </summary>
        private Client()
        {
            this.id = Guid.NewGuid();
            tcpReadState.Initialize();
            udpReadState.Initialize();

            tcpOutgoing = new ConcurrentQueue <string>();
            udpOutgoing = new ConcurrentQueue <string>();

            {             // Temp encryption
                EncDec encryptor = new EncDec();
                Crypt  e         = (b) => encryptor.Encrypt(b);
                Crypt  d         = (b) => encryptor.Decrypt(b);
                SetEncDec(e, d);
                //enc = e;
                //dec = d;
            }
        }
コード例 #2
0
ファイル: Client.cs プロジェクト: ninjapretzel/GMTKJam2020
        public Client(TcpClient tcpClient, Server server = null)
        {
            if (server == null)
            {
                server = Server.NullInstance;
            }
            this.server = server;
            this.id     = Guid.NewGuid();
            this.tcp    = tcpClient;
            tcpReadState.Initialize();
            udpReadState.Initialize();

            var remoteEndPoint = tcpClient.Client.RemoteEndPoint;
            var localEndpoint  = tcpClient.Client.LocalEndPoint;

            Log.Info($"\\eClient \\y {identity}\\e connected from \\y {localEndpoint} -> {remoteEndPoint}");
            if (remoteEndPoint is IPEndPoint && localEndpoint is IPEndPoint)
            {
                IPEndPoint remoteIpep = remoteEndPoint as IPEndPoint;
                IPEndPoint localIpep  = localEndpoint as IPEndPoint;
                remoteIP   = remoteIpep.Address.ToString();
                remotePort = remoteIpep.Port;
                localIP    = localIpep.Address.ToString();
                localPort  = localIpep.Port;

                int localUdpPort  = localPort + 1;
                int remoteUdpPort = remotePort + 1;
                localUdpHost  = new IPEndPoint(remoteIpep.Address, localUdpPort);
                remoteUdpHost = new IPEndPoint(remoteIpep.Address, remoteUdpPort);

                try {
                    udp = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    // Note: May need this if there are disconnections due to ICMP errors.
                    // const int SIO_UDP_CONNRESET = -1744830452;
                    // udp.IOControl((IOControlCode)SIO_UDP_CONNRESET, new byte[] { 0, 0, 0, 0 },  null);
                    if (isMaster)
                    {
                        udp.Bind(localUdpHost);
                    }
                    Log.Info($"{identity} UDP Connected to {localUdpHost} ==> {remoteUdpHost}");
                } catch (Exception e) {
                    Log.Warning($"{identity} Failed to bind UDP. Disabling UDP.", e);
                    udp = null;
                }
            }
            else
            {
                remoteIP   = "????";
                remotePort = -1;
                localIP    = "????";
                localPort  = -1;
                Log.Info($"{identity} UDP Unconnected.");
            }
            tcpStream.ReadTimeout  = DEFAULT_READWRITE_TIMEOUT;
            tcpStream.WriteTimeout = DEFAULT_READWRITE_TIMEOUT;

            tcpOutgoing = new ConcurrentQueue <string>();
            udpOutgoing = new ConcurrentQueue <string>();

            {             // Temp encryption
                EncDec encryptor = new EncDec();
                Crypt  e         = (b) => encryptor.Encrypt(b);
                Crypt  d         = (b) => encryptor.Decrypt(b);
                SetEncDec(e, d);
                //enc = e;
                //dec = d;
            }
        }