diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..05e8913 --- /dev/null +++ b/.clang-format @@ -0,0 +1,75 @@ +# Clang-Format configuration file +# Based on Google C++ style with moderate adjustments + +Language: Cpp +BasedOnStyle: Google + +# Indentation +IndentWidth: 4 +TabWidth: 4 +UseTab: Never +AccessModifierOffset: -4 + +# Line width +ColumnLimit: 100 + +# Braces +BreakBeforeBraces: Attach +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Empty +AllowShortBlocksOnASingleLine: Empty + +# Pointer and reference alignment +PointerAlignment: Left +ReferenceAlignment: Left + +# Include file sorting +SortIncludes: CaseSensitive +IncludeBlocks: Regroup +IncludeCategories: + # 1. Project headers + - Regex: '^"(client|server|common|protocol)/' + Priority: 1 + # 2. Third-party libraries + - Regex: '^<.*\.h>' + Priority: 2 + # 3. C++ standard library + - Regex: '^<.*>' + Priority: 3 + +# Namespace +NamespaceIndentation: None +CompactNamespaces: false +FixNamespaceComments: true + +# Alignment +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignOperands: true +AlignTrailingComments: true + +# Line breaking +AllowAllParametersOfDeclarationOnNextLine: true +BinPackArguments: true +BinPackParameters: true +BreakConstructorInitializers: BeforeColon +ConstructorInitializerAllOnOneLineOrOnePerLine: true + +# Spacing +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: ControlStatements +SpacesInAngles: false +SpacesInParentheses: false +SpacesInSquareBrackets: false + +# C++11 features +Standard: c++17 +Cpp11BracedListStyle: true + +# Comments +ReflowComments: true +SpacesBeforeTrailingComments: 2 diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..baeab80 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,52 @@ +# EditorConfig helps maintain consistent coding styles across different editors +# https://editorconfig.org + +root = true + +# Default settings (applies to all files) +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +indent_style = space +indent_size = 4 + +# C/C++ files +[*.{c,cc,cpp,h,hpp}] +indent_size = 4 + +# CMake files +[CMakeLists.txt] +indent_size = 4 + +[*.cmake] +indent_size = 4 + +# Markdown files (preserve trailing spaces for line breaks) +[*.md] +trim_trailing_whitespace = false + +# YAML files +[*.{yml,yaml}] +indent_size = 2 + +# JSON files +[*.json] +indent_size = 2 + +# Shell scripts +[*.sh] +indent_size = 2 + +# Batch scripts +[*.bat] +end_of_line = crlf + +# Makefile (must use tabs) +[Makefile] +indent_style = tab + +# Docker files +[Dockerfile*] +indent_size = 4 diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..9412c8d --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,37 @@ +--- +name: Bug 报告 +about: 报告一个 bug 帮助我们改进 +title: '[BUG] ' +labels: bug +assignees: '' +--- + +## 🐛 Bug 描述 +简要描述这个 bug 是什么。 + +## 📝 复现步骤 +1. 执行 '...' +2. 运行命令 '...' +3. 查看错误 + +## ✅ 期望行为 +描述你期望发生什么。 + +## ❌ 实际行为 +描述实际发生了什么。 + +## 🖥️ 环境信息 +- 操作系统: [例如 Ubuntu 22.04 / Windows 11] +- 编译器: [例如 GCC 11.4 / MSVC 19.36] +- CMake 版本: [例如 3.22] + +## 📋 日志输出 +``` +粘贴相关日志输出 +``` + +## 📷 截图 +如有必要,添加截图。 + +## 💡 其他信息 +添加任何其他有关该问题的上下文。 diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..b46c34c --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,29 @@ +--- +name: 功能请求 +about: 提出新功能或改进建议 +title: '[FEATURE] ' +labels: enhancement +assignees: '' +--- + +## 🚀 功能描述 +简要描述你想要的功能。 + +## 💡 动机 +为什么需要这个功能?它解决什么问题? + +## 📋 详细设计(可选) +如果你有实现思路,请描述: + +### 方案 +描述你提议的解决方案。 + +### 替代方案 +你考虑过的其他方案。 + +## ✅ 验收标准 +- [ ] 标准 1 +- [ ] 标准 2 + +## 📚 参考资料 +相关链接、文档或示例。 diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..c0b089c --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,33 @@ +## 📋 PR Description +Briefly describe what this PR does. + +## 🔗 Related Issue +Closes #(issue number) + +## 🔄 Change Type +Please check the applicable options: + +- [ ] 🐛 Bug fix (fix) +- [ ] ✨ New feature (feat) +- [ ] 📝 Documentation update (docs) +- [ ] 🎨 Code format/style (style) +- [ ] ♻️ Code refactoring (refactor) +- [ ] ✅ Test related (test) +- [ ] 🔧 Build/configuration (chore) + +## 📝 Change Details +- Change 1 +- Change 2 + +## ✅ Self-Check List +- [ ] Code compiles successfully +- [ ] All tests pass +- [ ] Necessary tests added +- [ ] Related documentation updated +- [ ] Commit messages follow conventions + +## 🧪 Testing Instructions +Describe how to test these changes. + +## 📷 Screenshots (if applicable) +Screenshots of UI changes. diff --git a/.gitignore b/.gitignore index 670a86b..2186dc2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,8 @@ -# IDE 个人化配置 +# IDE personal configurations .vscode/ *.code-workspace -# OS 文件 +# OS files .DS_Store Thumbs.db desktop.ini @@ -10,15 +10,15 @@ desktop.ini *.swo *~ -# 构建产物(如果根目录构建) +# Build artifacts (if built in root directory) build/ out/ bin/ dist/ -# 日志 +# Logs *.log -# 临时文件 +# Temporary files *.tmp *.temp diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..2aaa013 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,75 @@ +# Contributor Covenant Code of Conduct + +[![中文](https://img.shields.io/badge/lang-中文-red.svg)](CODE_OF_CONDUCT.zh-CN.md) + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, religion, or sexual identity +and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our +community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the + overall community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or + advances of any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email + address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported by creating an issue in this repository. All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.0, available at +https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. + +[homepage]: https://www.contributor-covenant.org diff --git a/CODE_OF_CONDUCT.zh-CN.md b/CODE_OF_CONDUCT.zh-CN.md new file mode 100644 index 0000000..0b6860b --- /dev/null +++ b/CODE_OF_CONDUCT.zh-CN.md @@ -0,0 +1,46 @@ +# 贡献者行为准则 + +[![English](https://img.shields.io/badge/lang-English-blue.svg)](CODE_OF_CONDUCT.md) + +## 我们的承诺 + +为了营造一个开放和友好的环境,我们作为贡献者和维护者承诺:无论年龄、体型、残疾、种族、性别特征、性别认同和表达、经验水平、教育程度、社会经济地位、国籍、外貌、种族、宗教或性取向如何,参与我们项目和社区的每个人都不会受到骚扰。 + +## 我们的标准 + +有助于创造积极环境的行为包括: + +* 使用友好和包容的语言 +* 尊重不同的观点和经验 +* 优雅地接受建设性批评 +* 专注于对社区最有利的事情 +* 对其他社区成员表示同理心 + +不可接受的参与者行为包括: + +* 使用性暗示的语言或图像,以及不受欢迎的性关注或性挑逗 +* 恶意挑衅、侮辱/贬损性评论以及个人或政治攻击 +* 公开或私下骚扰 +* 未经明确许可,发布他人的私人信息,如住址或电子邮件地址 +* 其他可以合理地被认为在专业环境中不适当的行为 + +## 我们的责任 + +项目维护者有责任为可接受的行为标准做出诠释,并对任何不可接受的行为采取恰当且公平的纠正措施。 + +项目维护者有权且有责任删除、编辑或拒绝与本行为准则不符的评论、提交、代码、wiki 编辑、Issue 和其他贡献,并可以暂时或永久性地封禁任何他们认为行为不适当、威胁、冒犯或有害的贡献者。 + +## 适用范围 + +本行为准则适用于所有项目空间,当个人在公共空间代表项目或其社区时也适用。代表项目或社区的例子包括使用官方项目电子邮件地址、通过官方社交媒体账户发布消息,或在线上或线下活动中担任指定代表。 + +## 执行 + +如遇到辱骂、骚扰或其他不可接受的行为,可以通过创建 Issue 联系项目团队进行举报。所有投诉都将被审查和调查,并将做出被认为必要且适合具体情况的回应。项目团队有义务对事件举报者保密。 + +## 归属 + +本行为准则改编自 [Contributor Covenant][homepage] 2.0 版, +可在 https://www.contributor-covenant.org/version/2/0/code_of_conduct.html 获取。 + +[homepage]: https://www.contributor-covenant.org diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..b3055a2 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,124 @@ +# Contributing Guide + +[![中文](https://img.shields.io/badge/lang-中文-red.svg)](CONTRIBUTING.zh-CN.md) + +Thank you for your interest in contributing to this project! Here's how you can help. + +## 🌿 Branching Strategy + +We use a simplified Git Flow model: + +| Branch | Purpose | Protection | +|--------|---------|------------| +| `main` | Stable releases | Protected, PR required | +| `develop` | Development integration | PR required | +| `feature/*` | New features | Personal branches | +| `bugfix/*` | Bug fixes | Personal branches | + +### Branch Naming Convention + +``` +feature/add-compression +feature/file-encryption +bugfix/fix-connection-timeout +bugfix/memory-leak-fix +docs/update-readme +``` + +## 📝 How to Contribute + +### 1. Fork & Clone + +```bash +# After forking the repository +git clone https://github.com/YOUR_USERNAME/Computer_Network_lab.git +cd Computer_Network_lab +git remote add upstream https://github.com/FrankieLiu04/Computer_Network_lab.git +``` + +### 2. Create a Feature Branch + +```bash +git checkout develop +git pull upstream develop +git checkout -b feature/your-feature-name +``` + +### 3. Develop & Commit + +```bash +# Make your changes... +git add . +git commit -m "feat: add your feature description" +``` + +#### Commit Message Convention + +We follow [Conventional Commits](https://www.conventionalcommits.org/) specification: + +| Type | Description | +|------|-------------| +| `feat` | New feature | +| `fix` | Bug fix | +| `docs` | Documentation update | +| `style` | Code style (no logic change) | +| `refactor` | Code refactoring | +| `test` | Test related | +| `chore` | Build/tooling changes | + +Examples: +``` +feat: add resumable file upload +fix: resolve memory overflow for large files +docs: update Docker deployment guide +``` + +### 4. Push & Create PR + +```bash +git push origin feature/your-feature-name +``` + +Then create a Pull Request on GitHub targeting the `develop` branch. + +## ✅ PR Checklist + +Before submitting your PR, please verify: + +- [ ] Code compiles successfully +- [ ] All tests pass (`ctest` or CI) +- [ ] Code follows style guidelines +- [ ] Documentation updated (if needed) +- [ ] Commit messages follow conventions + +## 🏗️ Development Environment + +### Requirements + +- CMake 3.16+ +- C++17 compatible compiler +- Docker (optional, for containerized testing) + +### Build + +```bash +cd reliable-remote-backup-system +cmake -B build -DCMAKE_BUILD_TYPE=Debug +cmake --build build +``` + +### Test + +```bash +cd build +ctest --output-on-failure +``` + +## 💬 Getting Help + +- Questions? Create an [Issue](https://github.com/FrankieLiu04/Computer_Network_lab/issues) +- Want to discuss? Use the `question` label + +## 📜 Code of Conduct + +Please read our [Code of Conduct](CODE_OF_CONDUCT.md) to maintain a friendly and inclusive environment. diff --git a/CONTRIBUTING.zh-CN.md b/CONTRIBUTING.zh-CN.md new file mode 100644 index 0000000..7ea10af --- /dev/null +++ b/CONTRIBUTING.zh-CN.md @@ -0,0 +1,124 @@ +# 贡献指南 + +[![English](https://img.shields.io/badge/lang-English-blue.svg)](CONTRIBUTING.md) + +感谢你对本项目的兴趣!以下是参与贡献的指南。 + +## 🌿 分支策略 + +我们采用简化的 Git Flow 模型: + +| 分支 | 用途 | 保护级别 | +|------|------|----------| +| `main` | 稳定发布版本 | 受保护,需要 PR | +| `develop` | 开发集成分支 | 需要 PR | +| `feature/*` | 新功能开发 | 个人分支 | +| `bugfix/*` | Bug 修复 | 个人分支 | + +### 分支命名规范 + +``` +feature/add-compression +feature/file-encryption +bugfix/fix-connection-timeout +bugfix/memory-leak-fix +docs/update-readme +``` + +## 📝 如何贡献 + +### 1. Fork & Clone + +```bash +# Fork 本仓库后 +git clone https://github.com/你的用户名/Computer_Network_lab.git +cd Computer_Network_lab +git remote add upstream https://github.com/FrankieLiu04/Computer_Network_lab.git +``` + +### 2. 创建功能分支 + +```bash +git checkout develop +git pull upstream develop +git checkout -b feature/你的功能名称 +``` + +### 3. 开发 & 提交 + +```bash +# 进行开发... +git add . +git commit -m "feat: 添加新功能描述" +``` + +#### Commit 消息规范 + +我们使用 [Conventional Commits](https://www.conventionalcommits.org/) 规范: + +| 类型 | 说明 | +|------|------| +| `feat` | 新功能 | +| `fix` | Bug 修复 | +| `docs` | 文档更新 | +| `style` | 代码格式(不影响逻辑) | +| `refactor` | 重构 | +| `test` | 测试相关 | +| `chore` | 构建/工具变动 | + +示例: +``` +feat: 添加文件断点续传功能 +fix: 修复大文件传输时的内存溢出问题 +docs: 更新 Docker 部署文档 +``` + +### 4. 推送 & 创建 PR + +```bash +git push origin feature/你的功能名称 +``` + +然后在 GitHub 上创建 Pull Request,目标分支选择 `develop`。 + +## ✅ PR 检查清单 + +提交 PR 前请确认: + +- [ ] 代码能够正常编译 +- [ ] 所有测试通过 (`ctest` 或 CI) +- [ ] 遵循代码风格规范 +- [ ] 更新了相关文档(如需要) +- [ ] Commit 消息符合规范 + +## 🏗️ 本地开发环境 + +### 依赖 + +- CMake 3.16+ +- C++17 兼容编译器 +- Docker(可选,用于容器化测试) + +### 构建 + +```bash +cd reliable-remote-backup-system +cmake -B build -DCMAKE_BUILD_TYPE=Debug +cmake --build build +``` + +### 测试 + +```bash +cd build +ctest --output-on-failure +``` + +## 💬 寻求帮助 + +- 有问题?创建 [Issue](https://github.com/FrankieLiu04/Computer_Network_lab/issues) +- 想讨论?使用 Issue 的 `question` 标签 + +## 📜 行为准则 + +请阅读我们的 [行为准则](CODE_OF_CONDUCT.zh-CN.md),确保友好、包容的协作环境。 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c6f892c --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 FrankieLiu04 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index dedef4a..92b573d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # Computer Network Lab — Reliable Remote Backup System +[![中文](https://img.shields.io/badge/lang-中文-red.svg)](README.zh-CN.md) [![CI](https://github.com/FrankieLiu04/Computer_Network_lab/actions/workflows/ci.yml/badge.svg)](https://github.com/FrankieLiu04/Computer_Network_lab/actions/workflows/ci.yml) [![C++17](https://img.shields.io/badge/C%2B%2B-17-blue.svg)](https://isocpp.org/) [![Docker](https://img.shields.io/badge/Docker-Ready-2496ED?logo=docker&logoColor=white)](https://www.docker.com/) diff --git a/original-labs/stable.c b/original-labs/stable.c index 80b11eb..90a12a1 100644 --- a/original-labs/stable.c +++ b/original-labs/stable.c @@ -114,7 +114,7 @@ void ComputeChecksum(struct pkt *packet) unsigned int sum = 0; sum += packet->seqnum; sum += packet->acknum; - + for(int i=0; i<20; i++) { sum += (unsigned char)packet->payload[i]; if(sum & 0xFFFF0000) { @@ -129,7 +129,7 @@ int CheckCorrupted(struct pkt packet) unsigned int sum = 0; sum += packet.seqnum; sum += packet.acknum; - + for(int i=0; i<20; i++) { sum += (unsigned char)packet.payload[i]; if(sum & 0xFFFF0000) { @@ -145,10 +145,10 @@ int CheckCorrupted(struct pkt packet) void A_output(struct msg_ message) { struct timer *pkt_timer; - + /* Create pkt before window is full */ if (nextseqnum < base + WINDOWSIZE) { - /* 创建新数据包 */ + /* Create new data packet */ struct pkt packet; packet.seqnum = nextseqnum; packet.acknum = NOTUSED; @@ -223,7 +223,7 @@ void A_input(struct pkt packet) return; // if no matching ACK found, just return } - + while (pktnum > 0 && winbuf[winfront].acknum == 1 && winbuf[winfront].seqnum == base) { base++; winfront = (winfront + 1) % WINDOWSIZE; @@ -231,7 +231,7 @@ void A_input(struct pkt packet) ack_adv_num++; } - + while (msg_num > 0 && nextseqnum < base + WINDOWSIZE) { struct msg_ message = buffer[buffront]; buffront = (buffront + 1) % MAXBUFSIZE; @@ -243,11 +243,11 @@ void A_input(struct pkt packet) memcpy(new_pkt.payload, message.data, 20); ComputeChecksum(&new_pkt); - + printf("[%.1f] A: send packet [%d] base [%d]\n", currenttime_(), nextseqnum, base); tolayer3(A, new_pkt); packet_sent++; // update packet sent counter - + winbuf[winrear] = new_pkt; winbuf[winrear].acknum = 0; /* initializing */ winrear = (winrear + 1) % WINDOWSIZE; @@ -266,15 +266,15 @@ void A_input(struct pkt packet) void A_packet_time_rinterrupt(struct timer* pkt_timer) { packet_time_out++; - + /* find pkt from buffer */ int found = 0; for(int i=0; ipkt_seq && winbuf[(winfront+i)%WINDOWSIZE].acknum == 0) { - + printf("[%.1f] A: packet [%d] time_ out, resend packet\n", currenttime_(), pkt_timer->pkt_seq); - - + + tolayer3(A, winbuf[(winfront+i)%WINDOWSIZE]); packet_sent++; // add this line packet_resent++; @@ -282,7 +282,7 @@ void A_packet_time_rinterrupt(struct timer* pkt_timer) break; } } - + if(found) { starttime_r(pkt_timer, A, RTT); } @@ -294,7 +294,7 @@ void B_input(struct pkt packet) static int expected_seq = 0; static struct pkt recv_window[8]; static int recv_marked[8] = {0}; /* 0: not marked, 1: marked */ - + if (CheckCorrupted(packet)) { if (packet.seqnum == 999999) { printf("[%.1f] B: packet [999999] corrupted\n", currenttime_()); @@ -304,35 +304,35 @@ void B_input(struct pkt packet) /* remoce packet_corrupt++,since checked in tolayer3 */ return; } - + /* 2. check */ if ((packet.seqnum >= expected_seq && packet.seqnum < expected_seq + WINDOWSIZE) || (packet.seqnum < expected_seq && packet.seqnum >= expected_seq - WINDOWSIZE)) { int slot = packet.seqnum % WINDOWSIZE; - + /* 3. buffer and sendACK */ if (recv_marked[slot] == 0 || recv_window[slot].seqnum != packet.seqnum) { /* buffer */ memcpy(&recv_window[slot], &packet, sizeof(struct pkt)); recv_marked[slot] = 1; - + /* send ack */ struct pkt ack_pkt; ack_pkt.acknum = packet.seqnum; ack_pkt.seqnum = NOTUSED; - memset(ack_pkt.payload, 0, 20); /* 清空ACK包的payload */ + memset(ack_pkt.payload, 0, 20); /* Clear ACK packet payload */ ComputeChecksum(&ack_pkt); tolayer3(B, ack_pkt); - + printf("[%.1f] B: packet [%d] received, send ACK [%d]\n", currenttime_(), packet.seqnum, packet.seqnum); } - - - while (recv_marked[expected_seq % WINDOWSIZE] == 1 && + + + while (recv_marked[expected_seq % WINDOWSIZE] == 1 && recv_window[expected_seq % WINDOWSIZE].seqnum == expected_seq) { tolayer5(B, recv_window[expected_seq % WINDOWSIZE].payload); - recv_marked[expected_seq % WINDOWSIZE] = 0; + recv_marked[expected_seq % WINDOWSIZE] = 0; packet_receieve++; expected_seq++; } @@ -344,7 +344,7 @@ void B_input(struct pkt packet) memset(ack_pkt.payload, 0, 20); ComputeChecksum(&ack_pkt); tolayer3(B, ack_pkt); - + printf("[%.1f] B: packet [%d] received, send ACK [%d]\n", currenttime_(), packet.seqnum, packet.seqnum); } @@ -381,13 +381,13 @@ void print_info() { printf("***********************************************************************************************************\n"); printf(" Packet Transmission Summary \n"); printf("***********************************************************************************************************\n"); - - /* 统计计算 - 每个包32KB,转换为Kb/s */ + + /* Statistics calculation - each packet is 32KB, convert to Kb/s */ float throughput = (packet_receieve * 32.0 * 8) / currenttime_(); - + /* 正确传输的包数量应该等于总消息数量,即接收方接收到的包数量 */ packet_correct = packet_receieve; - + printf("From Sender to Receiver: \n"); printf(" total sent pkts: %d \n", packet_sent); printf(" total correct pkts: %d \n", packet_correct); @@ -549,19 +549,19 @@ void init() /* initialize the simulator */ // float jimsrand(); printf("Enter the number of messages to simulate: \n"); - + scanf("%d", &nsimmax); total_sim_msg = nsimmax * 32; printf("Enter time_ between messages from sender's layer5 [ > 0.0]:\n"); scanf("%f", &lambda); printf("Enter channel pattern string\n"); - + scanf("%s", pattern); npttns = strlen(pattern); printf("Enter sender's window size\n"); - + scanf("%d", &WINDOWSIZE); winbuf = (struct pkt *) malloc(sizeof(struct pkt) * WINDOWSIZE);