Beispiel #1
0
        void ReceiveThread()
        {
            string receiveString;

            int count;

            count = stream.Read(buffer, 0, buffer.Length);
            // Block --------------------------------
            if (count <= 0)
            {
                Log("与服务器断开连接");
                NetworkSystem.ShutdownClient();
                return;
            }
            receiveString       = Encoding.UTF8.GetString(buffer, 0, count);
            NetworkSystem.netId = receiveString.Substring(0, receiveString.Length - 1);
            NetworkSystem.CallConnection();

            try
            {
                while (true)
                {
                    count = stream.Read(buffer, 0, buffer.Length);
                    // Block --------------------------------
                    if (count <= 0)
                    {
                        Log("与服务器断开连接");
                        NetworkSystem.ShutdownClient();
                        return;
                    }
                    // TODO 得处理超长度的情况
                    receiveString = Encoding.UTF8.GetString(buffer, 0, count);
                    Log($"Receive{client.Client.LocalEndPoint}:{receiveString}");
                    receiveStringBuffer += receiveString;

                    Match match = PacketCutter.Match(receiveStringBuffer);
                    while (match.Success)
                    {
                        NetworkSystem.CallReceive(match.Groups[1].Value);
                        receiveStringBuffer = match.Groups[2].Value;
                        match = PacketCutter.Match(receiveStringBuffer);
                    }
                }
            }
            catch (ThreadAbortException)
            {
                Log("Receive Thread Aborted.");
            }
            catch (Exception ex)
            {
                Log(ex);
                NetworkSystem.ShutdownClient();
            }
        }
Beispiel #2
0
 static void CallConnection() => NetworkSystem.CallConnection();