我们有一个定义为C ++类的应用程序协议,它们通过网络传输。我想连接到以这种格式发送数据的服务器。我想在lisp中编写一个客户端(首选sbcl)与该服务器通信。我希望它用纯lisp编写而不是使用CFFI来包装C ++ dll。样本结构看起来像这样:
class Header
{
public:
int MsgType;
uint64_t Length;
}
class SampleMsg
{
public:
Header MsgHeader;
char Field1[256];
bool Field2;
double Field3;
SomeOtherClass Field4;
}
我想知道如何在lisp中映射这些结构,以便它们是二进制兼容的以及如何读/写这样的结构。有没有比在结构中打包/解包每个字段更简单的方法?
例如,在C#中,您可以映射二进制结构,如下所示,并直接从字节数组中读取它:
[StructLayout(LayoutKind.Sequential)]
public struct Header
{
public int MsgType;
public ulong Length;
}
[StructLayout(LayoutKind.Sequential)]
public struct SampleMsg
{
public:
public Header MsgHeader;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string Field1;
public bool Field2;
public double Field3;
public SomeOtherClass Field4;
}
如果在lisp中可以使用类似的方法,那么它将是理想的。如果没有,我愿意做一些管道,只要它是可管理的。
编辑:
试过Svante的建议:
(ql:quickload "userial")
(in-package :sb-bsd-sockets)
(defun read-buffer (host port)
(let ((socket (make-instance 'inet-socket :type :stream :protocol :tcp)))
(socket-connect socket host port)
(let ((buf (socket-receive socket nil 1024 :element-type '(unsigned-byte 8))))
(socket-close socket)
buf)))
(defstruct header
msg-type
length)
(userial:make-slot-serializer (:header header (make-header))
:int64 msg-type
:uint64 length)
(defvar *buffer*)
(defvar *b*)
(setq *buffer* (read-buffer #(10 1 2 75) 5003))
(setq *b* (make-array 2048 :element-type '(unsigned-byte 8) :fill-pointer 0 :adjustable t))
(map 'vector #'(lambda (x) (vector-push x *b*)) *buffer*)
(setf (fill-pointer *b*) 0)
此时,*b*
包含以下内容:#(7 0 0 0 0 0 0 0 176 2 0 0 0 0 0 0 45 71 253 83 0 0 0 0 165 30 11 11 0 0 0 ...)
。第一个7对应于msg类型,应该是7.长度应该是688(176 + 2 * 256)。
现在我做
(userial:with-buffer *b* (userial:unserialize :header))
。这给了我
#S(HEADER :MSG-TYPE 504403158265495552 :LENGTH 12682699500628738048)
#(7 0 0 0 0 0 0 0 176 2 0 0 0 0 0 0)
似乎是一个字节序问题。如何解决这个问题?我找不到任何方法来处理userial lib中的字节序。
EDIT2:
最后最终放弃了使用和写作这些(遵循Practical Common Lisp一书):
(defun read-64 (buf)
(let ((u 0))
(setf (ldb (byte 8 56) u) (aref buf 7))
(setf (ldb (byte 8 48) u) (aref buf 6))
(setf (ldb (byte 8 40) u) (aref buf 5))
(setf (ldb (byte 8 32) u) (aref buf 4))
(setf (ldb (byte 8 24) u) (aref buf 3))
(setf (ldb (byte 8 16) u) (aref buf 2))
(setf (ldb (byte 8 8) u) (aref buf 1))
(setf (ldb (byte 8 0) u) (aref buf 0))
u))
(defun read-32 (buf)
(let ((u 0))
(setf (ldb (byte 8 24) u) (aref buf 3))
(setf (ldb (byte 8 16) u) (aref buf 2))
(setf (ldb (byte 8 8) u) (aref buf 1))
(setf (ldb (byte 8 0) u) (aref buf 0))
u))
(defun read-16 (buf)
(let ((u 0))
(setf (ldb (byte 8 8) u) (aref buf 1))
(setf (ldb (byte 8 0) u) (aref buf 0))
u))
现在我可以写(read-uint64 (subseq *buffer* 8 16))
来获取msg的长度。感谢您的帮助。
答案 0 :(得分:3)
您可以使用Quicklisp提供的userial
。
但是,我很难找到一种方法来消除保持两个定义位置同步的需要(一个在C ++上,一个在Lisp端)。
编辑:以下是我的想法。我只进行了一些非常浅的测试,所以没有保证。特别是,我还没有使用C ++输出进行测试,你很可能不得不调整很多用于对齐。
(defstruct header
msg-type
length)
;; Msg-type might be best handled with an enum unserializer:
;; (make-enum-unserializer :msg-type (:foo :bar)), but I don't know
;; what your values are.
(defstruct sample-msg
msg-header
field-1
field-2
field-3
field-4)
;; You might need to use a different serializer for msg-type for
;; alignment.
(make-slot-serializer (:header header (make-header))
:int msg-type
:uint64 length)
(make-vector-serializer :vector-256-char :uint8 256)
;; I have no idea how a boolean is serialized and aligned on the C++
;; side, so I'll just use :boolean for field-3 here as a first
;; attempt.
(make-slot-serializer (:sample-msg sample-msg (make-sample-msg))
:header msg-header
:vector-256-char field-1
:boolean field-2
:float64 field-3
:some-other field-4)
;; You can serialize and unserialize now:
(serialize :sample-msg some-sample-msg)
(rewind-buffer)
(unserialize :sample-msg)
;; Userial operates on an adjustable vector with fill-pointer in the
;; special variable *buffer*, so you'll need to fill that with content
;; from wherever you read that from.
(with-buffer (read-my-content)
(unserialize :sample-msg))