mirror of
https://github.com/clearlinux/uwsgi.git
synced 2026-09-06 21:51:30 +00:00
shared memory base implementation
This commit is contained in:
+6
-3
@@ -12,8 +12,8 @@ PROGRAM=uwsgi
|
||||
|
||||
all: uwsgi
|
||||
|
||||
uwsgi: utils.o socket.o main.o
|
||||
$(CC) $(LD_FLAGS) utils.o socket.o main.o -o $(PROGRAM)
|
||||
uwsgi: utils.o socket.o pymodule.o main.o
|
||||
$(CC) $(LD_FLAGS) utils.o socket.o pymodule.o main.o -o $(PROGRAM)
|
||||
|
||||
utils.o: utils.c
|
||||
$(CC) -c $(CFLAGS) utils.c
|
||||
@@ -21,8 +21,11 @@ utils.o: utils.c
|
||||
socket.o: socket.c
|
||||
$(CC) -c $(CFLAGS) socket.c
|
||||
|
||||
pymodule.o: uwsgi_pymodule.c
|
||||
$(CC) -c $(CFLAGS) -o pymodule.o uwsgi_pymodule.c
|
||||
|
||||
main.o: uwsgi.c
|
||||
$(CC) -c $(CFLAGS) -o main.o uwsgi.c
|
||||
|
||||
clean:
|
||||
rm -f utils.o socket.o main.o
|
||||
rm -f utils.o socket.o pymodule.o main.o
|
||||
|
||||
@@ -12,8 +12,8 @@ PROGRAM=uwsgi
|
||||
|
||||
all: uwsgi
|
||||
|
||||
uwsgi: utils.o socket.o main.o
|
||||
$(CC) $(LD_FLAGS) utils.o socket.o main.o -o $(PROGRAM)
|
||||
uwsgi: utils.o socket.o pymodule.o main.o
|
||||
$(CC) $(LD_FLAGS) utils.o socket.o pymodule.o main.o -o $(PROGRAM)
|
||||
|
||||
utils.o: utils.c
|
||||
$(CC) -c $(CFLAGS) utils.c
|
||||
@@ -21,8 +21,11 @@ utils.o: utils.c
|
||||
socket.o: socket.c
|
||||
$(CC) -c $(CFLAGS) socket.c
|
||||
|
||||
pymodule.o: uwsgi_pymodule.c
|
||||
$(CC) -c $(CFLAGS) -o pymodule.o uwsgi_pymodule.c
|
||||
|
||||
main.o: uwsgi.c
|
||||
$(CC) -c $(CFLAGS) -o main.o uwsgi.c
|
||||
|
||||
clean:
|
||||
rm -f utils.o socket.o main.o
|
||||
rm -f utils.o socket.o pymodule.o main.o
|
||||
|
||||
+22
-2
@@ -5,7 +5,27 @@ PYTHON_LIBS=`python2.4-config --libs`
|
||||
XML_CFLAGS=`xml2-config --cflags`
|
||||
XML_LIBS=`xml2-config --libs`
|
||||
|
||||
CFLAGS=$(PYTHON_CFLAGS) $(XML_CFLAGS)
|
||||
LD_FLAGS=$(PYTHON_LIBS) $(XML_LIBS)
|
||||
|
||||
PROGRAM=uwsgi24
|
||||
|
||||
all: uwsgi
|
||||
|
||||
uwsgi:
|
||||
$(CC) -o uwsgi24 $(PYTHON_CFLAGS) $(PYTHON_LIBS) $(XML_CFLAGS) $(XML_LIBS) utils.c socket.c uwsgi.c
|
||||
uwsgi: utils.o socket.o pymodule.o main.o
|
||||
$(CC) $(LD_FLAGS) utils.o socket.o pymodule.o main.o -o $(PROGRAM)
|
||||
|
||||
utils.o: utils.c
|
||||
$(CC) -c $(CFLAGS) utils.c
|
||||
|
||||
socket.o: socket.c
|
||||
$(CC) -c $(CFLAGS) socket.c
|
||||
|
||||
pymodule.o: uwsgi_pymodule.c
|
||||
$(CC) -c $(CFLAGS) -o pymodule.o uwsgi_pymodule.c
|
||||
|
||||
main.o: uwsgi.c
|
||||
$(CC) -c $(CFLAGS) -o main.o uwsgi.c
|
||||
|
||||
clean:
|
||||
rm -f utils.o socket.o pymodule.o main.o
|
||||
|
||||
+6
-3
@@ -12,8 +12,8 @@ PROGRAM=uwsgi
|
||||
|
||||
all: uwsgi
|
||||
|
||||
uwsgi: utils.o socket.o main.o
|
||||
$(CC) $(LD_FLAGS) utils.o socket.o main.o -o $(PROGRAM)
|
||||
uwsgi: utils.o socket.o pymodule.o main.o
|
||||
$(CC) $(LD_FLAGS) utils.o socket.o pymodule.o main.o -o $(PROGRAM)
|
||||
|
||||
utils.o: utils.c
|
||||
$(CC) -c $(CFLAGS) utils.c
|
||||
@@ -21,8 +21,11 @@ utils.o: utils.c
|
||||
socket.o: socket.c
|
||||
$(CC) -c $(CFLAGS) socket.c
|
||||
|
||||
pymodule.o: uwsgi_pymodule.c
|
||||
$(CC) -c $(CFLAGS) -o pymodule.o uwsgi_pymodule.c
|
||||
|
||||
main.o: uwsgi.c
|
||||
$(CC) -c $(CFLAGS) -o main.o uwsgi.c
|
||||
|
||||
clean:
|
||||
rm -f utils.o socket.o main.o
|
||||
rm -f utils.o socket.o pymodule.o main.o
|
||||
|
||||
+6
-3
@@ -12,8 +12,8 @@ PROGRAM=uwsgi26
|
||||
|
||||
all: uwsgi
|
||||
|
||||
uwsgi: utils.o socket.o main.o
|
||||
$(CC) $(LD_FLAGS) utils.o socket.o main.o -o $(PROGRAM)
|
||||
uwsgi: utils.o socket.o pymodule.o main.o
|
||||
$(CC) $(LD_FLAGS) utils.o socket.o pymodule.o main.o -o $(PROGRAM)
|
||||
|
||||
utils.o: utils.c
|
||||
$(CC) -c $(CFLAGS) utils.c
|
||||
@@ -21,8 +21,11 @@ utils.o: utils.c
|
||||
socket.o: socket.c
|
||||
$(CC) -c $(CFLAGS) socket.c
|
||||
|
||||
pymodule.o: uwsgi_pymodule.c
|
||||
$(CC) -c $(CFLAGS) -o pymodule.o uwsgi_pymodule.c
|
||||
|
||||
main.o: uwsgi.c
|
||||
$(CC) -c $(CFLAGS) -o main.o uwsgi.c
|
||||
|
||||
clean:
|
||||
rm -f utils.o socket.o main.o
|
||||
rm -f utils.o socket.o pymodule.o main.o
|
||||
|
||||
@@ -10,8 +10,8 @@ PROGRAM=uwsgi26_rs
|
||||
|
||||
all: uwsgi
|
||||
|
||||
uwsgi: utils.o socket.o main.o
|
||||
$(CC) $(LD_FLAGS) utils.o socket.o main.o -o $(PROGRAM)
|
||||
uwsgi: utils.o socket.o pymodule.o main.o
|
||||
$(CC) $(LD_FLAGS) utils.o socket.o pymodule.o main.o -o $(PROGRAM)
|
||||
|
||||
utils.o: utils.c
|
||||
$(CC) -c $(CFLAGS) utils.c
|
||||
@@ -19,8 +19,11 @@ utils.o: utils.c
|
||||
socket.o: socket.c
|
||||
$(CC) -c $(CFLAGS) socket.c
|
||||
|
||||
pymodule.o: uwsgi_pymodule.c
|
||||
$(CC) -c $(CFLAGS) -o pymodule.o uwsgi_pymodule.c
|
||||
|
||||
main.o: uwsgi.c
|
||||
$(CC) -c $(CFLAGS) -o main.o uwsgi.c
|
||||
|
||||
clean:
|
||||
rm -f utils.o socket.o main.o
|
||||
rm -f utils.o socket.o pymodule.o main.o
|
||||
|
||||
@@ -143,7 +143,8 @@ struct wsgi_request wsgi_req;
|
||||
|
||||
struct timeval start_of_uwsgi ;
|
||||
|
||||
char *sharedarea ;
|
||||
extern PyMethodDef *uwsgi_methods ;
|
||||
extern PyMethodDef *null_methods ;
|
||||
|
||||
#ifndef UNBIT
|
||||
#ifndef ROCK_SOLID
|
||||
@@ -578,12 +579,14 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
int socket_type;
|
||||
socklen_t socket_type_len;
|
||||
|
||||
sharedareasize = 0 ;
|
||||
|
||||
gettimeofday(&start_of_uwsgi, NULL) ;
|
||||
|
||||
setlinebuf(stdout);
|
||||
|
||||
cwd = uwsgi_get_cwd();
|
||||
binary_path = malloc(strlen(argv[0])) ;
|
||||
binary_path = malloc(strlen(argv[0])+1) ;
|
||||
if (binary_path == NULL) {
|
||||
perror("malloc()");
|
||||
exit(1);
|
||||
@@ -603,12 +606,12 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
|
||||
#ifndef UNBIT
|
||||
#ifndef ROCK_SOLID
|
||||
while ((i = getopt (argc, argv, "s:p:t:x:d:l:O:v:b:mcaCTPiMhrR:z:w:j:H:")) != -1) {
|
||||
while ((i = getopt (argc, argv, "s:p:t:x:d:l:O:v:b:mcaCTPiMhrR:z:w:j:H:A:")) != -1) {
|
||||
#else
|
||||
while ((i = getopt (argc, argv, "s:p:t:d:l:v:b:aCMhrR:z:j:H:")) != -1) {
|
||||
while ((i = getopt (argc, argv, "s:p:t:d:l:v:b:aCMhrR:z:j:H:A:")) != -1) {
|
||||
#endif
|
||||
#else
|
||||
while ((i = getopt (argc, argv, "p:t:mTPiv:b:rMR:Sz:w:C:j:H:")) != -1) {
|
||||
while ((i = getopt (argc, argv, "p:t:mTPiv:b:rMR:Sz:w:C:j:H:A:")) != -1) {
|
||||
#endif
|
||||
switch(i) {
|
||||
case 'j':
|
||||
@@ -617,6 +620,9 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
case 'H':
|
||||
pyhome = optarg;
|
||||
break;
|
||||
case 'A':
|
||||
sharedareasize = atoi(optarg);
|
||||
break;
|
||||
#ifdef UNBIT
|
||||
case 'S':
|
||||
single_interpreter = 1;
|
||||
@@ -779,16 +785,60 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
#else
|
||||
Py_SetProgramName("uWSGI");
|
||||
#endif
|
||||
Py_Initialize() ;
|
||||
|
||||
Py_Initialize() ;
|
||||
|
||||
|
||||
wsgi_spitout = PyCFunction_New(uwsgi_spit_method,NULL) ;
|
||||
wsgi_writeout = PyCFunction_New(uwsgi_write_method,NULL) ;
|
||||
|
||||
if (sharedareasize > 0) {
|
||||
sharedareamutex = mmap(NULL, sizeof(pthread_mutexattr_t) + sizeof(pthread_mutex_t), PROT_READ|PROT_WRITE , MAP_SHARED|MAP_ANONYMOUS , -1, 0);
|
||||
if (!sharedareamutex) {
|
||||
perror("mmap()");
|
||||
exit(1);
|
||||
}
|
||||
sharedarea = mmap(NULL, getpagesize() * sharedareasize, PROT_READ|PROT_WRITE , MAP_SHARED|MAP_ANONYMOUS , -1, 0);
|
||||
if (sharedarea) {
|
||||
fprintf(stderr,"shared area mapped at %p, you can access it with uwsgi.sharedarea_read(ptr,[len])\n", sharedarea);
|
||||
if (pthread_mutexattr_init((pthread_mutexattr_t *)sharedareamutex)) {
|
||||
fprintf(stderr,"unable to allocate mutexattr structure\n");
|
||||
exit(1);
|
||||
}
|
||||
if (pthread_mutexattr_setpshared((pthread_mutexattr_t *)sharedareamutex, PTHREAD_PROCESS_SHARED)) {
|
||||
fprintf(stderr,"unable to share mutex\n");
|
||||
exit(1);
|
||||
}
|
||||
if (pthread_mutex_init((pthread_mutex_t *) sharedareamutex + sizeof(pthread_mutexattr_t), (pthread_mutexattr_t *)sharedareamutex)) {
|
||||
fprintf(stderr,"unable to initialize mutex\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
perror("mmap()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
uwsgi_module = Py_InitModule("uwsgi", uwsgi_methods);
|
||||
if (uwsgi_module == NULL) {
|
||||
fprintf(stderr,"could not initialize the uwsgi python module\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
else {
|
||||
uwsgi_module = Py_InitModule("uwsgi", null_methods);
|
||||
if (uwsgi_module == NULL) {
|
||||
fprintf(stderr,"could not initialize the uwsgi python module\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef ROCK_SOLID
|
||||
|
||||
|
||||
wi = malloc(sizeof(struct uwsgi_app));
|
||||
if (wi == NULL) {
|
||||
perror("malloc()");
|
||||
@@ -874,6 +924,8 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifndef ROCK_SOLID
|
||||
if (single_interpreter == 1) {
|
||||
init_uwsgi_vars();
|
||||
@@ -936,6 +988,8 @@ int main(int argc, char *argv[], char *envp[]) {
|
||||
|
||||
fprintf(stderr,"request/response buffer (%d bytes) allocated.\n", buffer_size);
|
||||
|
||||
|
||||
|
||||
/* preforking() */
|
||||
if (master_process == 0) {
|
||||
fprintf(stderr, "spawned uWSGI worker 0 (pid: %d)\n", mypid);
|
||||
|
||||
BIN
Binary file not shown.
@@ -9,6 +9,8 @@
|
||||
#include <sys/types.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
|
||||
#include <poll.h>
|
||||
#include <sys/uio.h>
|
||||
@@ -16,6 +18,7 @@
|
||||
#include <sys/un.h>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#ifdef UNBIT
|
||||
#undef _XOPEN_SOURCE
|
||||
@@ -154,3 +157,9 @@ void uwsgi_xml_config(void);
|
||||
#ifndef ROCK_SOLID
|
||||
void uwsgi_wsgi_config(void);
|
||||
#endif
|
||||
|
||||
|
||||
char *sharedarea ;
|
||||
void *sharedareamutex ;
|
||||
int sharedareasize ;
|
||||
PyObject *uwsgi_module;
|
||||
|
||||
+236
@@ -0,0 +1,236 @@
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.*;
|
||||
import java.util.Enumeration;
|
||||
import org.apache.catalina.util.IOTools;
|
||||
|
||||
public class uwsgi extends HttpServlet {
|
||||
|
||||
private String mountpoint = null;
|
||||
|
||||
public void init(ServletConfig config) throws ServletException {
|
||||
super.init(config);
|
||||
|
||||
String servletName = getServletConfig().getServletName();
|
||||
if (servletName == null)
|
||||
servletName = "";
|
||||
if (servletName.startsWith("org.apache.catalina.INVOKER."))
|
||||
throw new UnavailableException
|
||||
("Cannot invoke uWSGIServlet through the invoker");
|
||||
|
||||
if (getServletConfig().getInitParameter("mountpoint") != null) {
|
||||
mountpoint = getServletConfig().getInitParameter("mountpoint");
|
||||
if (mountpoint.length() <= 1) {
|
||||
mountpoint = null ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||
throws IOException, ServletException {
|
||||
uWSGIHandler(request, response);
|
||||
}
|
||||
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||
throws IOException, ServletException {
|
||||
uWSGIHandler(request, response);
|
||||
}
|
||||
|
||||
|
||||
private void uWSGIHandler(HttpServletRequest request, HttpServletResponse response)
|
||||
throws IOException, ServletException {
|
||||
|
||||
Socket client = new Socket("localhost",3017);
|
||||
ByteArrayOutputStream uwsgi_request = new ByteArrayOutputStream() ;
|
||||
DataOutputStream wos = new DataOutputStream(uwsgi_request);
|
||||
|
||||
DataOutputStream os = new DataOutputStream(client.getOutputStream());
|
||||
DataInputStream is = new DataInputStream( client.getInputStream() );
|
||||
|
||||
|
||||
ServletOutputStream out = response.getOutputStream();
|
||||
|
||||
boolean hasBody = false;
|
||||
|
||||
wos.writeShort(swapShort( (short) 14));
|
||||
wos.writeBytes("REQUEST_METHOD");
|
||||
wos.writeShort(swapShort( (short) request.getMethod().length() ) );
|
||||
wos.writeBytes( request.getMethod() );
|
||||
|
||||
wos.writeShort(swapShort( (short) 12));
|
||||
wos.writeBytes("QUERY_STRING");
|
||||
if (request.getQueryString() != null) {
|
||||
wos.writeShort(swapShort( (short) request.getQueryString().length() ) );
|
||||
wos.writeBytes( request.getQueryString() );
|
||||
}
|
||||
else {
|
||||
wos.writeShort(0);
|
||||
}
|
||||
|
||||
wos.writeShort(swapShort( (short) 11));
|
||||
wos.writeBytes("SERVER_NAME");
|
||||
wos.writeShort(swapShort( (short) request.getServerName().length() ) );
|
||||
wos.writeBytes( request.getServerName() );
|
||||
|
||||
wos.writeShort(swapShort( (short) 11));
|
||||
wos.writeBytes("SERVER_PORT");
|
||||
wos.writeShort(swapShort( (short) Integer.toString(request.getServerPort()).length() ) );
|
||||
wos.writeBytes( Integer.toString(request.getServerPort()) );
|
||||
|
||||
wos.writeShort(swapShort( (short) 15));
|
||||
wos.writeBytes("SERVER_PROTOCOL");
|
||||
wos.writeShort(swapShort( (short) request.getProtocol().length() ) );
|
||||
wos.writeBytes( request.getProtocol() );
|
||||
|
||||
wos.writeShort(swapShort( (short) 11));
|
||||
wos.writeBytes("REQUEST_URI");
|
||||
if (request.getQueryString() != null) {
|
||||
if (request.getQueryString().length() > 0) {
|
||||
wos.writeShort(swapShort( (short) (request.getRequestURI().length()+1+request.getQueryString().length()) ) );
|
||||
wos.writeBytes( request.getRequestURI()+"?"+request.getQueryString() );
|
||||
}
|
||||
else {
|
||||
wos.writeShort(swapShort( (short) request.getRequestURI().length() ) );
|
||||
wos.writeBytes( request.getRequestURI() );
|
||||
}
|
||||
}
|
||||
else {
|
||||
wos.writeShort(swapShort( (short) request.getRequestURI().length() ) );
|
||||
wos.writeBytes( request.getRequestURI() );
|
||||
}
|
||||
|
||||
|
||||
if (mountpoint != null) {
|
||||
wos.writeShort(swapShort( (short) 11));
|
||||
wos.writeBytes("SCRIPT_NAME");
|
||||
wos.writeShort(swapShort( (short) mountpoint.length() ) ) ;
|
||||
wos.writeBytes( mountpoint ) ;
|
||||
}
|
||||
|
||||
|
||||
wos.writeShort(swapShort( (short) 9));
|
||||
wos.writeBytes("PATH_INFO");
|
||||
if (mountpoint != null) {
|
||||
wos.writeShort(swapShort( (short) (request.getRequestURI().length() - mountpoint.length()) ) );
|
||||
wos.writeBytes( request.getRequestURI().substring(mountpoint.length()) );
|
||||
}
|
||||
else {
|
||||
wos.writeShort(swapShort( (short) request.getRequestURI().length() ) );
|
||||
wos.writeBytes( request.getRequestURI() );
|
||||
}
|
||||
|
||||
wos.writeShort(swapShort( (short) 11));
|
||||
wos.writeBytes("REMOTE_ADDR");
|
||||
wos.writeShort(swapShort( (short) request.getRemoteAddr().length() ) );
|
||||
wos.writeBytes( request.getRemoteAddr() );
|
||||
|
||||
if (request.getRemoteUser() != null) {
|
||||
wos.writeShort(swapShort( (short) 11));
|
||||
wos.writeBytes("REMOTE_USER");
|
||||
wos.writeShort(swapShort( (short) request.getRemoteUser().length() ) );
|
||||
wos.writeBytes( request.getRemoteUser() );
|
||||
}
|
||||
|
||||
if (request.getContentType() != null) {
|
||||
wos.writeShort(swapShort( (short) 12));
|
||||
wos.writeBytes("CONTENT_TYPE");
|
||||
wos.writeShort(swapShort( (short) request.getContentType().length() ) );
|
||||
wos.writeBytes( request.getContentType() );
|
||||
}
|
||||
|
||||
if (request.getContentLength() > 0) {
|
||||
wos.writeShort(swapShort( (short) 14));
|
||||
wos.writeBytes("CONTENT_LENGTH");
|
||||
String sContentLength = new Integer(request.getContentLength()).toString();
|
||||
wos.writeShort(swapShort( (short) sContentLength.length() ) );
|
||||
wos.writeBytes( sContentLength );
|
||||
hasBody = true;
|
||||
}
|
||||
|
||||
|
||||
// taken from CGI servlet
|
||||
|
||||
Enumeration headers = request.getHeaderNames();
|
||||
String header = null;
|
||||
while (headers.hasMoreElements()) {
|
||||
header = null;
|
||||
header = ((String) headers.nextElement()).toUpperCase();
|
||||
wos.writeShort(swapShort( (short) ("HTTP_" + header.replace('-', '_')).length() ));
|
||||
wos.writeBytes("HTTP_" + header.replace('-', '_'));
|
||||
wos.writeShort(swapShort( (short) request.getHeader(header).length() ));
|
||||
wos.writeBytes( request.getHeader(header) );
|
||||
}
|
||||
|
||||
|
||||
os.writeByte(0);
|
||||
os.writeShort( swapShort( (short) wos.size() ) );
|
||||
os.writeByte(0);
|
||||
|
||||
uwsgi_request.writeTo( os );
|
||||
|
||||
|
||||
if (hasBody) {
|
||||
IOTools.flow(request.getInputStream(), os);
|
||||
}
|
||||
|
||||
boolean statusParsed = false ;
|
||||
for(;;) {
|
||||
String line = byte_readline(is) ;
|
||||
if (line == "") {
|
||||
break;
|
||||
}
|
||||
if (statusParsed == false) {
|
||||
String[] status = line.split(" ",3);
|
||||
response.setStatus( Integer.parseInt(status[1]) );
|
||||
statusParsed = true ;
|
||||
}
|
||||
else {
|
||||
String[] keyval = line.split(": ",2);
|
||||
if (keyval.length == 2) {
|
||||
response.addHeader(keyval[0], keyval[1]);
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
byte cb[] = new byte[4096];
|
||||
int len = 0;
|
||||
for(;;) {
|
||||
len = is.read(cb,0,4096) ;
|
||||
if ( len > 0) {
|
||||
out.write(cb,0,len);
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private String byte_readline(DataInputStream is) throws IOException {
|
||||
|
||||
ByteArrayOutputStream line = new ByteArrayOutputStream();
|
||||
for(;;) {
|
||||
byte b = is.readByte();
|
||||
if (b == 10) {
|
||||
break ;
|
||||
}
|
||||
line.write(b) ;
|
||||
}
|
||||
|
||||
return line.toString("ASCII").replaceAll("\\n","").replaceAll("\\r","");
|
||||
}
|
||||
|
||||
private short swapShort(short x) {
|
||||
|
||||
short tmp1 = (short) (x>>8) ;
|
||||
short tmp2 = (short) (x<<8) ;
|
||||
return (short) (tmp1 | tmp2) ;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
#include "uwsgi.h"
|
||||
|
||||
PyObject *py_uwsgi_sharedarea_inclong(PyObject *self, PyObject *args) {
|
||||
PyObject *arg0 ;
|
||||
int pos = 0 ;
|
||||
long value ;
|
||||
|
||||
if (sharedareasize <= 0) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
arg0 = PyTuple_GetItem(args, 0);
|
||||
if (!PyInt_Check(arg0)) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
pos = PyInt_AsLong(arg0);
|
||||
|
||||
if (pos+4 >= getpagesize()*sharedareasize) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
pthread_mutex_lock((pthread_mutex_t *) sharedareamutex + sizeof(pthread_mutexattr_t));
|
||||
memcpy(&value, sharedarea+pos, 4);
|
||||
value++;
|
||||
memcpy(sharedarea+pos, &value,4);
|
||||
pthread_mutex_unlock((pthread_mutex_t *) sharedareamutex + sizeof(pthread_mutexattr_t));
|
||||
|
||||
return PyInt_FromLong(value);
|
||||
|
||||
}
|
||||
|
||||
PyObject *py_uwsgi_sharedarea_writelong(PyObject *self, PyObject *args) {
|
||||
PyObject *arg0,*arg1 ;
|
||||
int pos = 0 ;
|
||||
long value ;
|
||||
|
||||
if (sharedareasize <= 0) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
arg0 = PyTuple_GetItem(args, 0);
|
||||
if (!PyInt_Check(arg0)) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
pos = PyInt_AsLong(arg0);
|
||||
|
||||
if (pos+4 >= getpagesize()*sharedareasize) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
arg1 = PyTuple_GetItem(args, 1);
|
||||
if (!PyInt_Check(arg1)) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
pthread_mutex_lock((pthread_mutex_t *) sharedareamutex + sizeof(pthread_mutexattr_t));
|
||||
value = (long) PyInt_AsLong(arg1);
|
||||
memcpy(sharedarea+pos, &value,4);
|
||||
pthread_mutex_unlock((pthread_mutex_t *) sharedareamutex + sizeof(pthread_mutexattr_t));
|
||||
|
||||
return PyInt_FromLong(value);
|
||||
|
||||
}
|
||||
|
||||
PyObject *py_uwsgi_sharedarea_writebyte(PyObject *self, PyObject *args) {
|
||||
PyObject *arg0,*arg1 ;
|
||||
int pos = 0 ;
|
||||
char value ;
|
||||
|
||||
if (sharedareasize <= 0) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
arg0 = PyTuple_GetItem(args, 0);
|
||||
if (!PyInt_Check(arg0)) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
pos = PyInt_AsLong(arg0);
|
||||
|
||||
if (pos >= getpagesize()*sharedareasize) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
arg1 = PyTuple_GetItem(args, 1);
|
||||
if (!PyInt_Check(arg1)) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
value = (char) PyInt_AsLong(arg1);
|
||||
sharedarea[pos] = value;
|
||||
|
||||
return PyInt_FromLong(sharedarea[pos]);
|
||||
|
||||
}
|
||||
|
||||
PyObject *py_uwsgi_sharedarea_readlong(PyObject *self, PyObject *args) {
|
||||
PyObject *arg0 ;
|
||||
int pos = 0 ;
|
||||
long value ;
|
||||
|
||||
if (sharedareasize <= 0) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
arg0 = PyTuple_GetItem(args, 0);
|
||||
if (!PyInt_Check(arg0)) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
pos = PyInt_AsLong(arg0);
|
||||
|
||||
if (pos+4 >= getpagesize()*sharedareasize) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
pthread_mutex_lock((pthread_mutex_t *) sharedareamutex + sizeof(pthread_mutexattr_t));
|
||||
memcpy(&value, sharedarea+pos, 4);
|
||||
pthread_mutex_unlock((pthread_mutex_t *) sharedareamutex + sizeof(pthread_mutexattr_t));
|
||||
|
||||
return PyInt_FromLong(value);
|
||||
|
||||
}
|
||||
|
||||
|
||||
PyObject *py_uwsgi_sharedarea_readbyte(PyObject *self, PyObject *args) {
|
||||
PyObject *arg0 ;
|
||||
int pos = 0 ;
|
||||
|
||||
if (sharedareasize <= 0) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
arg0 = PyTuple_GetItem(args, 0);
|
||||
if (!PyInt_Check(arg0)) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
pos = PyInt_AsLong(arg0);
|
||||
|
||||
if (pos >= getpagesize()*sharedareasize) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
return PyInt_FromLong(sharedarea[pos]);
|
||||
|
||||
}
|
||||
|
||||
PyObject *py_uwsgi_sharedarea_read(PyObject *self, PyObject *args) {
|
||||
PyObject *arg0, *arg1;
|
||||
|
||||
int len = 1 ;
|
||||
int pos = 0 ;
|
||||
|
||||
if (sharedareasize <= 0) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
arg0 = PyTuple_GetItem(args, 0);
|
||||
if (!PyInt_Check(arg0)) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
arg1 = PyTuple_GetItem(args, 1);
|
||||
if (PyInt_Check(arg1)) {
|
||||
len = PyInt_AsLong(arg1);
|
||||
}
|
||||
|
||||
pos = PyInt_AsLong(arg0);
|
||||
|
||||
if (pos+len >= getpagesize()*sharedareasize) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
return PyString_FromStringAndSize(sharedarea+pos, len);
|
||||
}
|
||||
|
||||
|
||||
|
||||
PyMethodDef uwsgi_methods[] = {
|
||||
{"sharedarea_read", py_uwsgi_sharedarea_read, METH_VARARGS, ""},
|
||||
{"sharedarea_readbyte", py_uwsgi_sharedarea_readbyte, METH_VARARGS, ""},
|
||||
{"sharedarea_writebyte", py_uwsgi_sharedarea_writebyte, METH_VARARGS, ""},
|
||||
{"sharedarea_readlong", py_uwsgi_sharedarea_readlong, METH_VARARGS, ""},
|
||||
{"sharedarea_writelong", py_uwsgi_sharedarea_writelong, METH_VARARGS, ""},
|
||||
{"sharedarea_inclong", py_uwsgi_sharedarea_inclong, METH_VARARGS, ""},
|
||||
{NULL, NULL},
|
||||
};
|
||||
|
||||
PyMethodDef null_methods[] = {
|
||||
{NULL, NULL},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user