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.

rxokita's shared items