Topics |
Note: LuaSocket is copyright © 2004-2007 Diego Nehab, and available under the terms of the MIT liscence.
LuaSocket adds socket programming functionality to Lua, including TCP, UDP, and higher-level protocols including HTTP, FTP, and SMTP. The higher-level protocols are implemented in pure Lua code, but the raw socket functionality is built into a C Module, which must be compiled for XLua.
These instructions will walk you through the process of building and testing the LuaSocket package.
Before you begin
All links are active as of first publication of this guide, but may change in the future. Likewise, future changes to Lua, XLua, or LuaSocket may render some or all of this guide invalid.
Getting started
If the goal is to build LuaSocket from source, then why did I ask you to download the Windows binary as well? The binary form of a Lua package may organize files in a specific hierarchy that is not obvious in the source code, or may provide files (such as Lua files) that aren't present in the source code at all. At least the former is true in this case. It's easier to use a package if you follow its prescribed organization.
Create a new empty folder somewhere on your disk. This is where we'll create our test MMF application. You can also re-use another folder if that suits you. Inside this folder, create a new "ext" folder if it doesn't exist. Now unzip the LuaSocket Windows binary package, and copy the contents into the ext folder. You can delete lua51.exe and lua51.dll, as they won't be used.
Notice how LuaSocket organizes its files. The lua directory contains all the Lua scripts that make up part of the package, while the other two directories each contain a different copy of core.dll. It's those two copies of core.dll we must now replace before building our XLua application.
Before moving on, make sure you've unzipped the Lua source code and LuaSocket source code to your hard drive somewhere.
Visual C++
LuaSocket already comes with Visual C++ project files, but chances are you will be required to run a conversion wizard when you open them. Some warnings or errors may be reported during this process, but can probably be ignored. Specifically, expect an error related to "libluasocket.vcproj", but it doesn't actually exist and thus does not concern us.
Your solution explorer should look something like this. Note that there are two projects listed: one for each of the two core.dll files we need to replace. Also notice that listed with each project is a reference to lua5.1.dll.lib. These "files" are really a reference to the relative path "..\..\lib\lua5.1.dll.lib", which would have been provided by the official Lua binary distribution, which we don't have. But we need to replace these references with xlua.lib instead. Select each lua5.1.dll.lib and delete it from the solution explorer. Now locate your copy of xlua.lib, and drag it onto the "mime" and "socket" project icons to add it to the projects. Alternatively, you can right click, select "Add -> Existing Item", and insert xlua.lib that way.
As an aside, other projects may list lib files in the project's properties window, under "Linker -> Input", with path(s) to the lib files under "Linker -> General". Select "C\C++", and the first property listed on the right pane will be "Additional Include Directories". Change the value to point to the /src directory of the Lua source code.
Once you've updated both projects, you can go ahead and build them (build solution). Check the output log on the bottom to make sure there are no errors (warnings are okay).
In the LuaSocket /src directory, you should now be able to find a copy of socket.dll and mime.dll. Use these to replace the two copies of core.dll (rename the files to core.dll after deleting the old ones).
Testing
Create a new MFA in your test directory, drop an XLua object on the frame, open the script editor, and add the following code:
Also check the "run script at start of frame" box. The rest of your application only needs events to intercept XLua print and error events, and write them to an edit box. If everything is working, you should see a list of track titles printed out, fetched from freedb.
If you have trouble, a slightly modified version of this example is provided with the XLua distribution as socket.mfa, along with a compatible copy of LuaSocket. No, your efforts wern't wasted, this is valuable practice for compiling other Lua packages that haven't already been done for you!
If you're interested in using LuaSocket, full API reference and additional examples can be found at the LuaSocket page.
Copyright 2010 Justin Aquadro