Sign in to follow this  

Shared Reading and Writing.

This topic is 4214 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm in the midst of alpha testing of my MMORPG Fleurin (Actually, its being done right now, but the server just crashed with a bunch of players online and I'm not sure how to fix it. The problem is... My webserver and my game-server both tried to access a file at relatively the same time. And it threw an IOException(C#). I have no clue how to fix this, any ideas? (Yes, I'm under alot of pressure atm)

Share this post


Link to post
Share on other sites
I would say run a check to see if the file is already open before trying to open it, but I'm not sure this can be done if you're talking about two seperate programs attempting to access the same file at the same time. It seems like it would be hard to stop two seperate programs from trying to perform different operations on the same file at the same time.

So yes, to answer your question, I really don't have all that much to add in the way of constructive help, but this is a question I'd like to know an answer to, so I thought I'd throw my two cents in there.

Vopisk

Share this post


Link to post
Share on other sites
I have a cast of people waiting for me to "fix this and restart the server", so I appreciate any ideas I can get.

Checking the access sounds like a tangible idea. I'll do some googling for functions to implement the lock check.

Edit: I can't find any such functions in C#.

Share this post


Link to post
Share on other sites
Mutexes work across processes (you just have to give them a unique name that's the same in both apps - I use GUIDs)

Share this post


Link to post
Share on other sites
I did this...

        /* Set the text writer to output. */
//MyTextReader = new StreamReader(TheFileName); /* <-- Old code */
MyFS = new FileStream(TheFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
MyTextReader = new StreamReader(MyFS);


... and hoping that it by setting the FileShare property, that it will let me have the server write to the file while the web server reads it.

At the very least, I have a few minutes to do some research before the server crashes again.

@Nypyren
Thanks, I'll look into that right now.

Share this post


Link to post
Share on other sites
Good call on the mutexes Nypyren, a quick google turned up a wealth of information and this is the obvious answer to the problem at hand. To the OP it still is not going to let you read and write at the same time, files don't like to be shared between two programs at once generally in my experience, unless the program is actually just making a copy of the file and doing something to it, closing the actual file to free it up for another program/function to access it. But at least it should keep you from crashing and at most I'd figure you're looking at a momentary hitch in your giddy-up.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
lol have one process poll the other process to access the file!! yeah! lol dont listen to me btw :P

Share this post


Link to post
Share on other sites
Is it important that I mention that only one process needs to write/read to the file; the other process only reads?

Share this post


Link to post
Share on other sites
Guys, it would appear that the FileShare attribute allows multiple processes to read/write to the file.

Have a look.. System.IO.FileShare

Although multiple writes would be very troublesome, the fact that only one of my threads does any writing means this may actually work.

Can anyone with experience in this field verify it for me?

Share this post


Link to post
Share on other sites
When you're writing to the file, are you appending, or are you rewriting all the data in the file?

Either way, you'll have to be sure not to let the readers read data that's only partially written. That's where the mutex might come in handy - although if you have more than two processes, you might need one of the other syncronization classes in case you want to have multiple readers able to read without waiting for a "single" resource like a mutex. (although I've heard ReaderWriterLock sucks, I don't have any experience using it).

(Other than that, the FileShare.ReadWrite will probably work fine if you make sure your write buffer is flushed or the stream is closed before readers are allowed to read).

Share this post


Link to post
Share on other sites
Quote:
Original post by Nypyren
Other than that, the FileShare.ReadWrite will probably work fine if you make sure your write buffer is flushed or the stream is closed before readers are allowed to read).


Considering that the two threads are running independent of eachother, I can't make sure my write buffer is flushed before a reader reads it.

Are you absolutely certian that there is a possibility that the read will read the partially written data?

Share this post


Link to post
Share on other sites
It will try and read partially written data if the stars align and it happens to be trying to read the last information from the file at the same time as it is being written. We're talking down to the ms's here, but with two seperately running processes, there's no telling when one or the other will try and perform their reading or writing functions.

Share this post


Link to post
Share on other sites
Bah, I knew my cheesy text file forum and text file characters would come back to bite me. Oh why didn't I listen to those crazy people shouting 'Use MySQL'. [bawling]

Mutex is poorly documented, and probably would be just as difficult for me to implement as say.. MySQL. I'm going to need some time to think about this. [depressed]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
...Mutex is poorly documented...

There are hundreds of thousands of web pages with info and even more books!!!!!

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Quote:
...Mutex is poorly documented...

There are hundreds of thousands of web pages with info and even more books!!!!!


When you consider the size of the internet, hundreds of thousands seems soo little. Also, google didn't return any matches that I could follow along with.

Obviously Mutex is properly documented, its just I can't find tutorials that I can understand. I might just swallow the 'glow in the dark' pill and implement MySQL.

Share this post


Link to post
Share on other sites
Wait a minute!

Why don't I just have the server make duplicates every minute of all the characters online into a backup folder which is read only by the web server.

To notify the server when a backup folder is flushed, I'll create a Done.txt file to signal it.

The web server will be a minute behind on the data, but thats perfectly fine given my critiera.

Its soo simple... [smile]

Share this post


Link to post
Share on other sites

This topic is 4214 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this