1

我听说在设计 BSD 套接字库时,C 编程缺少某种模式或特性,这导致了我们今天的设计。

例如,当传递struct socketaddr_instruct socketaddr系统调用(如bind.

从 GNU libc 头文件:

/* POSIX.1g specifies this type name for the `sa_family' member.  */
    typedef unsigned short int sa_family_t;

/* This macro is used to declare the initial common members
   of the data types used for socket addresses, `struct sockaddr',
   `struct sockaddr_in', `struct sockaddr_un', etc.  */

#define __SOCKADDR_COMMON(sa_prefix) \
  sa_family_t sa_prefix##family

#define __SOCKADDR_COMMON_SIZE  (sizeof (unsigned short int))

/* Structure describing a generic socket address.  */
struct sockaddr
  {
    __SOCKADDR_COMMON (sa_);    /* Common data: address family and length.  */
    char sa_data[14];       /* Address data.  */
  };

/* Structure describing an Internet socket address.  */
struct sockaddr_in
  {
    __SOCKADDR_COMMON (sin_);
    in_port_t sin_port;         /* Port number.  */
    struct in_addr sin_addr;        /* Internet address.  */

    /* Pad to size of `struct sockaddr'.  */
    unsigned char sin_zero[sizeof (struct sockaddr) -
               __SOCKADDR_COMMON_SIZE -
               sizeof (in_port_t) -
               sizeof (struct in_addr)];
  };

/* Internet address.  */
typedef uint32_t in_addr_t;
struct in_addr
  {
    in_addr_t s_addr;
  };

一个示例程序:

int main(int argc, char *argv[]) {
    ...

    // Initialize the server address
    struct sockaddr_in serv_addr;
    bzero((char *)&serv_addr, sizeof(serv_addr));

    serv_addr = (struct sockaddr_in) {
        .sin_port = htons(port),
        .sin_family = AF_INET ,
        .sin_addr.s_addr = INADDR_ANY
    };

    // Bind the server address to the TCP/IP socket
    int status = bind(server_socket, (struct sockaddr *)&serv_addr, sizeof(serv_addr));

    ...
}

我想不出这个通用接口还能如何到位。有任何想法吗?

4

2 回答 2

3

您可以使用委托模式来支持 C 中的一种继承形式。它看起来很漂亮,它是通用的,但它很重:有很多潜在的故障点,需要一些堆分配等。我猜他们不想操作系统 API 有这种权重吗?

这是一个例子

struct ShapeDelegate {
  double (*getSurfaceArea)(void* data);
  int (*isInside)(void* data, double x, double y);
  void (*destroy)(void* data);
}

struct Shape {
  void* data;
  struct ShapeDelegate delegate;
}

double Shape_getSurfaceArea(Shape* self) {
  return self->delegate.getSurfaceArea(self->data);
}

int Shape_isInside(Shape* self, double x, double y) {
  return self->delegate.isInside(self->data, x, y);
}

void Shape_destroy(Shape* self) {
  if (self->delegate.destroy != NULL)
    self->delegate.destroy(self->data);
}

现在,比如说,你想要一个 Circle 作为 Shape 的实现

struct CircleData {
  double x, y, r;
}

double Circle_getSurfaceArea(void* data) {
  CircleData* self = (CircleData*)data;
  return 2 * M_PI * self->r * self->r;
}

int Circle_isInside(void* data, double x, double y) {
  double dist;
  CircleData* self = (CircleData*)data;
  dist = sqrt(sqr(x - self->x) + sqr(y - self->y)); 
  return dist < self->r;
}

void Circle_destroy(void* data) {
  free(data);
}

struct ShapeDelegate Circle_ShapeDelegate {
   Circle_getSurfaceArea,
   Circle_isInside,
   Circle_destroy
};

你需要某种构造函数

void
Shape_initAsCircle(Shape* self, double x, double y, double r) {
  CircleData* data;

  data = (CircleData*)malloc(sizeof(CircleData));
  data->x = x, data->y = y, data->r = r;

  self->data = data;
  self.delegate = Circle_ShapeDelegate;
}
于 2013-06-01T00:30:51.767 回答
1

我不会说在设计 BSD 套接字库时 C 缺少子类化模式。相反,BSD 套接字 API 设计者专门利用了这种模式。将结构转换为基本结构(具有更少字段的更通用结构)并返回到专用结构是非常常见且完全受支持的。C 甚至有一些保证以确保其可移植性:如果结构定义的第一个元素是另一个结构,C 保证封闭结构的第一个成员的类型、内存地址和布局与类型、内存相同封闭结构的地址和布局转换为其第一个成员的类型。

于 2013-06-01T00:28:19.960 回答