|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
Hi, third year CS student
Been fidling a little bit with C# client-server chat application. It's np, working great, and I am now looking to extend it with voice chat, but I dont have the slightest idea how to do it -- dosn't seem to be any native audio libraries in C#, now that they have ditched DirectX support (I am using .net framework 4, so VS2010).
I have read abit about the Microphone class from XNA 4, which works if I am creating a XNA windows game, but I am not, I just want to utilize the microphone class but there's 0 documentation / examples on the net that does not use a Game client or Silverlight.
I have googled this stuff and read many pages but all of them are either about directx (which is not supported anymore), 2005ish articles which are outdated, or they just contain a binary and no sourcecode.
Does anyone have any idea what to do? I've been thinking about tryint to rewrite it in C++ but I love the native libraries and functions in C#, it's sweet to program in it.
|
I have a question, please dont laugh at me: how to make a new line in a text file in Java so that it works both in Windows and Linux?
I've this piece of code, where I append the \n char to make a new line in a string which will be saved to a text file eventually. It works on Linux, but on Windows I would have to use \r\n (or \n\r, dont really remember).
for(DBRow r : this.getRows()) { for(DBColumn c : r.getColumns()) { str += c.getValue() + "&"; }
StringBuffer buff = new StringBuffer(str); buff.setCharAt(str.lastIndexOf("&"), '\n'); str = buff.toString(); }
|
On November 14 2010 00:05 fabiano wrote:I have a question, please dont laugh at me: how to make a new line in a text file in Java so that it works both in Windows and Linux? I've this piece of code, where I append the \n char to make a new line in a string which will be saved to a text file eventually. It works on Linux, but on Windows I would have to use \r\n (or \n\r, dont really remember). for(DBRow r : this.getRows()) { for(DBColumn c : r.getColumns()) { str += c.getValue() + "&"; }
StringBuffer buff = new StringBuffer(str); buff.setCharAt(str.lastIndexOf("&"), '\n'); str = buff.toString(); }
You can use:
System.getProperty("line.separator");
It returns a new line character as a string for the current platform
|
Germany2896 Posts
|
Had to tweak my code a little, but it worked flawlessly.
Thanks!
|
Hey gang,
I've been working on a game recently, a grid based strategy game focusing on good performance, but now I've started working on pathing, since I want something like workers responding to clicks, and the performance is taking a hit. If it's just corridors, it's easy, big O of 1 most of the time, but once I start working with open spaces, it gets alot more complicated trying to find the shortest path with the least amount of work.
I'm trying out a couple of different methods of going about this right now, but in the meantime I thought I'd ask if anyone can recommend a good book or something concerning 2d pathing? Below is a screenshot of a potential stage, with a example start and end marker placed.
|
I suppose adjacent cells are the only moves possible? In which case it's pretty sparsely connected, so Dijkstra's should do fine. (See the wikipedia page here.)
|
Read up on dijkstras algorithm and the A* (a star) thingy (might be called different in english, but should be linked). Basically, just put a graph grid over your map and connect the graph nodes with edges if you can walk from one to the other, then just perform the shortest distance algorithm on that graph with the entities position as starting node and the target as target node.
|
|
Osmoses:
I recall reading in a game programming book a long time ago a solution for exactly the sort of problem you describe. Number the destination tile as 0. Then number any tiles adjacent to the destination tile as 1. Number any tiles that a 1 could move to -- other than 0 -- a 2. Repeat until you have numbered your Hero's current tile. Then as long as the hero moves to a smaller numbered tile than his current one every move, he is on a correct path. A benefit is that equal paths have equal likelihood instead of arbitrarily having to decide which path your algorithm will take.
Your example grid would look like this:
![[image loading]](http://rotanimod.com/numberedgrid.png)
I think this could be what you are looking for... let us know.
|
That would be a simplified version of Dijkstra's, and it works fine for this purpose. Just make sure you have an efficient way of determining what cell is connected to another, or you'll jump to O(n^3) time (maintain sets of visited, unseen, and fringe nodes).
|
Germany2896 Posts
When normal A* is not enough you can use a better heuristic. For example you could divide the map into (walkable) subregions, and pre-calculate the minimal travel-time between each region which you then use to fuel the heuristic used in A*. But in your simple example(really small level) both A* and Dijkstra should be fast enough. And Dijkstra is a lot simpler to implement.
|
On November 14 2010 00:20 MasterOfChaos wrote:@KaiserJohan There are several question on Stackoverflow about C# and audio. http://stackoverflow.com/questions/3924178/real-time-audio-playback-from-mic-c/ should contain what you want. As codec you'll want to use Speex. There is CSpeex, a C# port of Speex (Afaik it supports both encoding and decoding) and probably there are some wrappers of the c library too.
That is amazing, thanks alot, it was just what I was looking for!
NAudio seems like a really nice library.
|
On November 14 2010 01:58 RoTaNiMoD wrote:Osmoses: I recall reading in a game programming book a long time ago a solution for exactly the sort of problem you describe. Number the destination tile as 0. Then number any tiles adjacent to the destination tile as 1. Number any tiles that a 1 could move to -- other than 0 -- a 2. Repeat until you have numbered your Hero's current tile. Then as long as the hero moves to a smaller numbered tile than his current one every move, he is on a correct path. A benefit is that equal paths have equal likelihood instead of arbitrarily having to decide which path your algorithm will take. Your example grid would look like this: ![[image loading]](http://rotanimod.com/numberedgrid.png) I think this could be what you are looking for... let us know. Wow. I'm seriously humbled here folks, thanks alot for your help! I had found the djikstra wiki page before, but I didn't know how to apply it to a grid where all the nodes were connected by a distance of 1. Just before I came here I had thought up what I felt was a somewhat accurate implementation of djikstra:
![[image loading]](http://imgur.com/dFjwh.png)
My idea was to keep track of all the corners with nodes, since corners are the key turning positions for units, and then just calculate the path from node to node to end and get the combination of nodes that offered the shortest path. Since start and end could be in the middle of large areas, they would generate an additional node themselves, by going in the direction of each other until they hit a wall.
![[image loading]](http://imgur.com/7NLtJ.png)
Gotta say though, I like the simple thing with incremented numbers much better. Thanks for actually taking the time to draw me a picture, Rotanimod, I really appreciate it =)
edit: oops, missed two corner nodes, but whatever you get the picture
edit: just got it working, thanks again <3
|
Germany2896 Posts
Osmoses: Your idea isn't bad. Tricks similar to this one are useful if either the grid has a high resolution/is large and thus normal A*/Dijkstra become too slow, or if you want to allow movement in arbitrary directions. It is quite a bit harder to implement though.
|
Got a C#/general question:
Consider I have a custom class,
public class test { private int a,b; private string c;
<get / set functions, and also serialize/deserialize methods so I can stick it the class object into a byte[] and send it over a socket> }
The problem is whenever I receive data on the other side, an event is triggered executes a receieve function which reads the data from a buffer and deserializes it back into my custom class test -- however, sometimes when I read from the buffer, number of bytes equals to 1½ or 2 classes might've been in the buffer already so when I deserialize, it deserializes into 1 complete class object, but throws the rest of the data away, so I dont get complete information.
The solution would be to know how big the class is, so I can only read from the buffer up to a certain index.... but does the size of the class object vary with the size of its variables? For example, would string c = "kekeke" generate a smaller object than c= "kekekeekkeekeke"? In that case, how do I know how much to read form the buffer?
|
On November 16 2010 21:50 KaiserJohan wrote: Got a C#/general question:
Consider I have a custom class,
public class test { private int a,b; private string c;
<get / set functions, and also serialize/deserialize methods so I can stick it the class object into a byte[] and send it over a socket> }
The problem is whenever I receive data on the other side, an event is triggered executes a receieve function which reads the data from a buffer and deserializes it back into my custom class test -- however, sometimes when I read from the buffer, number of bytes equals to 1½ or 2 classes might've been in the buffer already so when I deserialize, it deserializes into 1 complete class object, but throws the rest of the data away, so I dont get complete information.
The solution would be to know how big the class is, so I can only read from the buffer up to a certain index.... but does the size of the class object vary with the size of its variables? For example, would string c = "kekeke" generate a smaller object than c= "kekekeekkeekeke"? In that case, how do I know how much to read form the buffer?
First small naming thingie, you worded it incorrectly I would say, because size of the variable is size of the type of that variable, so for string variable size of that variable is size of the String class. You meant to ask if size of the class object vary with different values of its variables.
It kind of depends what you mean by size of the class object. Size of the class object itself is determined by the class declaration, so it is fixed. In your case variables a and b contribute 4 bytes to the size of the object, variable c contributes 4/8 bytes depending on your architecture (32 vs 64 bits) because string is basically a pointer(reference). Then you add the overhead and you have size of the class object. But for serialization/deserialization you are not interested only in the size of one object, but all objects that it references, in your case size of test and size of String.
|
On November 16 2010 21:50 KaiserJohan wrote: Got a C#/general question:
Consider I have a custom class,
public class test { private int a,b; private string c;
<get / set functions, and also serialize/deserialize methods so I can stick it the class object into a byte[] and send it over a socket> }
The problem is whenever I receive data on the other side, an event is triggered executes a receieve function which reads the data from a buffer and deserializes it back into my custom class test -- however, sometimes when I read from the buffer, number of bytes equals to 1½ or 2 classes might've been in the buffer already so when I deserialize, it deserializes into 1 complete class object, but throws the rest of the data away, so I dont get complete information.
The solution would be to know how big the class is, so I can only read from the buffer up to a certain index.... but does the size of the class object vary with the size of its variables? For example, would string c = "kekeke" generate a smaller object than c= "kekekeekkeekeke"? In that case, how do I know how much to read form the buffer?
Ahh, I didn't notice your last question String c = "kekeke" generates object of the same size as c= "kekekeekkeekeke", but that is because the actual "string" is not stored within the object, rather that object contains pointer to the memory storing the "string".
But as I said in serialization/deserialization size of the object is not the only thing you need, you need also sizes of referenced objects and size of memory blocks referenced by those, and that is not fixed. So basically if you do not send size of deserialized object along with the data, you cannot know beforehand how big a chunk of the buffer you need. You would need to parse the buffer yourself to determine where one deserialized object ends and another begins.
Sorry if the formulation is not clear, if this is so, hopefully someone else will put it more clearly. This case is one reason I think all programmers should know C/C++ (or some other language with pointers). Because C#, Java, PHP, .... shelters you from pointers, but they are actually there all the time, and many things make so much more sense when you know that. /end of rant 
|
Thats the fun with binary transfer. There are three common solutions for this with different places where they can be used:
1. Preceed the chunk of data with the bytes required to deserialize the object, so after serialization read the number of bytes, send the number of bytes followed by the serialized data. When reading, first read the first Bytes (depending on how many you used to represent the length, common are 4 bytes (a full Int32)), then read the number of bytes until you have enough, deserialize and continue from the beginning. Advantages: Relatively easy to implement and quite reliable. Disadvantage: You can not directly serialize into the stream, you first have to store it into memory, get the length and then write it. Problematic with big datasets
2. Use stop-signs. Stop signs (not sure about the correct term, it's been a while) are multiple bytes that represent the end of a chunk, for example if you define 0xFFFFFFFF as stop bytes then you read until you find those 4 bytes and serialize everything up until this point. Advantages: You can transfer a full chunk, send the 0xFFFFFFFF, then transfer the next chunk. Disadvantages: Doesn't work for data that could contain any value (so just about all binary data doesn't work)
3. Use fixed size data. Define that every object requires exactly x bytes, if it requires less than that then use serialization-neutral padding (often only works for text serialization like JSON or XML where you pad with spaces). Advantages: No need to count bytes at all, just take x bytes from the buffer and deserialize it. Disadvantages: Possibly a lot of overhead since you have to use the biggest possible size and pad everything smaller to this.
Networking looks like a lot of fun... up until you understand that there is no guarantee that a send() results in exactly one recv()/read(), there can be multiple (partial) chunks of data in a single read() or even just a small part of a single dataset, depending on a hundred factors (system configuration, network device, OS, networking library, routers, proxys, ...). You have to define ways to know how much you got exactly and even if you got it working you suddenly get another case where you get only half the data you expect and the other half is still waiting in the buffer. At least with TCP you don't need to worry (that much) about corrupt or mixed up packages.
Did i mention the joy of having a n-to-1 architecture like a webserver where you have to manage 1000 or more connections at the same time (up to the point where one thread per connection becomes a _very_ bad idea).
For my last project i decided to skip all that networking stuff and the headaches it can cause and just used the HttpListener, though it was only a one-way communication (client sends request for data and gets data back from the server, server didn't send requests/commands to the client).
I really recommend finding a library that takes this problems away unless you love spending hours on it (it _is_ fun and i love it, but you _will_ get headaches from it at some point).
|
On November 16 2010 22:46 mcc wrote:Show nested quote +On November 16 2010 21:50 KaiserJohan wrote: Got a C#/general question:
Consider I have a custom class,
public class test { private int a,b; private string c;
<get / set functions, and also serialize/deserialize methods so I can stick it the class object into a byte[] and send it over a socket> }
The problem is whenever I receive data on the other side, an event is triggered executes a receieve function which reads the data from a buffer and deserializes it back into my custom class test -- however, sometimes when I read from the buffer, number of bytes equals to 1½ or 2 classes might've been in the buffer already so when I deserialize, it deserializes into 1 complete class object, but throws the rest of the data away, so I dont get complete information.
The solution would be to know how big the class is, so I can only read from the buffer up to a certain index.... but does the size of the class object vary with the size of its variables? For example, would string c = "kekeke" generate a smaller object than c= "kekekeekkeekeke"? In that case, how do I know how much to read form the buffer?
Ahh, I didn't notice your last question String c = "kekeke" generates object of the same size as c= "kekekeekkeekeke", but that is because the actual "string" is not stored within the object, rather that object contains pointer to the memory storing the "string". But as I said in serialization/deserialization size of the object is not the only thing you need, you need also sizes of referenced objects and size of memory blocks referenced by those, and that is not fixed. So basically if you do not send size of deserialized object along with the data, you cannot know beforehand how big a chunk of the buffer you need. You would need to parse the buffer yourself to determine where one deserialized object ends and another begins. Sorry if the formulation is not clear, if this is so, hopefully someone else will put it more clearly. This case is one reason I think all programmers should know C/C++ (or some other language with pointers). Because C#, Java, PHP, .... shelters you from pointers, but they are actually there all the time, and many things make so much more sense when you know that. /end of rant 
@ mcc No, the formulation is crystal clear. I've done a fair bit of C programming during my studies, such as constructing a small OS in MIPS assembler / C, so I know a fair bit about pointers and memory management, although I prefer high-level languages such as C#. While I have done some C I havn't done any c++ programming actually, the windows API scares me with all the gazillion function/initialization and stuff; it's not the actual problems concerning memory and pointers (and wierd debugging messages) but I guess I got to get around learning that someday..
I can imagine something like this might've been easier to do in C++ though funny enough, when it comes to serialization; I mean wouldn't it just be to point at the begining of the allocated memory for that object and send it over the socket? I guess it could be done in C# too, with the Marshall class (I think it allows unmanaged code like pointers)
@morfildur: Out of all those 3, the first solution seems pretty sweet, I'm gonna have a go at it. Maybe second would work, but how do I know the serializer wont possibly add that chunk of 4 bytes?
I can feel your pain, debugging with my server app(since it's a client-server based chat, everything gets sent to server which redirects it to the other clients) is a pain with all the possible asynchronous calls for all the clients. I use TCP connection for this, but I'm thinking of adding a UDP socket for a networkstream for voice communication -- still wondering how that would work with encoding and decoding though, but maybe the provided decoder library would handle things such as end-markers for decoding for me...
But yes network programming is pretty fun to do atleast, I'm just making stuff for fun / practice, and a client-server chat/VOIP like mIRC+ventrilo combined is a good goal. (wouldn't have any ideas about another field/type of program besides network programmign that could be a good practice? especially a c++ based app)
|
|
|
|