예제 #1
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            Socket socket = (Socket)ar.AsyncState;

            try
            {
                int received = socket.EndReceive(ar);
                if (received <= 0)
                {
                    CloseClient(index);
                }
                else
                {
                    byte[] databuffer = new byte[received];
                    Array.Copy(_buffer, databuffer, received);
                    ProtocolManager.HandleNetworkInformation(index, databuffer);
                    socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket);
                }
            }
            catch (Exception ex)
            {
                Log.Add(LogSeverity.ERROR, "Exception thrown " + ex.ToString());
                CloseClient(index);
            }
        }
예제 #2
0
        private void HandleAlternativeConnection()
        {
            List <BgoProtocolReader> list = RecvMessage();

            foreach (BgoProtocolReader item in list)
            {
                Protocol.ProtocolID protocolID = (Protocol.ProtocolID)item.ReadByte();

                Log.Add(LogSeverity.INFO, Log.LogDir.In, string.Format("Protocol ID: {0} ({1})", (byte)protocolID, protocolID));

                //ushort num = item.ReadUInt16();
                try
                {
                    Protocol protocol = ProtocolManager.GetProtocol(protocolID);
                    protocol.ParseMessage(index, item);
                }
                catch (Exception ex)
                {
                    string text = "Couldn't handle message for " + protocolID + " Protocol. ";
                    if (ProtocolManager.GetProtocol(protocolID) == null)
                    {
                        text = text + protocolID + " Protocol is not (any more) registered. ";
                    }
                    text = text + "Caught Exception: " + ex.ToString();
                    Log.Add(LogSeverity.ERROR, text);
                }
            }
        }
예제 #3
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                int received = socket.EndReceive(ar);
                if (received <= 0)
                {
                    CloseClient(index);
                }
                else
                {
                    byte[] databuffer = new byte[received];
                    Buffer.BlockCopy(_buffer, 0, databuffer, 0, received);

                    byte[] incomingMsg = new byte[2];
                    incomingMsg[0] = databuffer[0];
                    incomingMsg[1] = databuffer[1];
                    ushort packetLength = (ushort)(BgoProtocolReader.ReadBufferSize(incomingMsg) + 2);
                    byte[] parsedArray  = new byte[packetLength];
                    Array.Copy(databuffer, 0, parsedArray, 0, packetLength);

                    // The first packet should always be right.
                    ProtocolManager.HandleNetworkInformation(index, parsedArray);

                    // If the first packet length isn't equal the received buffer length, then it received more than
                    // one packet on the buffer and it should be parsed to avoid any kind of problem.
                    // That's how Async works xd. There might be a better solution but this is what I could do.
                    if (packetLength != databuffer.Length)
                    {
                        ushort prevPacketLength = 0;
                        while (prevPacketLength + packetLength != databuffer.Length)
                        {
                            prevPacketLength += packetLength;
                            incomingMsg       = new byte[2];
                            incomingMsg[0]    = databuffer[prevPacketLength + 0];
                            incomingMsg[1]    = databuffer[prevPacketLength + 1];
                            packetLength      = (ushort)(BgoProtocolReader.ReadBufferSize(incomingMsg) + 2);
                            parsedArray       = new byte[packetLength];
                            Array.Copy(databuffer, prevPacketLength, parsedArray, 0, packetLength);

                            ProtocolManager.HandleNetworkInformation(index, parsedArray);
                        }
                    }

                    socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket);
                }
            }
            catch (Exception ex)
            {
                Log.Add(LogSeverity.ERROR, "Exception thrown " + ex);
                CloseClient(index);
            }
        }
예제 #4
0
 private static async Task Main(string[] args)
 {
     // Since the original game was based on protocols, we have to first setup the server protocols
     // to receive/send what the game wants.
     ProtocolManager.InitProtocols();
     // We are using a database called MongoDB instead of MySQL since Mongo makes it a lot easier to
     // work with unplanned things like the unknown number of cols and tables. So anyone can just run
     // a local Mongo that this server will handle everything else.
     Database.Database.Start();
     // The game had this weird (imo) card system so we have to remake it in order to make the game work.
     Catalogue.SetupCards();
     // Now we have to make the server accept connections and make it handle them.
     Server.InitServer();
     // This line should keep the server alive in order to use it.
     await Task.Delay(-1);
 }
예제 #5
0
 public static CommunityProtocol GetProtocol()
 {
     return(ProtocolManager.GetProtocol(ProtocolID.Community) as CommunityProtocol);
 }
예제 #6
0
 public static LoginProtocol GetProtocol()
 {
     return(ProtocolManager.GetProtocol(ProtocolID.Login) as LoginProtocol);
 }
예제 #7
0
 public static GameProtocol GetProtocol()
 {
     return(ProtocolManager.GetProtocol(ProtocolID.Game) as GameProtocol);
 }
예제 #8
0
 public static RoomProtocol GetProtocol()
 {
     return(ProtocolManager.GetProtocol(ProtocolID.Room) as RoomProtocol);
 }
예제 #9
0
 public static ShopProtocol GetProtocol()
 {
     return(ProtocolManager.GetProtocol(ProtocolID.Shop) as ShopProtocol);
 }
예제 #10
0
 public static PlayerProtocol GetProtocol()
 {
     return(ProtocolManager.GetProtocol(ProtocolID.Player) as PlayerProtocol);
 }
예제 #11
0
 public static DebugProtocol GetProtocol()
 {
     return(ProtocolManager.GetProtocol(ProtocolID.Debug) as DebugProtocol);
 }
 public static FeedbackProtocol GetProtocol()
 {
     return(ProtocolManager.GetProtocol(ProtocolID.Feedback) as FeedbackProtocol);
 }
예제 #13
0
 public static SettingProtocol GetProtocol()
 {
     return(ProtocolManager.GetProtocol(ProtocolID.Setting) as SettingProtocol);
 }
예제 #14
0
 public static SyncProtocol GetProtocol()
 {
     return(ProtocolManager.GetProtocol(ProtocolID.Sync) as SyncProtocol);
 }
 public static UniverseProtocol GetProtocol()
 {
     return(ProtocolManager.GetProtocol(ProtocolID.Universe) as UniverseProtocol);
 }
예제 #16
0
 public static StoryProtocol GetProtocol()
 {
     return(ProtocolManager.GetProtocol(ProtocolID.Story) as StoryProtocol);
 }
 public static SubscribeProtocol GetProtocol()
 {
     return(ProtocolManager.GetProtocol(ProtocolID.Subscribe) as SubscribeProtocol);
 }
예제 #18
0
 public static SceneProtocol GetProtocol()
 {
     return(ProtocolManager.GetProtocol(ProtocolID.Scene) as SceneProtocol);
 }
 public static CatalogueProtocol GetProtocol()
 {
     return(ProtocolManager.GetProtocol(ProtocolID.Catalogue) as CatalogueProtocol);
 }
예제 #20
0
        private void HandleConnection()
        {
            CancellationTokenSource tokenSource       = new CancellationTokenSource();
            CancellationToken       cancellationToken = tokenSource.Token;

            Task.Factory.StartNew(delegate
            {
                while (!cancellationToken.IsCancellationRequested)
                {
                    bool lockTaken = false;
                    try
                    {
                        Monitor.Enter(socket, ref lockTaken);
                        if (socket != null && socket.Connected)
                        {
                            if (socket.Poll(1000, SelectMode.SelectRead))
                            {
                                byte[] buffer = new byte[1];
                                if (socket.Receive(buffer, SocketFlags.Peek) == 0)
                                {
                                    throw new Exception("The socket has been closed");
                                }
                                byte[] array = new byte[2];
                                if (socket.Receive(array, 0, array.Length, SocketFlags.None) != 2)
                                {
                                    throw new Exception("Error receiving: packetLenght != 4");
                                }
                                int num       = BgoProtocolReader.ReadBufferSize(array);
                                byte[] array2 = new byte[num];
                                if (socket.Receive(array2, 0, array2.Length, SocketFlags.None) != num)
                                {
                                    throw new Exception("Error receiving: receive length != packet length");
                                }
                                BgoProtocolReader buffer2 = new BgoProtocolReader(array2);
                                ProtocolManager.HandleNetworkInformation(index, buffer2);
                            }
                        }
                        else
                        {
                            tokenSource.Cancel();
                            tokenSource.Dispose();
                        }
                    }
                    catch (Exception ex)
                    {
                        try
                        {
                            Console.WriteLine("Exception: " + ex.Message);
                            Log.Add(LogSeverity.INFO, string.Format("Connection from {0} has been terminated.", ip));
                            Server.GetSectorById(Character.PlayerShip.sectorId).LeaveSector(this, RemovingCause.JustRemoved);
                            socket.Shutdown(SocketShutdown.Both);
                            socket.Close();
                            closing   = true;
                            Character = null;
                            Server.Clients.Remove(index);
                        }
                        finally
                        {
                            if (socket != null)
                            {
                                socket.Dispose();
                            }
                        }
                    }
                    finally
                    {
                        if (lockTaken)
                        {
                            Monitor.Exit(socket);
                        }
                    }
                }
            }, cancellationToken).ContinueWith(delegate(Task x)
            {
                x.Dispose();
            });
        }