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

@ -16,6 +16,9 @@ namespace FFXIVClassic_World_Server.DataObjects
public readonly int[] ownedZoneIds;
public bool isConnected = false;
public Socket zoneServerConnection;
private ClientConnection conn;
private byte[] buffer = new byte[0xFFFF];
public ZoneServer(string ip, int port)
{
@ -34,6 +37,18 @@ namespace FFXIVClassic_World_Server.DataObjects
{
zoneServerConnection.Connect(remoteEP);
isConnected = true;
conn = new ClientConnection();
conn.socket = zoneServerConnection;
conn.buffer = new byte[0xFFFF];
try
{
zoneServerConnection.BeginReceive(conn.buffer, 0, conn.buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), conn);
}
catch (Exception e)
{
throw new ApplicationException("Error occured starting listeners, check inner exception", e);
}
}
catch (Exception e)
{ Program.Log.Error("Failed to connect"); return; }
@ -54,5 +69,71 @@ namespace FFXIVClassic_World_Server.DataObjects
}
}
private void ReceiveCallback(IAsyncResult result)
{
ClientConnection conn = (ClientConnection)result.AsyncState;
//Check if disconnected
if ((conn.socket.Poll(1, SelectMode.SelectRead) && conn.socket.Available == 0))
{
conn = null;
isConnected = false;
Program.Log.Info("Zone server @ {0}:{1} disconnected!", zoneServerIp, zoneServerPort);
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)
{
SubPacket subpacket = SubPacket.CreatePacket(ref offset, conn.buffer, bytesRead);
//If can't build packet, break, else process another
if (subpacket == null)
break;
else
Server.GetServer().OnReceiveSubPacketFromZone(this, subpacket);
}
//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
{
conn = null;
isConnected = false;
Program.Log.Info("Zone server @ {0}:{1} disconnected!", zoneServerIp, zoneServerPort);
}
}
catch (SocketException)
{
conn = null;
isConnected = false;
Program.Log.Info("Zone server @ {0}:{1} disconnected!", zoneServerIp, zoneServerPort);
}
}
}
}