3

我有一个任务,我必须实现一些网络协议。该分配包括通过理想连接(不会丢失或损坏数据)和确实损坏数据的连接进行的通信。我想让它尽可能模块化,但我不知道该怎么做(一切似乎都如此交织在一起)。这是我必须做的事情的清单:

  • 滑动窗口。该任务假设一个完美的连接。但是,我想抽象出这个细节(实现滑动窗口代码,以便处理连接质量是其他模块的责任)。
  • 用于不完美连接的Go-Back-N协议。在这里,我想重用之前涉及滑动窗口的任务中的代码。
  • 选择性重复。这听起来应该取代 Go-Back-N 模块,同时仍然在滑动窗口模块之上工作。
  • 错误检测。这个看起来应该与我是否使用滑动窗口、Go-Back-N 或选择性重复无关。然而,我不知道如何将错误检查与消息接收分开。

也许我可以实现分层架构(受 OSI 模型的启发),但我不知道该怎么做。我需要朝着正确的方向轻推。

这些不同的模块应该如何相互交互?这样 Go-Back-N 或 Selective Repeat 的功能与我是否要填充数据链接(滑动窗口)无关,并且错误检查是在所有这些之上透明地完成的。

编辑:另一个困难是某些协议(Go-Back-N、选择性重复、滑动窗口)需要特定于该协议的状态,并且在 C 中没有实现有状态功能的好方法。

4

1 回答 1

2

也许你可以使用链式函数?

最初,您可以使用、和等struct connection字段。首先,您将使用处理理想连接的函数来填充它。void (*write)(struct connection *con, char *buf, size_t len, void *data);void *write_data;int (*get_write_queue_size)(struct connection *con, void *data)void *get_write_queue_size_data

Then, for adding a sliding window, you would make a struct sliding_window_connection with all the fields from struct connection that you want to intercept. Then, you would move the old functions and data-pointers from the struct connection into the struct sliding_window_connection, replace the functions in the struct connection with the sliding window implementations and replace the data pointers in the struct connection with pointers to the struct sliding_window_connection. The new write could e.g. look somewhat like this:

void sliding_window_connection_write
      (struct connection *con, char *buf, size_t len, void *data) {
  struct sliding_window_connection *swcon = data;
  /* ... do magic for the sliding window ... */
  /* if we want the buffer to be sent now, call the lower layer like this: */
  swcon->write(con, buf, len, swcon->write_data);
}

For stacking go-back-n or selective repeat on top, you would then add the go-back-n or selective repeat functions the same way.

For reading, you would basically do the same – let the data bubble up through the layers and maybe manipulate or interpret it on the way.

To make this work better, you might want to add a function similar to write that can be used by a layer (e.g. go-back-n) to signal a lower layer (e.g. the sliding window) for purposes like "hey, please resend bytes m->n, thanks".

As error detection would have to happen on a higher level than these things, you would have to add it after the other things – the later you add something, the higher its layer.

于 2013-03-30T20:12:15.733 回答