Wednesday, December 12, 2012

lua string to table

I couldn't find a clear way to do this... the entire bullshit that everyone always does is just reference the damn wiki. Which is about as clear as mud. I hate it when programmers assume you're already a programmer.

in anycase given a string like
myString = "this is a long string"
and you want to turn this into an array of letters
like myArray = {t,h,i,s, ,i,s, , a, ,l,o,n,g, ,s,t,r,i,n,g}
for reasons of being able to do things based on each letter.

you should do this:

myArray = {} -- make empty array
for i = 0, #myString do -- #myString is the length of the string
    local s = string.sub(myString,i,i) -- gets the character at location i in the string
   myArray[i] = s -- assigns the table[i] to the character which was returned to s
end


that's it... a clear example with, imagine that!

Tuesday, November 6, 2012

String^ to const char* C++/CX

So today I was looking to convert a textBox text field into a const char to use as an IP address to send data to. This wasn't exactly easy to find any info on so I had to write this post.

First I needed to get the String^ from the textBox xaml object like this
String^ s = IPAddress->Text;
this assumes that the textBox is named "IPAddress".
then make a size_t object and i'll call it i, this was needed to set the data size for the wcstombs_s function at the end. Not exactly sure what this is used for, but this is working, so if anyone has any good understanding of this function then please fill me in down in the comments below.

String^ s = IPAddress->Text;
size_t   i;
const wchar_t* cw = s->Data();
char *c= new char[s->Length()+1];
memset(c,0,s->Length()+1);
wcstombs_s(&i, c, (size_t)s->Length()+1,  s->Data(), (size_t)s->Length()+1 );
SomeClientFunction.SetIPAddress(c);

then I make the const wchar_t* pointer and use the s->Data() to satisfy that.
then i make a new char[] array with the length of the String^ this is pretty simple. so char[s->Length() + 1] is how that's done.
then I have to allocate the memory for the char*c using memset, pretty easy here.
then the weird wcstombs_s which is needed since wcstombs was depricated, so i needed to use the safe version of it to get past the WinRT checks. I guess this is a memory security thing.
now the new safe version of the function asks for the &i which im not sure what does, then you add in the target char array then set a size, then give it a source using s->Data(), then tell it how long that array is.
and that's that, worked for me!

I'll also note that my SomeClientFunction was expecting (const char * ip) in it's args.

I just use this blog to help remind me how I did things in general.

hope this helps someone.

edit:
So the &i is a returned value that's the size of the actual c in this case. Since c could actually be shorter than the original size allocated for it when using some other methods to create the size of c.

Friday, October 5, 2012

Dealing with create_task in WinRT using C++/CX


Getting into Windows8 C++/CX

Tutorials and introductions to C++/CX
http://msdn.microsoft.com/en-us/library/windows/apps/hh465045.aspx
from that link:
"When you use Windows Runtime objects, you're (typically) using C++/CX, which provides special syntax to create and access Windows Runtime objects in a way that enables C++ exception handling, delegates, events, and automatic reference counting of dynamically created objects. When you use C++/CX, the details of the underlying COM and Windows architecture are almost completely hidden from your app code. For more information, see C++/CX Language Reference. However, you can also program directly against the COM interfaces by using the Windows Runtime C++ Template Library."
"You're primarily programming against a new, easy-to-navigate, object-oriented API, the Windows Runtime, although Win32 is still available for some functionality."
Some functionality has me concerned as the threading and concurrency namespaces are useful for what we were doing in the previous C# version.
This continues into a tutorial about what the different components in a new project are. The partial ref and auto keyword are also mentioned in breif detail.
in the first paragraph of that page there were a couple of links to the C++/CX language reference including...
as a note the example shows Grid^ grid = ref new Grid(); the ^ is used when dealing with anything that uses ref new to create;
I guess this feels sort of like

int s = int(0);
int *i = &s;

where when a pointer is used you need to use a reference operator to get the value at the address of a pointer.
in my test i wrote the following.

auto grid = ref new Grid();//this is the same as Grid^
grid->Width = 600;
grid->Height = 500;
auto red = ref new SolidColorBrush(Colors::Red);
grid->Background = red;
if(grid->Parent == nullptr)
{
LayoutRoot->Children->Append(grid);
}

One important thing is that Colors::Red is not Colors->Red. As -> points to properties of a class and :: points to static members of a class.
"In the most basic sense, a ref class is a COM object that implements the IInspectable interface and whose lifetime is managed by a smart pointer."
"The partial keyword tells the compiler that the declaration of this class is continued in another code file."
good to know, sort of nice you can break up a big source file into a few small ones.

"If you, the programmer, have to add variables or functions to the MainPage class, do so in MainPage.xaml.h and MainPage.xaml.cpp." As you normally would.
"If the XAML editor has to add variables or other boilerplate code, it does so in the *.g.h and *.g.hpp files."

I've got to find where the .g. files are hiding in our project, I'm pretty sure I saw them somewhere, but i'll check if adding things into these files will allow for better cross talk between the C++ and C++/CX stuff.

"In general, you can safely ignore the *.g.* files. That's why Solution Explorer hides them by default."
Hey, hiding them by default? What if we, the programmer, need to edit the xaml?? WTF. Weird I can't find these files. oh well, moving on...
further down in the post there's a special include for

#include <ppltasks.h>

I didn't catch this include before, wasn't in any documentation other than the tutorial. Things like this can't be missed so I think something that important should be a bit more highlighted.

auto grid = ref new Grid();
grid->Width = 600;
grid->Height = 500;
auto red = ref new SolidColorBrush(Colors::Red);
grid->Background = red;

create_task([grid]{
grid->Height = 100; //this throws an error
}).then([grid]{});

if(grid->Parent == nullptr)
{
LayoutRoot->Children->Append(grid);
}

In the previous test code I found that this throws an error when trying to execute anything inside of the lambda in the create_task function. The red squiggle under the [grid] says a local lambda is not allowed in a member function of a WinRT class. Not sure why that is.
expanding on the create_task function. In

I created the following code to stick a wait timer.
auto i = 0.5;

create_task([this, i]{
auto thispage = this;
        thispage->Dispatcher->RunAsync(
CoreDispatcherPriority::Normal,
ref new DispatchedHandler([thispage,i]()
       {
thispage->GridControlElement->Opacity = i;
       }));
wait(SomeRandomInteger); //a static global variable.
}).then([this, i]{
auto thispage = this;
        thispage ->Dispatcher->RunAsync(
CoreDispatcherPriority::Normal,
ref new DispatchedHandler([thispage]()
        {
thispage->GridControlElement->Opacity = 1.0;
        }));
});

So this is scoped as the page that the create_task lives within. It's then passed through the lambda into the create_task function I also added an number value to see how that will work when passed into the [this,i] part of the create_task.

Then I created another auto so that this can be used as thispage. so now the create_task has access to elements on the page. so then we can create a new DispatchedHandler and pass along another referenced task.

Elements in the thispage reference can be accessed and modified asynchronously from the rest of the UI.
So after some amount of time while the wait is running, the UI element will be mostly translucent. then after the timer is up it goes back to being fully opaque.

all the while the wait is running the rest of the interface is doing just fine.

Adding this into a touch event...

http://msdn.microsoft.com/en-us/library/windows/apps/hh699871.aspx
from that link:
"Visual C++ component extensions (C++/CX) is a set of extensions to the C++ language that enable the creation of Windows Store apps and Windows Runtime components in an idiom that is as close as possible to modern C++. Use C++/CX to write Windows Store apps and components in native code that easily interact with Visual C#, Visual Basic, and JavaScript, and other languages that support the Windows Runtime. In those rare cases that require direct access to the raw COM interfaces, or non-exceptional code, you can use the Windows Runtime C++ Template Library (WRL)." and "Windows Store DirectX games and graphics-intensive apps. For more information, see Create your first Windows Store app using DirectX." the DirectX + Xaml are the core of what we're doing. also, that page has a "quick reference" for the other components for C++/CX

A collection of links to various C++/CX documentation dealing with the new extensions for WinRT

http://blogs.msdn.com/b/vcblog/archive/2012/08/29/cxxcxpart00anintroduction.aspx
http://sridharpoduri.com/2012/08/13/programming-windows-8-applications-using-c-cxan-update-on-the-book/

Thursday, October 4, 2012

Timers in windows8 using c++


Just getting a basic timer in c++ isn't as easy as in C#.
So I'm stepping myself through this and documenting my findings.
I just want to start a timer on a touch event to fade out a control when the finger leaves the screen.
So to do this I want that control to wait for a few seconds before going back to it's home position.
I could use wait, but then the rest of the screen locks up till after the wait.
Since I want to use multi touch features this is bad.
The best way to do this *I think* is concurrency and the call function, but finding a good example for this has been really hard.

http://msdn.microsoft.com/en-us/library/dd504870.aspx I started here to get a start on the concurrency run time.
Also writing a windows 8 metro app in WPF, so that's adding some little hitches here and there with various features that I can and can't use.
Also can't use 3rd party libs like boost.asio which is because the client doesn't like those things, doh.
Google for running multuple tasks and I get to
this link http://msdn.microsoft.com/en-us/library/dd728065.aspx.
In here was the following code snippet...

int wmain()
{
// Create a call object that prints a single character to the console.
call<wchar_t> report_progress([](wchar_t c) {
wcout << c;
});
// Create a timer object that sends the dot character to the  
// call object every 100 milliseconds.
timer<wchar_t> progress_timer(100, L'.', &report_progress, true);
wcout << L"Performing a lengthy operation";
// Start the timer on a separate context.
progress_timer.start();
// Perform a lengthy operation on the main context.
perform_lengthy_operation();
// Stop the timer and print a message.
progress_timer.stop();
wcout << L"done.";
}

This looked pretty helpful. There's the call and the timer functions that look pretty good.
Unfortunately in report_progress([] (wchar_t c) {...} there's a lambda and I get the following error

Error: a local lambda is not allowed in a member function of a WinRT class.

damn.

So now what? Dig around more for timer class in WinRT and I find
http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/fd9c3010-484b-4881-88c8-765473fd5b1e
What's going on here?

Instead of SetTimer(), you should use Windows::UI::Xaml::DispatcherTimer.
See the MSDN article here: http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.dispatchertimer.aspx.

Okay, so lets go there.
http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.dispatchertimer.aspx.
and...

Server Error in '/' Application.
The resource cannot be found.Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. Requested URL: /en-us/library/windows/apps/windows.ui.xaml.dispatchertimer.aspx.

Crap, so look up xaml dispatch timer and get to  http://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.ui.xaml.dispatchertimer
I think im getting closer. This is looking a bit better, something that seems to be useful, finally.
I just used this in C# for the previous version of the interface I was building in C#, perfect!
I think I can use this. The example is in C# so i need to find a C++ example,
get to this http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/edff3bf2-4223-49fe-b66b-119702458700
The thread goes through a few iterations. I get to what I think is really close but get stuck at.

timer->Tick += ref new  Windows::Foundation::EventHandler<Object^>(this, &DispatcherTimer_Tick); 

I get an error:

object not needed for the specified function

So I delete "this" and the following "," and that seems to do the trick.
No more errors...
So I start my vars with...

Windows::UI::Xaml::DispatcherTimer ^SomeTimer;

and then add in a way to count ticks...

int Ticks;

and when the xaml page is initialized i add in

XAMLPage()
{
InitializeComponent();
//other init stuff...
SomeTimer = ref new DispatcherTimer;
SomeTimer->Tick += ref new  Windows::Foundation::EventHandler<Object^>( &Timer_Tick); 
TimeSpan t;
t.Duration=1;
SomeTimer->Interval = t;
}

Then I precede the page initialization with my Timer_Tick function that looks something like...

void Timer_Tick(Platform::Object^ sender, Platform::Object^ e)
{
OutputDebugString(L"TIMER!!!");
Ticks++;
if(Ticks>10)
SomeTimer->Stop();
}

so after 10 ticks the timer stops.

I'm far from a super c++ programmer, and mostly a C# or unrealscript game programmer guy.
I've been doing this stuff for a while and I learn fast, and that's pretty important.
I do these little process blog entries to help myself out.
It's like a breadcrumb trail I can follow to avoid backtracking.
Hope this helps someone.

Tuesday, September 11, 2012

Working in Windows 8

So the first weird thing that's bugging me is the mix of windows desktop and windows8 metro apps. When i alt tab i get all of the different apps running. Chrome has two versions, one for wen i use the metro icon to launch it, then another when i launch it from the task bar in the desktop. With a few different desktop apps and a single metro app running you can do this weird split desktop thing. I'm not sure that it makes a whole lot of sense just yet. Especially since you can use both sides at the same time.

Really weird. This is going to need some more development time by everyone involved. The apps need to have a sort of scrunched up in a margin mode so they can remain useful. Something to think about when making a metro app I guess.



The little column on the right is some other app... since it's all white, i don't know what it is...

Thursday, May 24, 2012

FarseerPhysicsXNA, just getting things to work.

So almost all of their documentation on codeplex.com refer to version 2.0, and using 3.0 a lot of functions seem to have been changed. Also, they forgot that a part of a release is documentation, so imo, they're far from done.

DebugView, why doesnt debugview work?
oh because their example refers to their sample project. Which would be great, if I had started with their project.

So now i have to either incorporate my project into their project or take parts of their overly complex sample project and incorporate it into mine.

Damnit.

Monday, May 14, 2012

xna file updates in windows phone 7.1

getting models to load into the content
So i managed to get the model into the phone at least once, but from there on out when I update the fbx the model doesn't get updated. Having this issue is pretty annoying. Even more awkward, if I load in a different model, nothing is displayed at all, but the load function doesn't error. So what's going on? Is the model too big, too small, not enough documentation on how the fbx should be setup for me to figure out what's wrong with the model.
so

Debug.WriteLine("mesh info>> " + ball.Meshes.Count.ToString());

tells me that

mesh info>> 0

So even more annoying is that Visual Studio doesn't always like to re-process the model into it's xnb when the file is updated. I think it's deciding that the fbx's time stamp hasn't changed enough for it to bother. Annoying so i have to use an A and B to keep switching between the two so it'll pick a different file altogether to load to force it to load the model as a new model.

So it's running fine, I'm just finding that visual studio has a bad habbit of ignoring file updates when building the xnb files from the fbx files. annoying to say the least.

Interestingly enough though a primitive that's 1000 units across seems to be about the correct size to take up most of the center of the screen on the phone. 100 units is a small object on the screen. but my camera was 5000 units away, moving it to about 1000 units away from the origin made a big difference but the object is also a bit more distorted with perspective.

In anycase, if you're having issues with models updating then try changing the name of the file you're loading. seems like the file doesn't always update. when the fbx is changed.

Friday, May 11, 2012

2D and 3D content in XNA


Drawing layers of 2D and 3D content in XNA on the Windows Phone.


private void OnDraw(object sender, GameTimerEventArgs e)
{
SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);

spriteBatch.Begin();
spriteBatch.Draw(background, mainFrame, Color.White);
elementRenderer.Render();
spriteBatch.Draw(elementRenderer.Texture, Vector2.Zero, Color.White);
spriteBatch.End();


Draw3DContent();

spriteBatch.Begin();
spriteBatch.Draw(cursorImage(), cursorPos, Color.White);
spriteBatch.End();
}


So this is a modified versuion of some code that i came to when drawing 2d stuff and 3d sutff using silverlight and xna for both 2d and 3d elements. The first chunk is going to be a begin and end for the background and some UI elements that are drawn in silverlight. then I have another function that has all of the 3d elements each chunk ending in mesh.Draw();
the last bit is to draw a silverlight cursor using xna sprite stuff since the cursor is animated and has several states.


I wasn't able to find an example of this anywhere so im posting it here.

Tuesday, May 1, 2012

generics and types

So today I came across

And to be honest, I have no idea what T really was. I assumed it had to do with typecasting, but wasn't sure exactly how it was used here. So it seems like it's a "name" and not a keyword, which is good since it's just a letter. But still, it seems like it's something that I needed to learn a bit more about. 

Looking into my C# book I find a section on Generics which seems promising. My book says something about Generics offering better static typing, but from my past experience static typing just makes using variables more strict. I hate putting limitations on how easily i can use a letter or number.

Both of these things infer that you need to add
using System.Collections.Generic and possibly System.LINQ, but I commented the two statements, and the code still compiles fine with the <T> in the function. Not sure why that is since it's inferring that <T> is something that comes out of one of those two references. Am I missing something? why is this working without those two references?

public Diddly something<Diddly>(Diddly someValue)
{
Diddly squat;
squat = someValue;
return squat;
}

why, exactly, is that chunk of code valid C#? I'm not completely clear as to how Diddly is used and why squat can be Diddly. This all seems a bit weird to me. I'll have to experiment with that it all means. Microsoft says to use <T> and not <Diddly> just out of using standards, but to me T is no more descriptive than Diddly. I suppose they're trying to say that it's a T for Type, but why not just use <GenericType> instead of <T> for the sake of being descriptive or expressive as people like to say about C# and F# and the other # languages.
Adding in a debug line i get this...
So T is a string. a string type of string, which i'd imagine it should be. But why the <T> ?
So what if i turn all of the Ts into string?


This gives me this error and it tells me it doesn't like types as identifiers. So, that tells me that C# is actually looking at all of the instances of the T as a variable of some kind. But why? how are all of these things being used?
Visual studio has a helper that would like to tell me that to fix this the first place where T should be it's asking:
If it would like the T to be a class or type, but it just said it didn't want types. Either way, neither of those generated objects were valid.


This is confusing though, somehow this is valid? Why? <int> and <string> what are these and how do they work? Thought I was trying to learn something today, instead so far I'm just getting confused.




email sucks.

Oops
so i managed to forget that i still have an old email account. it's on my old server okita.com and it's really neglected. basically, it's rendered itself useless. And it's impossible to get the server to sort through the files so i can even clear out the in box.
I'm trying to get their tech support to clear the server's files for me.

Friday, April 27, 2012

Audio Recording on Windows Phone7.1

Audio recording from the windows phone mic uses the XNA framework.
So when I added the audio calls it requires a GameTimer update which has a FrameAction function. This wasn't already in my app when i started because I didn't use the XNA game template.

So I'll have to add one, But I already added a Tick update function for updates. I wonder if this will overlap in a bad way...

right now im using:

_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromMilliseconds(30);
_timer.Tick += new EventHandler(Tick);
_timer.Start();

for updates. so, possibly, i can switch that to being a GameTimer instead? giving that a try...
So, it looks like the dispatchTimer can be replaced with GameTimer and the functions in it do the same thing

_timer = new GameTimer();
_timer.UpdateInterval = TimeSpan.FromMilliseconds(30);
_timer.Update += Tick;
_timer.Start();

This is the new code, GameTimer() replaced DispatcherTimer() and UpdateInterval replaces Interval, and Tick was replaced with Update and rather than a new EventHandler i just add the name of the function thats going to be updated. Then I start the timer.

Does the same thing it seems.
BUT now im getting this crappy error:

A first chance exception of type 'System.InvalidOperationException' occurred in Microsoft.Xna.Framework.dll
An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.Xna.Framework.dll

Additional information: FrameworkDispatcher.Update has not been called. Regular FrameworkDispatcher.Update calls are necessary for fire and forget sound effects and framework events to function correctly. See http://go.microsoft.com/fwlink/?LinkId=193853 for details.

seems like I need to add something... a short bit of reading I find I need to call it manually for some reason...
in my tick I added:

void Tick(object sender, GameTimerEventArgs e)
{
   FrameworkDispatcher.Update(); // this was needed?
   ...
}

And that seems to have fulfilled what i need for calling the framework dispatcher... whatever that is.
so now it looks like i can get a buffer filled with mic stream data bytes. Golly, in the last four days I've only written about 480 lines of code. but every method was something I had to learn about.

hopefully, someone, other than me, found a line of code in this blog useful.

Thursday, April 26, 2012

Timers and Ticks in Windows Phone 7

So I've come to the conclusion that this code

Timer stateTimer = new Timer(tcb, autoEvent, 1000, 250);


Sucks. The Timer Class gets garbage collected far too quickly.
I had added a timer.Start() and the function just debug.writeline to an int that counted up 30 times a second.
So after a few seconds it would count to about 213 or so, then it would stop. Nothing stopped it, i just disappeared even though the function wasn't told to stop. So I think it was garbage collected. And i found a few places that talked about the timer having to be called in a way that prevented it from being garbage collected. LAME!

However!

using System.Windows.Threading;
this line gives you a different way to do the exact same thing, and it's a lot more readable.

in the class define a nice variable
public class SomeClass
{

DispatcherTimer _timer;

public MainPage()
{
//this timer works way better.
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromMilliseconds(30);
_timer.Tick += new EventHandler(Tick);
_timer.Start();
  InitializeComponent();  }
void Tick(object sender, EventArgs e)
{
//updating in a tick
}

}
that's it, that's all, nothing else needed.
The tick is called 30 times a second, and it never stops!
The other Timer() function gives me problems, this one doesn't I don't know why. But I am doing things on the windows phone, so that might have something to do with it.

Update 5pm
Moving into other things on the Phone, basically if you can access the MotionController vs the Gyro or the Accelerometer, you're better off. Things like getting Yaw Pitch and Roll or even Quaternion are way more useful than gyro and accelerator alone. Something I'd say you should do is just ignore phones that don't use motion controller. It's obvious, those phones will suck anyway.


Wednesday, April 25, 2012

Comparing data from the iPhone and the Lumia 900, and moving on to recording sound.

So firstly I'm seeing that the acceleration data is more muted on the Lumia, or rather maybe the iPhone is more sensitive. The overall values coming out of the Nokia are smaller in comparison. But they are consistent, so it seems like I just need to add a multiplier to make it more like the iPhone data. Also, is it me or are the X and Y switched on the Lumia from the iPhone.

Also, i need a better office chair...

My next trick will be sending recorded audio over the websocket.

Tuesday, April 24, 2012

WebSockets on the windows Phone 7.1 day 3

Okay, so im sending what I think is a websocket connection attempt. The server logs this out:


Client Connected!!
==================
Client IP 127.0.0.1:60929
TimeStamp: 11:10:14.288
<0> GET
<1> /
<2> HTTP/1.1


<3> Upgrade:
<4> WebSocket


<5> Connection:
<6> Upgrade


<7> Sec-WebSocket-Version:
<8> 13


<9> Sec-WebSocket-Key:
<10> N2M4OWQ3MDgtOGYwNC00MQ==


<11> Host:
<12> 10.10.10.221:11111


<13> Origin:
<14> 10.10.10.221


<15> 


<16> SERVING FILE: ../../../www/index.html TO: 127.0.0.1:60929

So i think it's a http port I'm connecting to, but I'm pretty sure that it might be a web socket since there's all that sec-websocket-version: stuff thats in there. Rather confusing, but I think im getting closer. On the phone side of things I'm checking the websocket status and it's saying "connecting" so i think that's getting closer.

Update: 12pm
Reading up on "subProtocol" i find an RFC which states in the first line in the subProtocol section " _This section is non-normative._" so... This is going to be a very difficult read being that I don't even have any idea what the fuck that sentence even means. Even so, it's saying a sub-protocol is a sub-protocol, which explains nothing. This is why I hate talking to other programmers.


lunch break...
So I managed to notice that the app im sending things to was updating with funny error codes, and it was a bunch of errors coming from the websocket, so that's a good sign. I just have to format the out going stream to something that the websocket message receiver can understand.


the websocket() function is weird.

ws = new WebSocket(("ws://" + hostName + ":" + portNumber));
or
ws = new WebSocket(("ws://" + hostName + ":" + portNumber),"",null,null,"","",WebSocketVersion.DraftHybi00);

Update 3pm
Trying to set the websocket version isnt easy, I have to fill in the rest of the garbage that the method expects. So i'm hoping that the first version works since the more detailed version requires a ton of crap that I don't feel like filling in. AH mother f*cker... so the phone wants to talk in Rfc6455 and server is Hybi00 so i do have to fill in the rest of that shit. damnit...


Update 4:30pm

Yay, managed to fulfill the damn full version of the websocket function. You using string.empty and null you can fill in the junk that doesn't need to be filled right now for testing.


WebSocketVersion ver = WebSocketVersion.DraftHybi00;// <-- need to switch to this mode.
string subProt = "HTTP / 1.1";
ws = new WebSocket(("ws://" + hostName + ":" + portNumber), subProt, string.Empty, null, string.Empty,string.Empty,ver);

Finally got the version of the prototcol to be DraftHybi00 which is what the server is expecting. Now im only getting one weird error... but I can at least test the other versions of the websocket. For one thing, subProtocol is a string. What it expected in that string was difficult to find. So just randomly guessing I found on some RFC.

On your own you can replace ("ws://" + hostName + ":" + portNumber) with a regular url as long as you use ws:// instead of http:// then follow the ip address or web address with a : and a port number that makes sense, web is 80 sometimes on your local area network you might use 8181 or soemthing that your websocket server is listening to.

"CONNECT example.com HTTP/1.1"

so i just tried putting "HTTP/1.1" into the subprotocol, nothing complained, so I'm just going to go with that as the subprotocol. Until I can find a better string to use than that I'll stick with it.

horray! looks like the server side is at least saying

RECEIVED NO DATA


RECEIVED NO DATA


RECEIVED NO DATA


When I send a string through my websocket. that means i just need to format a string to send to it!

it's working!

So now i just gotta format my socket data into a form that the server can understand. and in my case it's a custom server thing so there's nothing i can share about it here.

Monday, April 23, 2012

Silverlight websockets and windows phone day 2

Unproductive weekend is over, now I have to get back to websockets on the phone.
Downloaded the source for websockets4 from http://websocket4net.codeplex.com/ but the example code still gives me errors after adding various things to the project.

Tried to use the nuGet plugin from visual studio and I get an error

PM> Install-Package WebSocket4Net
Attempting to resolve dependency 'Json.Net (≥ 4.0.5.14411)'.
Install-Package : Unable to resolve dependency 'Json.Net (≥ 4.0.5.14411)'.
At line:1 char:16
+ Install-Package <<<<  WebSocket4Net
    + CategoryInfo          : NotSpecified: (:) [Install-Package], InvalidOperationException
    + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageComman 
   d


PM> 
So that failed. And there's no obvious way to get help, damnit. First experience with nuGet sucks. first experience with websockets4 sucks. So i try to get Json from somewhere else, and try to use that, and nothing new happens with the dependencies other than I have more references added in the directory.

So im going to try http://html5labs.interoperabilitybridges.com/prototypes/websockets/websockets/download
the sort of official implementation of websockets. from the Microsoft site I find that I need to include "using System.Json" which I don't have. So I also need to go get that. So after more reading I find that Json is not supported by Windows Phone, so another dead end.

None of those projects worked with sockets as the functions that I needed like TcpClient dont exist for Windows Phone. So WebSocket4Net doesn't do much without the TcpClient, or things like Listen... looking at that again to see if there's something I missed...

update 6pm
further investigation leads me to believe that websocket4net might still have some solutions, it's just undocumented and opaque. WebSocketSamples.zip from their site should shed some light on how the functions are used. I hope.

So, im doing something liket this...
ws = new WebSocket(("ws://" + hostName + ":" + portNumber));

ws.Opened += new EventHandler(ws_Opened);
ws.Open();
Debug.WriteLine("state?" + ws.State.ToString());
So the Output for that ends up like
state?Connecting
So im guessing that there's something missing on the server side.
But it is doing something... I think im getting closer.

Friday, April 20, 2012

Silverlight Windows Phone and Sockets part 1 of an on going learning experience.

So im going to be learning how to write a silverlight app on a windows phone to send gyro data to a pc. Sounds simple, but from the get go I found that the http version of socket is really damn slow, so I'm supposed to use websocket so i've been told. Right now I have an HTTP version of this working which is really slow.

so here it goes. I'm going to read
http://www.rajneeshnoonia.com/blog/2010/12/silverlight-websockets-duplex-communication/
and
http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2010/07/27/silverlight-and-websockets.aspx

so hopefully I'll be able to wrap my head around sockets in general by the end of the day.
So websockets allow TCP over a single socket. and Wiki says that any client can use websockets not just browsers. Which is good since im writing a silverlight client. Wiki also mentions "realtime games" which is good since I need to send gyro data as fast as possible from the phone to the pc to use the phone as a controller.

So the wiki technical overview mentions that some browsers dont support this for security reasons, im hoping that the latest version of silverlight im running does support websockets, or im SOL.

Wiki mentions something about a handshake, which is what my fellow programmer mentioned, so it seems like im on the right track. Okay so enough with the wiki, onto the URLs i mentioned.

first with http://mtaulty.com/ he mentions TCP being full duplex and open and closes a connection only at the beginning and end of a session. I want to do this, so again seems like im on the right track. This is better than the http version of the app im working with right now with "using System.Net.Sockets;" in my C# app so far.

he also mentions that i should read http://www.whatwg.org/specs/web-socket-protocol/ but really? it's a gigantic unformatted txt file... WTF? it does mention a lot of UTF8 looking bytes that are line breaks and stuff. so i'll just stick to that being something about the sorts of characters to send to make websockets do specific things.

but what? Now he's talking about http polling and other "tricks" and how great http is making use of sockets. No, i dont want http, it's slow!

okay, hes going back into websockets now. so it's looking like things are back on track....

reading about checking if your browser supports websockets, i'll skip this shit... and he just says use WebSockets open send close methods to use websockets... fucker, that didnt help worth shit.

after further research on msdn I find .NET Framework 4.5 has system.net.websockets, which is what I need, and it's a beta... so lets see if i can get it to work in visual studio 2010 found it here http://www.microsoft.com/download/en/confirmation.aspx?id=28978

i read somewhere that it's not side-by-side compatible with 4.0 so lets hope that the beta mostly works for what I need. since the WebSocket part of the framework is new I'm hoping that they spent a lot of time on this part to make it work right. crossing my fingers...

update ~4pm:
ah! so i found that mtaulty guys video posts which seem a lot more informative http://channel9.msdn.com/blogs/mtaulty/silverlight-4-beta-networking-part-9-udp-multicasting

I installed .NET 4.5 and there's no system.net.winsockets reference to add. I'm not sure what else might have been added but I can't really tell if there's a way to check if 4.5 installed correctly since it just over wrote 4.0 stuff. So I'm going ahead and Installing Visual Studio 2011 beta, so I hope that perhaps that might have the websocket thing I need to add to the project so I can write this client for the phone if the phone can even make use of the new .net libs through silverlight.

update ~7pm:
After installing the Visual Studio 2011 beta I'm met with a rather start grey everything. Kinda neat, but also pretty flat and it feels awkward with the windows 7 glassy frames. And, starting up a project I find I have to go to the on-line templates to find a Windows Phone template that might have what I need. And once I got that up I check for System.Net.Websockets and NOTHING I can't find the reference to add to my phone project. So I'm pretty much dead in the water.

looks like I'll have to write a small javascript web app to websocket data from silverlight to the PC.
We'll take a look at how that might work on monday. For now I'm going to go home and enjoy some Unrealscript and shoot things.

Wednesday, February 8, 2012

Starting to get into Game Maker

Just figuring out how to build scripts in Game Maker.

So far it's pretty simple. Almost over simplified. However, it's got a lot of clever things.
The timeline editor seems like a pretty simple way to control flow of action.
Add in a step0 and call some script.
Then add in Step at position 1, and then make it sleep "Zzz" for 1000 ms, or a second.
then add in a step at 2, resume timeline.

Set the timeline to loop and you have a script that executes once a second on the object the script is applied to.
I have an object which on create sets it's timeline and then starts it.

then my script looks like this:

globalvar someVar;
{
 cfTime = 1.0;
 cfTime += someVar;
 show_debug_message(cfTime);
 someVar += 1.0;
}

with this script applied to the timeline to execute, the messages increment +1 every second. pretty simple.
in the messages window when running in debug i get a nice counter. I'll be using this for a lot of things, Just getting all of my globalvar's to be easily remembered is going to be a big part of the organization of this project.

rxokita's shared items