标签:
由于python本身的线程基本上比较残废,所以为了利用机器的cpu,就不得不用上多进程。。。
在游戏服务器的设计中,最为常见的方式是:
挂一个前端服务器,专门来维护与客户端的连接,然后将客户端的请求数据转发给后端服务器。。。
上面的方式是现在最为正统的。。。
但是自己因为环境的限制,需要做到对客户端透明,然后将后端的服务器转换成为多进程的。。。所以这里就只有用一点比较别扭的方法了,首先处理登录等一些常规的逻辑放在前端服务器,当进入放进进行匹配战斗之后,将客户端的socket连接直接交给后端服务器,然后进行处理。。。。。
因此这里就需要实现一个python能用的文件描述符传递扩展库。。。。
还好自己用C语言做过类似的东西。。。所以基本上把代码拿过来,再用cython做一层包装,也就能用了,这里就直接贴代码吧:
#include <sys/socket.h> #include <stdio.h> #include <stdlib.h> #include <stddef.h> #include <sys/un.h> #include <string.h> #include <unistd.h> int serv_listen(const char *name); int send_fd(int sock, int fd, char* data); int recv_fd(int sock, char *data); void close_fd(int fd);
#include "sr.h" int sr_connect(const char *name) { int fd, size; struct sockaddr_un un; memset(&un, 0, sizeof(un)); un.sun_family = AF_UNIX; if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { printf("no1\n"); return 0; } sprintf(un.sun_path, "%s%05d", "tmp", getpid()); size = offsetof(struct sockaddr_un, sun_path) + strlen(un.sun_path); if (bind(fd, (struct sockaddr *)&un, size) < 0) { printf("no2\n"); return 0; } strcpy(un.sun_path, name); size = offsetof(struct sockaddr_un, sun_path) + strlen(un.sun_path); if (connect(fd, (struct sockaddr *)&un, size) < 0) { printf("no3\n"); return 0; } // if (listen(fd, 10) < 0) { // printf("no3"); // } // char* hello = "hello fjs"; // ssize_t out = send(fd, (void*)hello, strlen(hello), 0); // send_fd(lis, lis); return fd; } int send_fd(int sock, int fd, char* data) { printf("here1: %s\n", data); struct iovec iov[1]; iov[0].iov_base = data; iov[0].iov_len = strlen(data); printf("len: %d\n", strlen(data)); printf("here2: %s\n", iov[0].iov_base); int cmsgsize = CMSG_LEN(sizeof(int)); struct cmsghdr* cmptr = (struct cmsghdr*)malloc(cmsgsize); if(cmptr == NULL){ return -1; } cmptr->cmsg_level = SOL_SOCKET; cmptr->cmsg_type = SCM_RIGHTS; // we are sending fd. cmptr->cmsg_len = cmsgsize; struct msghdr msg; msg.msg_iov = iov; msg.msg_iovlen = 1; msg.msg_name = NULL; msg.msg_namelen = 0; msg.msg_control = cmptr; msg.msg_controllen = cmsgsize; *(int *)CMSG_DATA(cmptr) = fd; int ret = sendmsg(sock, &msg, 0); free(cmptr); if (ret == -1){ return -1; } return 0; } int recv_fd(int sock, char* data) { int cmsgsize = CMSG_LEN(sizeof(int)); struct cmsghdr* cmptr = (struct cmsghdr*)malloc(cmsgsize); if (cmptr == NULL) { return -1; } char buf[33]; // the max buf in msg. memset(buf, 0, 33); struct iovec iov[1]; iov[0].iov_base = buf; iov[0].iov_len = sizeof(buf); struct msghdr msg; msg.msg_iov = iov; msg.msg_iovlen = 1; msg.msg_name = NULL; msg.msg_namelen = 0; msg.msg_control = cmptr; msg.msg_controllen = cmsgsize; int ret = recvmsg(sock, &msg, 0); if (ret == -1) { return -1; } int fd = *(int *)CMSG_DATA(cmptr); strcpy(data, iov[0].iov_base); free(cmptr); return fd; } void close_fd(int fd) { close(fd); } // int main() // { // int lis = serv_listen("/tmp/fjs.sock"); // printf("ok\n"); // char* hello = "hello fjs"; // // ssize_t out = send(lis, (void*)hello, strlen(hello), 0); // send_fd(lis, lis); // }
最后贴上cython的包装文件:
cdef extern from "sr.h": extern int sr_connect(const char *name) extern int send_fd(int sock, int fd, char* data) extern int recv_fd(int sock, char* data) void close_fd(int fd) cdef extern from "stdlib.h": extern void *malloc(unsigned int num_bytes) extern void free(void *ptr) def connect_and_send(): cdef char* dist = "/tmp/fjs.sock" cdef int fd = sr_connect(dist) send_fd(fd, fd, "fdsaf"); def fjs_recv_fd(sock): cdef int fd = sock cdef char* data = <char*>malloc(33) fd = recv_fd(fd, data) try: out_data = data return (fd, out_data) finally: free(data) def fjs_send_fd(fd1, fd2, data): cdef int source = fd1 cdef int des = fd2 send_fd(source, des, data) def fjs_close_fd(fd): cdef int now_fd = fd close_fd(fd)
嗯。。就上面这些代码。。。这里需要基于unix域socket。。。。
最后吐槽一下。。。。python真他妈的慢。。。。
标签:
原文地址:http://blog.csdn.net/fjslovejhl/article/details/45815331