Permalink
Browse files

Socket fixes and luascript-plugin work

  • Loading branch information...
1 parent 5e17b7b commit 642fafbf9b6f6f581a56e1de9ebcbc48747ddda8 @fador committed Aug 13, 2012
View
@@ -1,5 +1,5 @@
/*
- Copyright (c) 2011, The Mineserver Project
+ Copyright (c) 2012, The Mineserver Project
All rights reserved.
Redistribution and use in source and binary forms, with or without
@@ -29,7 +29,9 @@
We declare functions that serve as C callbacks explicitly as 'extern "C"'
to avoid any potential ABI incompatibilities.
*/
+class User;
extern "C" void accept_callback(int fd, short ev, void* arg);
extern "C" void client_callback(int fd, short ev, void* arg);
extern "C" void *user_validation_thread(void *arg);
+bool client_write(User *user);
@@ -42,6 +42,8 @@ extern "C"
#include <lualib.h>
}
+#include "luawrapper.h"
+
struct LuaScript
{
std::string script;
@@ -213,6 +215,11 @@ int lua_calltest(lua_State *pL)
return 0;
}
+void registerLuaFunctions(lua_State *pL)
+{
+ lua_register(pL, "calltest", lua_calltest);
+}
+
void luaLoad(std::string user, std::string command, std::deque<std::string> args)
{
@@ -228,11 +235,12 @@ void luaLoad(std::string user, std::string command, std::deque<std::string> args
luaScripts.push_back(LuaScript());
unsigned int last = luaScripts.size()-1;
+ //Initialize
luaScripts[last].pL = lua_open();
- luaScripts[last].script = args[0];
-
+ luaScripts[last].script = args[0];
luaL_openlibs(luaScripts[last].pL);
+ //Try to open and run
if(luaL_dofile(luaScripts[last].pL, luaScripts[last].script.c_str()) != 0)
{
lua_close(luaScripts[last].pL);
@@ -243,7 +251,7 @@ void luaLoad(std::string user, std::string command, std::deque<std::string> args
else
{
//Init stuff
- lua_register(luaScripts[last].pL, "calltest", lua_calltest);
+ registerLuaFunctions(luaScripts[last].pL);
std::string msg = MSG_PREFIX+"script "+args[0]+" loaded and running!";
mineserver->chat.sendmsgTo(user.c_str(), msg.c_str());
}
@@ -0,0 +1,49 @@
+/*
+ Copyright (c) 2012, The Mineserver Project
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+ * Neither the name of the The Mineserver Project nor the
+ names of its contributors may be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#include <iostream>
+#include <sstream>
+#include <string>
+#include <deque>
+#include <stdint.h>
+#include <cstdlib>
+#include <ctime>
+#include <map>
+#include <vector>
+
+extern "C"
+{
+#include <lua.h>
+#include <lauxlib.h>
+#include <lualib.h>
+}
+
+
+#define MINESERVER_C_API
+#include "plugin_api.h"
+
+extern mineserver_pointer_struct* mineserver;
@@ -0,0 +1,27 @@
+/*
+ Copyright (c) 2012, The Mineserver Project
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+ * Neither the name of the The Mineserver Project nor the
+ names of its contributors may be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
View
@@ -793,7 +793,7 @@ bool Mineserver::run()
tempuser->kick("User not Premium");
}
//Flush
- client_callback(tempuser->fd, EV_WRITE, tempuser);
+ client_write(tempuser);
}
}
ServerInstance->validatedUsers.clear();
@@ -816,7 +816,7 @@ bool Mineserver::run()
(*it)->sethealth((*it)->health - 5);
}
//Flush data
- client_callback((*it)->fd, EV_WRITE, (*it));
+ client_write((*it));
}
}
#ifdef WIN32
View
@@ -69,6 +69,87 @@ static uint8_t* const upBUF = BUF.data();
static char* cpBUFCRYPT = reinterpret_cast<char*>(BUFCRYPT.data());
+
+bool client_write(User *user)
+{
+ //If there is data in the output buffer, crypt it before writing
+ if (!user->buffer.getWriteEmpty())
+ {
+ std::vector<char> buf;
+
+ user->buffer.getWriteData(buf);
+
+ //More glue - Fador
+ if(user->crypted)
+ {
+ //We might have to write some data uncrypted ToDo: fix
+ if(user->uncryptedLeft)
+ {
+ user->bufferCrypted.addToWrite((uint8_t *)buf.data(),user->uncryptedLeft);
+ }
+ int p_len = buf.size()-user->uncryptedLeft, f_len = 0;
+ if(p_len)
+ {
+ uint8_t *buffer = (uint8_t *)malloc(p_len+1);
+ EVP_EncryptUpdate(&user->en, (uint8_t *)buffer, &p_len, (const uint8_t *)buf.data()+user->uncryptedLeft, buf.size()-user->uncryptedLeft);
+ int written = p_len + f_len;
+ user->bufferCrypted.addToWrite((uint8_t *)buffer,written);
+ free(buffer);
+ }
+ user->uncryptedLeft = 0;
+ }
+ else
+ {
+ user->bufferCrypted.addToWrite((uint8_t *)buf.data(),buf.size());
+ user->uncryptedLeft = 0;
+ }
+
+ //free(outBuf);
+ user->buffer.clearWrite(buf.size());
+ }
+
+ //We have crypted data ready to be written
+ if(!user->bufferCrypted.getWriteEmpty())
+ {
+ std::vector<char> buf;
+ user->bufferCrypted.getWriteData(buf);
+
+ //Try to write the whole buffer
+ const int written = send(user->fd, buf.data(), buf.size(), 0);
+
+ //Handle errors
+ if (written == SOCKET_ERROR)
+ {
+ #ifdef WIN32
+ #define ERROR_NUMBER WSAGetLastError()
+ if ((ERROR_NUMBER != WSATRY_AGAIN && ERROR_NUMBER != WSAEINTR && ERROR_NUMBER != WSAEWOULDBLOCK))
+ #else
+ #define ERROR_NUMBER errno
+ if ((errno != EAGAIN && errno != EINTR))
+ #endif
+ {
+ LOG2(ERROR, "Error writing to client, tried to write " + dtos(buf.size()) + " bytes, code: " + dtos(ERROR_NUMBER));
+ delete user;
+ return false;
+ }
+ }
+ else
+ {
+ //Remove written amount from the buffer
+ user->bufferCrypted.clearWrite(written);
+ }
+
+ //If we couldn't write everything at once, add EV_WRITE event calling this function again..
+ if (!user->bufferCrypted.getWriteEmpty())
+ {
+ event_set(user->GetEvent(), user->fd, EV_WRITE | EV_READ, client_callback, user);
+ event_add(user->GetEvent(), NULL);
+ return false;
+ }
+ }
+ return true;
+}
+
extern "C" void client_callback(int fd, short ev, void* arg)
{
User* user = reinterpret_cast<User*>(arg);
@@ -171,81 +252,11 @@ extern "C" void client_callback(int fd, short ev, void* arg)
} // while(user->buffer)
} //End reading
- //If there is data in the output buffer, crypt it before writing
- if (!user->buffer.getWriteEmpty())
- {
- std::vector<char> buf;
-
- user->buffer.getWriteData(buf);
-
- //More glue - Fador
- if(user->crypted)
- {
- //We might have to write some data uncrypted ToDo: fix
- if(user->uncryptedLeft)
- {
- user->bufferCrypted.addToWrite((uint8_t *)buf.data(),user->uncryptedLeft);
- }
- int p_len = buf.size()-user->uncryptedLeft, f_len = 0;
- if(p_len)
- {
- uint8_t *buffer = (uint8_t *)malloc(p_len+1);
- EVP_EncryptUpdate(&user->en, (uint8_t *)buffer, &p_len, (const uint8_t *)buf.data()+user->uncryptedLeft, buf.size()-user->uncryptedLeft);
- int written = p_len + f_len;
- user->bufferCrypted.addToWrite((uint8_t *)buffer,written);
- free(buffer);
- }
- user->uncryptedLeft = 0;
- }
- else
- {
- user->bufferCrypted.addToWrite((uint8_t *)buf.data(),buf.size());
- user->uncryptedLeft = 0;
- }
-
- //free(outBuf);
- user->buffer.clearWrite(buf.size());
- }
-
- //We have crypted data ready to be written
- if(!user->bufferCrypted.getWriteEmpty())
+ //Write data to user socket
+ if(!client_write(user))
{
- std::vector<char> buf;
- user->bufferCrypted.getWriteData(buf);
-
- //Try to write the whole buffer
- const int written = send(fd, buf.data(), buf.size(), 0);
-
- //Handle errors
- if (written == SOCKET_ERROR)
- {
- #ifdef WIN32
- #define ERROR_NUMBER WSAGetLastError()
- if ((ERROR_NUMBER != WSATRY_AGAIN && ERROR_NUMBER != WSAEINTR && ERROR_NUMBER != WSAEWOULDBLOCK))
- #else
- #define ERROR_NUMBER errno
- if ((errno != EAGAIN && errno != EINTR))
- #endif
- {
- LOG2(ERROR, "Error writing to client, tried to write " + dtos(buf.size()) + " bytes, code: " + dtos(ERROR_NUMBER));
- delete user;
- return;
- }
- }
- else
- {
- //Remove written amount from the buffer
- user->bufferCrypted.clearWrite(written);
- }
-
- //If we couldn't write everything at once, add EV_WRITE event calling this function again..
- if (!user->bufferCrypted.getWriteEmpty())
- {
- event_set(user->GetEvent(), fd, EV_WRITE | EV_READ, client_callback, user);
- event_add(user->GetEvent(), NULL);
- return;
- }
- }
+ return;
+ }
//Add EV_READ event again
event_set(user->GetEvent(), fd, EV_READ, client_callback, user);
@@ -61,7 +61,7 @@
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
- <AdditionalDependencies>lua51.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
+ <AdditionalDependencies>lua5.1.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@@ -80,11 +80,15 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
- <AdditionalDependencies>lua51.lib;lua5.1.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
+ <AdditionalDependencies>lua5.1.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\plugins\luascript\luascript.cpp" />
+ <ClCompile Include="..\..\plugins\luascript\luawrapper.cpp" />
+ </ItemGroup>
+ <ItemGroup>
+ <ClInclude Include="..\..\plugins\luascript\luawrapper.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

0 comments on commit 642fafb

Please sign in to comment.