Skip to content

Commit

Permalink
Added network code to receive client requests... most likely does not…
Browse files Browse the repository at this point in the history
… work yet.
  • Loading branch information
oleyb committed Feb 15, 2011
1 parent e5adc28 commit 704910c
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ server/bin
server/ipch
*.sdf
server/openni/*
server/deps/*
server/deps/*
/mono-server/KinectOpenNIMono/KinectOpenNIMono/bin
132 changes: 123 additions & 9 deletions mono-server/KinectOpenNIMono/KinectOpenNIMono/Main.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
using System;
using System.Threading;

using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Text;
namespace KinectOpenNIMono
{
class MainClass
{
private readonly string SAMPLE_XML_FILE = @"../../Sample-Tracking.xml";

public static ManualResetEvent allDone = new ManualResetEvent (false);

private xn.Context context;
private Thread readerThread;
private bool shouldRun;
private xnv.SessionManager sessionManager;
private xnv.PointControl pointControl;

public static void Main (string[] args)
{
MainClass main = new MainClass ();
Expand All @@ -28,7 +41,7 @@ private void SetupKinect ()
this.readerThread = new Thread (ReaderThread);
this.readerThread.Start ();
}

private void ReaderThread ()
{
while (this.shouldRun) {
Expand All @@ -55,17 +68,118 @@ void pointControl_PointUpdate (ref xnv.HandPointContext context)

Console.Out.WriteLine ("PointUpdate: " + sendData);
}


}

private readonly string SAMPLE_XML_FILE = @"../../Sample-Tracking.xml";
#region Async Server Code
private void SetupServer ()
{
IPAddress ipAddr = IPAddress.Parse ("127.0.0.1");
IPEndPoint localEP = new IPEndPoint (ipAddr, 6001);

Socket listener = new Socket (localEP.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

try {
listener.Bind (localEP);
listener.Listen (10);

while (true) {
allDone.Reset ();

Console.WriteLine ("Waiting for a connection...");
listener.BeginAccept (new AsyncCallback (acceptCallback), listener);

allDone.WaitOne ();
}
} catch (Exception e) {
Console.WriteLine (e.ToString ());
}
Console.WriteLine ("Closing the listener...");
}

private xn.Context context;
private Thread readerThread;
private bool shouldRun;
private xnv.SessionManager sessionManager;
private xnv.PointControl pointControl;
public void acceptCallback (IAsyncResult ar)
{
allDone.Set ();

Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept (ar);

StateObject state = new StateObject ();
state.workSocket = handler;
handler.BeginReceive (state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback (ReadCallback), state);
}

public static void ReadCallback (IAsyncResult ar)
{
byte[] byteContent;

// Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;

// Read data from the client socket.
int bytesRead = handler.EndReceive (ar);

if (bytesRead > 0) {
// There might be more data, so store the data received so far.
BinaryWriter bWriter = new BinaryWriter (state.mStream);
bWriter.Write (bytesRead);
byteContent = state.mStream.ToArray ();

//Check to see if its as long as the as3kinect buffer is (6).
// if not, read more data.
if (byteContent.Length == 6) {
// All the data has been read
//Send (handler, byteContent);
Console.WriteLine ("Read in message from client!!! " + byteContent.ToString ());
} else {
// Not all data received. Get more.
handler.BeginReceive (state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback (ReadCallback), state);
}
}
}

private static void Send (Socket handler, byte[] data)
{
handler.BeginSend (data, 0, data.Length, 0, new AsyncCallback (SendCallback), handler);
}

private static void SendCallback (IAsyncResult ar)
{
try {
// Retrieve the socket from the state object.
Socket handler = (Socket)ar.AsyncState;

// Complete sending the data to the remote device.
int bytesSent = handler.EndSend (ar);
Console.WriteLine ("Sent {0} bytes to client.", bytesSent);

handler.Shutdown (SocketShutdown.Both);
handler.Close ();

} catch (Exception e) {
Console.WriteLine (e.ToString ());
}
}

// State object for reading client data asynchronously
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 6;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public MemoryStream mStream = new MemoryStream();
//public StringBuilder sb = new StringBuilder ();
}
#endregion


}
}

24 changes: 24 additions & 0 deletions mono-server/KinectOpenNIMono/KinectOpenNIMono/Sample-Tracking.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<OpenNI>
<Licenses>
<License vendor="PrimeSense" key="0KOIk2JeIBYClPWVnMoRKn5cdY4="/>
</Licenses>
<Log writeToConsole="true" writeToFile="false">
<!-- 0 - Verbose, 1 - Info, 2 - Warning, 3 - Error (default) -->
<LogLevel value="3"/>
<Masks>
<Mask name="ALL" on="false"/>
</Masks>
<Dumps>
</Dumps>
</Log>
<ProductionNodes>
<Node type="Depth">
<Configuration>
<MapOutputMode xRes="640" yRes="480" FPS="30"/>
<Mirror on="true"/>
</Configuration>
</Node>
<Node type="Gesture" />
<Node type="Hands" />
</ProductionNodes>
</OpenNI>

0 comments on commit 704910c

Please sign in to comment.