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); } }
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); } } }
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); } }
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); }
public static CommunityProtocol GetProtocol() { return(ProtocolManager.GetProtocol(ProtocolID.Community) as CommunityProtocol); }
public static LoginProtocol GetProtocol() { return(ProtocolManager.GetProtocol(ProtocolID.Login) as LoginProtocol); }
public static GameProtocol GetProtocol() { return(ProtocolManager.GetProtocol(ProtocolID.Game) as GameProtocol); }
public static RoomProtocol GetProtocol() { return(ProtocolManager.GetProtocol(ProtocolID.Room) as RoomProtocol); }
public static ShopProtocol GetProtocol() { return(ProtocolManager.GetProtocol(ProtocolID.Shop) as ShopProtocol); }
public static PlayerProtocol GetProtocol() { return(ProtocolManager.GetProtocol(ProtocolID.Player) as PlayerProtocol); }
public static DebugProtocol GetProtocol() { return(ProtocolManager.GetProtocol(ProtocolID.Debug) as DebugProtocol); }
public static FeedbackProtocol GetProtocol() { return(ProtocolManager.GetProtocol(ProtocolID.Feedback) as FeedbackProtocol); }
public static SettingProtocol GetProtocol() { return(ProtocolManager.GetProtocol(ProtocolID.Setting) as SettingProtocol); }
public static SyncProtocol GetProtocol() { return(ProtocolManager.GetProtocol(ProtocolID.Sync) as SyncProtocol); }
public static UniverseProtocol GetProtocol() { return(ProtocolManager.GetProtocol(ProtocolID.Universe) as UniverseProtocol); }
public static StoryProtocol GetProtocol() { return(ProtocolManager.GetProtocol(ProtocolID.Story) as StoryProtocol); }
public static SubscribeProtocol GetProtocol() { return(ProtocolManager.GetProtocol(ProtocolID.Subscribe) as SubscribeProtocol); }
public static SceneProtocol GetProtocol() { return(ProtocolManager.GetProtocol(ProtocolID.Scene) as SceneProtocol); }
public static CatalogueProtocol GetProtocol() { return(ProtocolManager.GetProtocol(ProtocolID.Catalogue) as CatalogueProtocol); }
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(); }); }