Beispiel #1
0
    static void Main(String[] args)
    {
        ProtocolManager.GetInstance().Register();
        StartClient();
        bool gameRunning = true;

        while (gameRunning)
        {
        }
    }
        public Form1()
        {
            InitializeComponent();

            InitNetDeviceListUI();

            netManager   = ProtocolManager.GetInstance();
            recvHandler  = new ProtocolManager.OnProtocolRecvListener(OnProtocolRecvListener);
            errorHandler = new ProtocolManager.OnProtocolErrorListener(OnProtocolErrorListener);
            netManager.eventProtocolRecvHandler  += recvHandler;
            netManager.eventProtocolErrorHandler += errorHandler;
        }
        private void Init()
        {
            labelConnectStatus.Text = "设备未连接";

            netManager   = ProtocolManager.GetInstance();
            recvHandler  = new ProtocolManager.OnProtocolRecvListener(OnProtocolRecvListener);
            errorHandler = new ProtocolManager.OnProtocolErrorListener(OnProtocolErrorListener);
            netManager.eventProtocolRecvHandler  += recvHandler;
            netManager.eventProtocolErrorHandler += errorHandler;

            netManager.SetOnGetMachineTemperatureListener(onGetMachinetemperature);
            netManager.SetOnReportValueListener(onReportValue);
        }
Beispiel #4
0
    static void Main(string[] args)
    {
        ProtocolManager.GetInstance().Register();
        ProtocolHandler.GetInstance().Register();
        SessionServer.GetInstance().Start();

        bool gameRunning = true;

        //ThreadManager.GetInstance().RegisterWork(() => {
        //    Console.WriteLine("Test");
        //});

        while (gameRunning)
        {
        }
    }
Beispiel #5
0
    async static void StartClient()
    {
        TcpClient tcpClient = new TcpClient("127.0.0.1", SESSION_SERVER_PORT);

        Console.WriteLine("Session Server Connected!");

        Task consoleReadTask = new Task(() => {
            while (true)
            {
                var chat = Console.ReadKey();
                if (chat.Key == ConsoleKey.UpArrow)
                {
                    Send(new Login {
                        PID     = "antori",
                        LoginAt = 1234,
                    });
                }
            }
        });

        consoleReadTask.Start();

        networkStream = tcpClient.GetStream();
        receiveBuffer = new byte[RECEIVE_BUFFER_SIZE];

        Send(new Login {
            PID     = "antori",
            LoginAt = 1234,
        });
        while (true)
        {
            int bytesReceived = await networkStream.ReadAsync(receiveBuffer, 0, RECEIVE_BUFFER_SIZE).ConfigureAwait(false);

            int packetLength = BitConverter.ToInt32(receiveBuffer);
            Console.WriteLine("packet Length : " + packetLength);

            if (packetLength <= 0)
            {
                Console.WriteLine("No Packet");
                continue;
            }

            while (bytesReceived <= packetLength)
            {
                bytesReceived += await networkStream.ReadAsync(receiveBuffer, bytesReceived, receiveBuffer.Length - bytesReceived).ConfigureAwait(false);
            }

            int    bodyLength     = packetLength - PACKET_LENGTH_HEADER_SIZE;
            byte[] optimizeBuffer = new byte[bodyLength];
            Array.Copy(receiveBuffer, PACKET_LENGTH_HEADER_SIZE, optimizeBuffer, 0, bodyLength);

            using (var ms = new MemoryStream(optimizeBuffer))
                using (var br = new BinaryReader(ms)) {
                    int protocol_id = br.ReadInt32();
                    Console.WriteLine("Protocol ID : " + protocol_id);

                    var protocol = ProtocolManager.GetInstance().GetProtocol(protocol_id);
                    if (protocol != null)
                    {
                        protocol.Read(br);
                    }
                }


            Array.Clear(receiveBuffer, 0, receiveBuffer.Length);
        }
    }
    async void ReceiveProcess()
    {
        while (true)
        {
            int bytesReceived = 0;
            try {
                bytesReceived = await networkStream.ReadAsync(receiveBuffer, 0, receiveBuffer.Length).ConfigureAwait(false);

                while (bytesReceived <= Const.PACKET_LENGTH_HEADER_SIZE)
                {
                    bytesReceived += await networkStream.ReadAsync(receiveBuffer, bytesReceived, receiveBuffer.Length - bytesReceived).ConfigureAwait(false);
                }
            } catch (Exception e) {
                Console.WriteLine(e);
                connectedServer.OnClientLeave(session_id);
                return;
            }


            int packetLength = BitConverter.ToInt32(receiveBuffer);
            Console.WriteLine("packet Length : " + packetLength);

            if (packetLength <= 0)
            {
                Console.WriteLine("No Packet");
                continue;
            }

            try {
                while (bytesReceived <= packetLength)
                {
                    bytesReceived += await networkStream.ReadAsync(receiveBuffer, bytesReceived, receiveBuffer.Length - bytesReceived).ConfigureAwait(false);
                }
            } catch (Exception e) {
                Console.WriteLine(e);
                connectedServer.OnClientLeave(session_id);
                return;
            }


            int    bodyLength     = packetLength - Const.PACKET_LENGTH_HEADER_SIZE;
            byte[] optimizeBuffer = new byte[bodyLength];
            Array.Copy(receiveBuffer, Const.PACKET_LENGTH_HEADER_SIZE, optimizeBuffer, 0, bodyLength);

            using (var ms = new MemoryStream(optimizeBuffer))
                using (var br = new BinaryReader(ms)) {
                    int protocol_id = br.ReadInt32();
                    Console.WriteLine("Protocol ID : " + protocol_id);

                    var protocol = ProtocolManager.GetInstance().GetProtocol(protocol_id);
                    if (protocol != null)
                    {
                        protocol.Read(br);
                        ProtocolHandler.GetInstance().Protocol_Logic(protocol, this);
                    }
                }


            Array.Clear(receiveBuffer, 0, receiveBuffer.Length);
        }
    }