More work on the world server. Modified map server to communicate with it.

This commit is contained in:
Filip Maj 2016-08-29 08:17:14 -04:00
parent bd26a71fef
commit 6bffe69b21
16 changed files with 507 additions and 577 deletions

View file

@ -26,7 +26,7 @@ namespace FFXIVClassic_Map_Server
private Socket mServerSocket;
private Dictionary<uint, ConnectedPlayer> mConnectedPlayerList = new Dictionary<uint, ConnectedPlayer>();
private List<ClientConnection> mConnectionList = new List<ClientConnection>();
private ZoneConnection mWorldConnection = new ZoneConnection();
private LuaEngine mLuaEngine = new LuaEngine();
private static WorldManager mWorldManager;
@ -119,7 +119,7 @@ namespace FFXIVClassic_Map_Server
Program.Log.Info("Map Server has started @ {0}:{1}", (mServerSocket.LocalEndPoint as IPEndPoint).Address, (mServerSocket.LocalEndPoint as IPEndPoint).Port);
Console.ForegroundColor = ConsoleColor.Gray;
mProcessor = new PacketProcessor(this, mConnectedPlayerList, mConnectionList);
mProcessor = new PacketProcessor(this, mConnectedPlayerList);
//mGameThread = new Thread(new ThreadStart(mProcessor.update));
//mGameThread.Start();
@ -138,21 +138,18 @@ namespace FFXIVClassic_Map_Server
#region Socket Handling
private void AcceptCallback(IAsyncResult result)
{
ClientConnection conn = null;
ZoneConnection conn = null;
Socket socket = (System.Net.Sockets.Socket)result.AsyncState;
try
{
conn = new ClientConnection();
conn = new ZoneConnection();
conn.socket = socket.EndAccept(result);
conn.buffer = new byte[BUFFER_SIZE];
lock (mConnectionList)
{
mConnectionList.Add(conn);
}
mWorldConnection = 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);
@ -163,11 +160,7 @@ namespace FFXIVClassic_Map_Server
{
if (conn != null)
{
lock (mConnectionList)
{
mConnectionList.Remove(conn);
}
mWorldConnection = null;
}
mServerSocket.BeginAccept(new AsyncCallback(AcceptCallback), mServerSocket);
}
@ -175,10 +168,7 @@ namespace FFXIVClassic_Map_Server
{
if (conn != null)
{
lock (mConnectionList)
{
mConnectionList.Remove(conn);
}
mWorldConnection = null;
}
mServerSocket.BeginAccept(new AsyncCallback(AcceptCallback), mServerSocket);
}
@ -208,20 +198,13 @@ namespace FFXIVClassic_Map_Server
/// <param name="result"></param>
private void ReceiveCallback(IAsyncResult result)
{
ClientConnection conn = (ClientConnection)result.AsyncState;
ZoneConnection conn = (ZoneConnection)result.AsyncState;
//Check if disconnected
if ((conn.socket.Poll(1, SelectMode.SelectRead) && conn.socket.Available == 0))
{
if (mConnectedPlayerList.ContainsKey(conn.owner))
mConnectedPlayerList.Remove(conn.owner);
lock (mConnectionList)
{
mConnectionList.Remove(conn);
}
if (conn.connType == BasePacket.TYPE_ZONE)
Program.Log.Info("{0} has disconnected.", conn.owner == 0 ? conn.GetAddress() : "User " + conn.owner);
return;
mWorldConnection = null;
Program.Log.Info("Disconnected from world server!");
}
try
@ -237,13 +220,13 @@ namespace FFXIVClassic_Map_Server
//Build packets until can no longer or out of data
while (true)
{
BasePacket basePacket = BuildPacket(ref offset, conn.buffer, bytesRead);
SubPacket subPacket = SubPacket.CreatePacket(ref offset, conn.buffer, bytesRead);
//If can't build packet, break, else process another
if (basePacket == null)
if (subPacket == null)
break;
else
mProcessor.ProcessPacket(conn, basePacket);
mProcessor.ProcessPacket(conn, subPacket);
}
//Not all bytes consumed, transfer leftover to beginning
@ -264,63 +247,20 @@ namespace FFXIVClassic_Map_Server
}
else
{
Program.Log.Info("{0} has disconnected.", conn.owner == 0 ? conn.GetAddress() : "User " + conn.owner);
lock (mConnectionList)
{
mConnectionList.Remove(conn);
}
mWorldConnection = null;
Program.Log.Info("Disconnected from world server!");
}
}
catch (SocketException)
{
if (conn.socket != null)
{
Program.Log.Info("{0} has disconnected.", conn.owner == 0 ? conn.GetAddress() : "User " + conn.owner);
lock (mConnectionList)
{
mConnectionList.Remove(conn);
}
mWorldConnection = null;
Program.Log.Info("Disconnected from world server!");
}
}
}
/// <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