Changed folder from Proxy to World. Did some nlog configing.

This commit is contained in:
Filip Maj 2016-08-28 14:25:37 -04:00
parent 364ab40b3f
commit bd26a71fef
21 changed files with 32 additions and 24 deletions

View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient"/>
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
</DbProviderFactories>
</system.data></configuration>

View file

@ -0,0 +1,44 @@
using FFXIVClassic.Common;
using System;
using System.IO;
namespace FFXIVClassic_World_Server
{
class ConfigConstants
{
public static String OPTIONS_BINDIP;
public static String OPTIONS_PORT;
public static bool OPTIONS_TIMESTAMP = false;
public static String DATABASE_HOST;
public static String DATABASE_PORT;
public static String DATABASE_NAME;
public static String DATABASE_USERNAME;
public static String DATABASE_PASSWORD;
public static bool Load()
{
Program.Log.Info("Loading config.ini");
if (!File.Exists("./config.ini"))
{
Program.Log.Error("FILE NOT FOUND!");
return false;
}
INIFile configIni = new INIFile("./config.ini");
ConfigConstants.OPTIONS_BINDIP = configIni.GetValue("General", "server_ip", "127.0.0.1");
ConfigConstants.OPTIONS_PORT = configIni.GetValue("General", "server_port", "54992");
ConfigConstants.OPTIONS_TIMESTAMP = configIni.GetValue("General", "showtimestamp", "true").ToLower().Equals("true");
ConfigConstants.DATABASE_HOST = configIni.GetValue("Database", "host", "");
ConfigConstants.DATABASE_PORT = configIni.GetValue("Database", "port", "");
ConfigConstants.DATABASE_NAME = configIni.GetValue("Database", "database", "");
ConfigConstants.DATABASE_USERNAME = configIni.GetValue("Database", "username", "");
ConfigConstants.DATABASE_PASSWORD = configIni.GetValue("Database", "password", "");
return true;
}
}
}

View file

@ -0,0 +1,67 @@
using System;
using System.Net.Sockets;
using System.Collections.Concurrent;
using System.Net;
using FFXIVClassic.Common;
using FFXIVClassic_World_Server.DataObjects;
namespace FFXIVClassic_World_Server
{
class ClientConnection
{
//Connection stuff
public Socket socket;
public byte[] buffer;
private BlockingCollection<BasePacket> SendPacketQueue = new BlockingCollection<BasePacket>(1000);
public int lastPartialSize = 0;
//Instance Stuff
public Session owner;
public void QueuePacket(BasePacket packet)
{
SendPacketQueue.Add(packet);
}
public void QueuePacket(SubPacket subpacket, bool isAuthed, bool isEncrypted)
{
SendPacketQueue.Add(BasePacket.CreatePacket(subpacket, isAuthed, isEncrypted));
}
public void FlushQueuedSendPackets()
{
if (!socket.Connected)
return;
while (SendPacketQueue.Count > 0)
{
BasePacket packet = SendPacketQueue.Take();
byte[] packetBytes = packet.GetPacketBytes();
try
{
socket.Send(packetBytes);
}
catch (Exception e)
{ Program.Log.Error("Weird case, socket was d/ced: {0}", e); }
}
}
public String GetAddress()
{
return String.Format("{0}:{1}", (socket.RemoteEndPoint as IPEndPoint).Address, (socket.RemoteEndPoint as IPEndPoint).Port);
}
public bool IsConnected()
{
return (socket.Poll(1, SelectMode.SelectRead) && socket.Available == 0);
}
public void Disconnect()
{
if (socket.Connected)
socket.Disconnect(false);
}
}
}

View file

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace FFXIVClassic_World_Server.DataObjects
{
class Session
{
public enum Channel {ZONE, CHAT};
public readonly uint sessionId;
public readonly ClientConnection clientConnection;
public readonly Channel type;
public ZoneServer routing1, routing2;
public Session(uint sessionId, ClientConnection connection, Channel type)
{
this.sessionId = sessionId;
this.clientConnection = connection;
this.type = type;
connection.owner = this;
}
}
}

View file

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using FFXIVClassic.Common;
namespace FFXIVClassic_World_Server.DataObjects
{
class ZoneServer
{
public readonly string zoneServerIp;
public readonly int zoneServerPort;
public readonly int[] ownedZoneIds;
public bool isConnected = false;
public Socket zoneServerConnection;
public ZoneServer(string ip, int port)
{
zoneServerIp = ip;
zoneServerPort = port;
}
public void Connect()
{
Program.Log.Info("Connecting to zone server @ {0}:{1}", zoneServerIp, zoneServerPort);
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(zoneServerIp), zoneServerPort);
zoneServerConnection = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
try
{
zoneServerConnection.Connect(remoteEP);
isConnected = true;
}
catch (Exception e)
{ Program.Log.Error("Failed to connect"); return; }
}
public void SendPacket(SubPacket subpacket)
{
if (isConnected)
{
byte[] packetBytes = subpacket.GetBytes();
try
{
zoneServerConnection.Send(packetBytes);
}
catch (Exception e)
{ Program.Log.Error("Weird case, socket was d/ced: {0}", e); }
}
}
}
}

View file

@ -0,0 +1,105 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{3067889D-8A50-40D6-9CD5-23AA8EA96F26}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>FFXIVClassic_World_Server</RootNamespace>
<AssemblyName>FFXIVClassic World Server</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Cyotek.Collections.Generic.CircularBuffer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=58daa28b0b2de221, processorArchitecture=MSIL">
<HintPath>..\packages\Cyotek.CircularBuffer.1.0.0.0\lib\net20\Cyotek.Collections.Generic.CircularBuffer.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Dapper, Version=1.40.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapper.1.42\lib\net45\Dapper.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MySql.Data, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
<HintPath>..\packages\MySql.Data.6.9.8\lib\net45\MySql.Data.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\packages\NLog.4.3.5\lib\net45\NLog.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="RabbitMQ.Client, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89e7d7c5feba84ce, processorArchitecture=MSIL">
<HintPath>..\packages\RabbitMQ.Client.4.0.0\lib\net451\RabbitMQ.Client.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ConfigConstants.cs" />
<Compile Include="DataObjects\ClientConnection.cs" />
<Compile Include="DataObjects\ZoneServer.cs" />
<Compile Include="DataObjects\Session.cs" />
<Compile Include="PacketProcessor.cs" />
<Compile Include="Packets\Receive\HelloPacket.cs" />
<Compile Include="Packets\Send\_0x2Packet.cs" />
<Compile Include="Packets\Send\_0x7Packet.cs" />
<Compile Include="Packets\Send\_0x8PingPacket.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Server.cs" />
<Compile Include="WorldMaster.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<Content Include="NLog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Include="NLog.xsd">
<SubType>Designer</SubType>
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FFXIVClassic Common Class Lib\FFXIVClassic Common Class Lib.csproj">
<Project>{3a3d6626-c820-4c18-8c81-64811424f20e}</Project>
<Name>FFXIVClassic Common Class Lib</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View file

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<!-- optional, add some variabeles
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="myvar" value="myvalue" />
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets async="true">
<!--
add your targets here
See https://github.com/nlog/NLog/wiki/Targets for possible targets.
See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
-->
<!--
Write events to a file with the date in the filename.
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
-->
<!--<target xsi:type="ColoredConsole" name="console" layout="[${longdate}] [${uppercase:${level}}] ${message}" />-->
<target xsi:type="File" name="file" fileName="${basedir}/logs/${shortdate}/map.log"
layout="[${date:format=dd MMM yyyy HH\:mm\:ss.fff}] [${uppercase:${level}}] ${message}" />
<target xsi:type="ColoredConsole" name="console"
layout="[${date:format=dd MMM yyyy HH\:mm\:ss.fff}] [${uppercase:${level}}] ${message}" />
<target xsi:type="ColoredConsole" name="packets"
layout="${message}">
<highlight-row
condition="equals('${logger}', 'FFXIVClassic.Common.BasePacket') and equals('${event-context:item=color}', '6')"
backgroundColor="DarkYellow" foregroundColor="NoChange" />
<highlight-row
condition="equals('${logger}', 'FFXIVClassic.Common.SubPacket') and equals('${event-context:item=color}', '4')"
backgroundColor="DarkRed" foregroundColor="NoChange" />
<highlight-row
condition="equals('${logger}', 'FFXIVClassic.Common.SubPacket') and equals('${event-context:item=color}', '5')"
backgroundColor="DarkMagenta" foregroundColor="NoChange" />
</target>
</targets>
<rules>
<!-- add your logging rules here -->
<logger name='*' minlevel='Trace' writeTo='file' />
<logger name='FFXIVClassic_World_Server.Program' minlevel='Trace' writeTo='console' />
<!--
Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"
<logger name="*" minlevel="Debug" writeTo="f" />
-->
</rules>
</nlog>

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,91 @@
using FFXIVClassic.Common;
using FFXIVClassic_World_Server.DataObjects;
using FFXIVClassic_World_Server.Packets.Receive;
using FFXIVClassic_World_Server.Packets.Send;
using FFXIVClassic_World_Server.Packets.Send.Login;
using System;
using System.Collections.Generic;
using System.IO;
namespace FFXIVClassic_World_Server
{
class PacketProcessor
{
/*
Session Creation:
Get 0x1 from server
Send 0x7
Send 0x2
Zone Change:
Send 0x7
Get 0x8 - Wait??
Send 0x2
*/
Server mServer;
public PacketProcessor(Server server)
{
mServer = server;
}
public void ProcessPacket(ClientConnection client, BasePacket packet)
{
if (packet.header.isCompressed == 0x01)
BasePacket.DecompressPacket(ref packet);
List<SubPacket> subPackets = packet.GetSubpackets();
foreach (SubPacket subpacket in subPackets)
{
subpacket.DebugPrintSubPacket();
//Initial Connect Packet, Create session
if (subpacket.header.type == 0x01)
{
HelloPacket hello = new HelloPacket(packet.data);
if (packet.header.connectionType == BasePacket.TYPE_ZONE)
mServer.AddSession(client, Session.Channel.ZONE, hello.sessionId);
else if (packet.header.connectionType == BasePacket.TYPE_CHAT)
mServer.AddSession(client, Session.Channel.CHAT, hello.sessionId);
client.QueuePacket(_0x7Packet.BuildPacket(0x0E016EE5), true, false);
client.QueuePacket(_0x2Packet.BuildPacket(hello.sessionId), true, false);
}
//Ping from World Server
else if (subpacket.header.type == 0x07)
{
SubPacket init = _0x8PingPacket.BuildPacket(client.owner.sessionId);
client.QueuePacket(BasePacket.CreatePacket(init, true, false));
}
//Zoning Related
else if (subpacket.header.type == 0x08)
{
//Response, client's current [actorID][time]
//BasePacket init = Login0x7ResponsePacket.BuildPacket(BitConverter.ToUInt32(packet.data, 0x10), Utils.UnixTimeStampUTC(), 0x07);
//client.QueuePacket(init);
packet.DebugPrintPacket();
}
//Game Message
else if (subpacket.header.type == 0x03)
{
//Send to the correct zone server
uint targetSession = subpacket.header.targetId;
if (mServer.GetSession(targetSession).routing1 != null)
mServer.GetSession(targetSession).routing1.SendPacket(subpacket);
if (mServer.GetSession(targetSession).routing2 != null)
mServer.GetSession(targetSession).routing2.SendPacket(subpacket);
}
else
packet.DebugPrintPacket();
}
}
}
}

View file

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FFXIVClassic_World_Server.Packets.Receive
{
class HelloPacket
{
public bool invalidPacket = false;
public uint sessionId;
public HelloPacket(byte[] data)
{
using (MemoryStream mem = new MemoryStream(data))
{
using (BinaryReader binReader = new BinaryReader(mem))
{
try
{
byte[] readIn = new byte[12];
binReader.BaseStream.Seek(0x14, SeekOrigin.Begin);
binReader.Read(readIn, 0, 12);
sessionId = UInt32.Parse(Encoding.ASCII.GetString(readIn));
}
catch (Exception)
{
invalidPacket = true;
}
}
}
}
}
}

View file

@ -0,0 +1,44 @@
using FFXIVClassic.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FFXIVClassic_World_Server.Packets.Send
{
class _0x2Packet
{
public const ushort OPCODE = 0x0002;
public const uint PACKET_SIZE = 0x38;
public static SubPacket BuildPacket(uint actorID)
{
byte[] data = new byte[PACKET_SIZE];
using (MemoryStream mem = new MemoryStream(data))
{
using (BinaryWriter binWriter = new BinaryWriter(mem))
{
try
{
binWriter.Write((UInt32)actorID);
}
catch (Exception)
{ }
}
}
byte[] reply2Data = {
0x66, 0x00, 0x00, 0x00, 0xC8, 0xD6, 0xAF, 0x2B, 0x38, 0x2B, 0x5F, 0x26, 0xB8, 0x8D, 0xF0, 0x2B,
0xC8, 0xFD, 0x85, 0xFE, 0xA8, 0x7C, 0x5B, 0x09, 0x38, 0x2B, 0x5F, 0x26, 0xC8, 0xD6, 0xAF, 0x2B,
0xB8, 0x8D, 0xF0, 0x2B, 0x88, 0xAF, 0x5E, 0x26
};
data = reply2Data;
return new SubPacket(false, OPCODE, 0, 0, data);
}
}
}

View file

@ -0,0 +1,37 @@
using FFXIVClassic.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FFXIVClassic_World_Server.Packets.Send
{
class _0x7Packet
{
public const ushort OPCODE = 0x0007;
public const uint PACKET_SIZE = 0x18;
public static SubPacket BuildPacket(uint actorID)
{
byte[] data = new byte[PACKET_SIZE];
using (MemoryStream mem = new MemoryStream(data))
{
using (BinaryWriter binWriter = new BinaryWriter(mem))
{
try
{
binWriter.Write((UInt32)actorID);
binWriter.Write((UInt32)Utils.UnixTimeStampUTC());
}
catch (Exception)
{ }
}
}
return new SubPacket(false, OPCODE, 0, 0, data);
}
}
}

View file

@ -0,0 +1,33 @@
using FFXIVClassic.Common;
using System;
using System.IO;
namespace FFXIVClassic_World_Server.Packets.Send.Login
{
class _0x8PingPacket
{
public const ushort OPCODE = 0x0008;
public const uint PACKET_SIZE = 0x18;
public static SubPacket BuildPacket(uint actorID)
{
byte[] data = new byte[PACKET_SIZE];
using (MemoryStream mem = new MemoryStream(data))
{
using (BinaryWriter binWriter = new BinaryWriter(mem))
{
try
{
binWriter.Write((UInt32)actorID);
binWriter.Write((UInt32)Utils.UnixTimeStampUTC());
}
catch (Exception)
{}
}
}
return new SubPacket(false, OPCODE, 0, 0, data);
}
}
}

View file

@ -0,0 +1,72 @@
using MySql.Data.MySqlClient;
using NLog;
using NLog.Fluent;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FFXIVClassic_World_Server
{
class Program
{
public static Logger Log;
static void Main(string[] args)
{
// set up logging
Log = LogManager.GetCurrentClassLogger();
#if DEBUG
TextWriterTraceListener myWriter = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(myWriter);
#endif
bool startServer = true;
Log.Info("==================================");
Log.Info("FFXIV Classic World Server");
Log.Info("Version: 0.0.1");
Log.Info("==================================");
//Load Config
if (!ConfigConstants.Load())
startServer = false;
//Test DB Connection
Log.Info("Testing DB connection... ");
using (MySqlConnection conn = new MySqlConnection(String.Format("Server={0}; Port={1}; Database={2}; UID={3}; Password={4}", ConfigConstants.DATABASE_HOST, ConfigConstants.DATABASE_PORT, ConfigConstants.DATABASE_NAME, ConfigConstants.DATABASE_USERNAME, ConfigConstants.DATABASE_PASSWORD)))
{
try
{
conn.Open();
conn.Close();
Log.Info("Connection ok.");
}
catch (MySqlException e)
{
Log.Error(e.ToString());
startServer = false;
}
}
//Start server if A-OK
if (startServer)
{
Server server = new Server();
server.StartServer();
while (startServer)
{
String input = Console.ReadLine();
Log.Info("[Console Input] " + input);
//cp.DoCommand(input, null);
}
}
Program.Log.Info("Press any key to continue...");
Console.ReadKey();
}
}
}

View file

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("FFXIVClassic Proxy Server")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FFXIVClassic Proxy Server")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("3067889d-8a50-40d6-9cd5-23aa8ea96f26")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View file

@ -0,0 +1,311 @@
using FFXIVClassic.Common;
using FFXIVClassic_World_Server.DataObjects;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
namespace FFXIVClassic_World_Server
{
class Server
{
public const int FFXIV_MAP_PORT = 54992;
public const int BUFFER_SIZE = 0xFFFF; //Max basepacket size is 0xFFFF
public const int BACKLOG = 100;
public const int HEALTH_THREAD_SLEEP_TIME = 5;
private static Server mSelf;
private Socket mServerSocket;
WorldManager mWorldManager;
PacketProcessor mPacketProcessor;
private List<ClientConnection> mConnectionList = new List<ClientConnection>();
private Dictionary<uint, Session> mZoneSessionList = new Dictionary<uint, Session>();
private Dictionary<uint, Session> mChatSessionList = new Dictionary<uint, Session>();
public Server()
{
mSelf = this;
}
public static Server GetServer()
{
return mSelf;
}
public bool StartServer()
{
mPacketProcessor = new PacketProcessor(this);
mWorldManager = new WorldManager(this);
mWorldManager.LoadZoneServerList();
mWorldManager.ConnectToZoneServers();
IPEndPoint serverEndPoint = new System.Net.IPEndPoint(IPAddress.Parse(ConfigConstants.OPTIONS_BINDIP), int.Parse(ConfigConstants.OPTIONS_PORT));
try
{
mServerSocket = new System.Net.Sockets.Socket(serverEndPoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
}
catch (Exception e)
{
throw new ApplicationException("Could not Create socket, check to make sure not duplicating port", e);
}
try
{
mServerSocket.Bind(serverEndPoint);
mServerSocket.Listen(BACKLOG);
}
catch (Exception e)
{
throw new ApplicationException("Error occured while binding socket, check inner exception", e);
}
try
{
mServerSocket.BeginAccept(new AsyncCallback(AcceptCallback), mServerSocket);
}
catch (Exception e)
{
throw new ApplicationException("Error occured starting listeners, check inner exception", e);
}
Console.ForegroundColor = ConsoleColor.White;
Program.Log.Info("World Server accepting connections @ {0}:{1}", (mServerSocket.LocalEndPoint as IPEndPoint).Address, (mServerSocket.LocalEndPoint as IPEndPoint).Port);
Console.ForegroundColor = ConsoleColor.Gray;
return true;
}
#region Socket Handling
private void AcceptCallback(IAsyncResult result)
{
ClientConnection conn = null;
Socket socket = (System.Net.Sockets.Socket)result.AsyncState;
try
{
conn = new ClientConnection();
conn.socket = socket.EndAccept(result);
conn.buffer = new byte[BUFFER_SIZE];
lock (mConnectionList)
{
mConnectionList.Add(conn);
}
Program.Log.Info("Connection {0}:{1} has connected.", (conn.socket.RemoteEndPoint as IPEndPoint).Address, (conn.socket.RemoteEndPoint as IPEndPoint).Port);
//Queue recieving of data from the connection
conn.socket.BeginReceive(conn.buffer, 0, conn.buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), conn);
//Queue the accept of the next incomming connection
mServerSocket.BeginAccept(new AsyncCallback(AcceptCallback), mServerSocket);
}
catch (SocketException)
{
if (conn != null)
{
lock (mConnectionList)
{
mConnectionList.Remove(conn);
}
}
mServerSocket.BeginAccept(new AsyncCallback(AcceptCallback), mServerSocket);
}
catch (Exception)
{
if (conn != null)
{
lock (mConnectionList)
{
mConnectionList.Remove(conn);
}
}
mServerSocket.BeginAccept(new AsyncCallback(AcceptCallback), mServerSocket);
}
}
public void AddSession(ClientConnection connection, Session.Channel type, uint id)
{
Session session = new Session(id, connection, type);
switch (type)
{
case Session.Channel.ZONE:
if (!mZoneSessionList.ContainsKey(id))
mZoneSessionList.Add(id, session);
break;
case Session.Channel.CHAT:
if (!mChatSessionList.ContainsKey(id))
mChatSessionList.Add(id, session);
break;
}
}
public void RemoveSession(Session.Channel type, uint id)
{
switch (type)
{
case Session.Channel.ZONE:
if (mZoneSessionList.ContainsKey(id))
{
mZoneSessionList[id].clientConnection.Disconnect();
mConnectionList.Remove(mZoneSessionList[id].clientConnection);
mZoneSessionList.Remove(id);
}
break;
case Session.Channel.CHAT:
if (mChatSessionList.ContainsKey(id))
{
mChatSessionList[id].clientConnection.Disconnect();
mConnectionList.Remove(mChatSessionList[id].clientConnection);
mChatSessionList.Remove(id);
}
break;
}
}
public Session GetSession(uint targetSession, Session.Channel type = Session.Channel.ZONE)
{
switch (type)
{
case Session.Channel.ZONE:
if (mZoneSessionList.ContainsKey(targetSession))
return mZoneSessionList[targetSession];
break;
case Session.Channel.CHAT:
if (mChatSessionList.ContainsKey(targetSession))
return mChatSessionList[targetSession];
break;
}
return null;
}
/// <summary>
/// Receive Callback. Reads in incoming data, converting them to base packets. Base packets are sent to be parsed. If not enough data at the end to build a basepacket, move to the beginning and prepend.
/// </summary>
/// <param name="result"></param>
private void ReceiveCallback(IAsyncResult result)
{
ClientConnection conn = (ClientConnection)result.AsyncState;
//Check if disconnected
if ((conn.socket.Poll(1, SelectMode.SelectRead) && conn.socket.Available == 0))
{
lock (mConnectionList)
{
mConnectionList.Remove(conn);
}
return;
}
try
{
int bytesRead = conn.socket.EndReceive(result);
bytesRead += conn.lastPartialSize;
if (bytesRead >= 0)
{
int offset = 0;
//Build packets until can no longer or out of data
while (true)
{
BasePacket basePacket = BuildPacket(ref offset, conn.buffer, bytesRead);
//If can't build packet, break, else process another
if (basePacket == null)
break;
else
{
mPacketProcessor.ProcessPacket(conn, basePacket);
}
}
//Not all bytes consumed, transfer leftover to beginning
if (offset < bytesRead)
Array.Copy(conn.buffer, offset, conn.buffer, 0, bytesRead - offset);
conn.lastPartialSize = bytesRead - offset;
//Build any queued subpackets into basepackets and send
conn.FlushQueuedSendPackets();
if (offset < bytesRead)
//Need offset since not all bytes consumed
conn.socket.BeginReceive(conn.buffer, bytesRead - offset, conn.buffer.Length - (bytesRead - offset), SocketFlags.None, new AsyncCallback(ReceiveCallback), conn);
else
//All bytes consumed, full buffer available
conn.socket.BeginReceive(conn.buffer, 0, conn.buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), conn);
}
else
{
lock (mConnectionList)
{
mConnectionList.Remove(conn);
}
}
}
catch (SocketException)
{
if (conn.socket != null)
{
lock (mConnectionList)
{
mConnectionList.Remove(conn);
}
}
}
}
/// <summary>
/// Builds a packet from the incoming buffer + offset. If a packet can be built, it is returned else null.
/// </summary>
/// <param name="offset">Current offset in buffer.</param>
/// <param name="buffer">Incoming buffer.</param>
/// <returns>Returns either a BasePacket or null if not enough data.</returns>
public BasePacket BuildPacket(ref int offset, byte[] buffer, int bytesRead)
{
BasePacket newPacket = null;
//Too small to even get length
if (bytesRead <= offset)
return null;
ushort packetSize = BitConverter.ToUInt16(buffer, offset);
//Too small to whole packet
if (bytesRead < offset + packetSize)
return null;
if (buffer.Length < offset + packetSize)
return null;
try
{
newPacket = new BasePacket(buffer, ref offset);
}
catch (OverflowException)
{
return null;
}
return newPacket;
}
#endregion
public WorldManager GetWorldManager()
{
return mWorldManager;
}
}
}

View file

@ -0,0 +1,144 @@
using FFXIVClassic.Common;
using FFXIVClassic_World_Server.DataObjects;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace FFXIVClassic_World_Server
{
class WorldManager
{
private Server mServer;
private Dictionary<string, ZoneServer> mZoneServerList;
public WorldManager(Server server)
{
mServer = server;
}
public void LoadZoneServerList()
{
mZoneServerList = new Dictionary<string, ZoneServer>();
using (MySqlConnection conn = new MySqlConnection(String.Format("Server={0}; Port={1}; Database={2}; UID={3}; Password={4}", ConfigConstants.DATABASE_HOST, ConfigConstants.DATABASE_PORT, ConfigConstants.DATABASE_NAME, ConfigConstants.DATABASE_USERNAME, ConfigConstants.DATABASE_PASSWORD)))
{
try
{
conn.Open();
string query = @"
SELECT
serverIp,
serverPort
FROM server_zones
WHERE serverIp IS NOT NULL";
MySqlCommand cmd = new MySqlCommand(query, conn);
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
string ip = reader.GetString(0);
int port = reader.GetInt32(1);
string address = ip + ":" + port;
if (!mZoneServerList.ContainsKey(address))
{
ZoneServer zone = new ZoneServer(ip, port);
mZoneServerList.Add(address, zone);
}
}
}
}
catch (MySqlException e)
{ Console.WriteLine(e); }
finally
{
conn.Dispose();
}
}
}
public void ConnectToZoneServers()
{
Program.Log.Info("--------------------------");
Program.Log.Info("Connecting to zone servers");
Program.Log.Info("--------------------------");
foreach (ZoneServer zs in mZoneServerList.Values)
{
zs.Connect();
}
}
//Moves the actor to the new zone if exists. No packets are sent nor position changed.
public void DoSeamlessZoneServerChange(Session session, uint destinationZoneId)
{
}
//Moves actor to new zone, and sends packets to spawn at the given zone entrance
public void DoZoneServerChange(Session session, uint zoneEntrance)
{
/*
->Tell old server to save session info and remove session. Start zone packets.
->Update the position to zoneEntrance
->Update routing
->Tell new server to load session info and add session. Send end zone packets.
*/
}
//Moves actor to new zone, and sends packets to spawn at the given coords.
public void DoZoneServerChange(Session session, uint destinationZoneId, string destinationPrivateArea, byte spawnType, float spawnX, float spawnY, float spawnZ, float spawnRotation)
{
/*
->Tell old server to save session info and remove
->Update the position to params
->Update routing
->Tell new server to load session info and add
*/
}
//Login Zone In
public void DoLogin(Session session)
{
/*
->Update routing
->Tell new server to load session info and add
*/
}
public class ZoneEntrance
{
public uint zoneId;
public string privateAreaName;
public byte spawnType;
public float spawnX;
public float spawnY;
public float spawnZ;
public float spawnRotation;
public ZoneEntrance(uint zoneId, string privateAreaName, byte spawnType, float x, float y, float z, float rot)
{
this.zoneId = zoneId;
this.privateAreaName = privateAreaName;
this.spawnType = spawnType;
this.spawnX = x;
this.spawnY = y;
this.spawnZ = z;
this.spawnRotation = rot;
}
}
}
}

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Cyotek.CircularBuffer" version="1.0.0.0" targetFramework="net452" />
<package id="Dapper" version="1.42" targetFramework="net452" />
<package id="MySql.Data" version="6.9.8" targetFramework="net452" />
<package id="NLog" version="4.3.5" targetFramework="net452" />
<package id="NLog.Config" version="4.3.5" targetFramework="net452" />
<package id="NLog.Schema" version="4.3.4" targetFramework="net452" />
<package id="RabbitMQ.Client" version="4.0.0" targetFramework="net452" requireReinstallation="True" />
</packages>