2016-08-22 10:43:04 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2016-08-22 14:18:37 -04:00
|
|
|
|
using System.Net;
|
2016-08-22 10:43:04 -04:00
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-08-23 16:57:24 -04:00
|
|
|
|
using FFXIVClassic.Common;
|
2016-09-26 16:20:01 -04:00
|
|
|
|
using FFXIVClassic_World_Server.Packets.WorldPackets.Send;
|
2016-08-22 10:43:04 -04:00
|
|
|
|
|
|
|
|
|
namespace FFXIVClassic_World_Server.DataObjects
|
|
|
|
|
{
|
|
|
|
|
class ZoneServer
|
|
|
|
|
{
|
2016-08-22 14:18:37 -04:00
|
|
|
|
public readonly string zoneServerIp;
|
|
|
|
|
public readonly int zoneServerPort;
|
2016-12-03 12:19:59 -05:00
|
|
|
|
public readonly List<uint> ownedZoneIds;
|
2016-08-22 14:18:37 -04:00
|
|
|
|
public bool isConnected = false;
|
2016-08-22 10:43:04 -04:00
|
|
|
|
public Socket zoneServerConnection;
|
2016-12-03 12:19:59 -05:00
|
|
|
|
private ClientConnection conn;
|
2016-08-29 08:17:14 -04:00
|
|
|
|
|
|
|
|
|
private byte[] buffer = new byte[0xFFFF];
|
2016-08-22 14:18:37 -04:00
|
|
|
|
|
2016-12-03 12:19:59 -05:00
|
|
|
|
public ZoneServer(string ip, int port, uint firstId)
|
2016-08-22 14:18:37 -04:00
|
|
|
|
{
|
|
|
|
|
zoneServerIp = ip;
|
|
|
|
|
zoneServerPort = port;
|
2016-12-03 12:19:59 -05:00
|
|
|
|
|
|
|
|
|
ownedZoneIds = new List<uint>();
|
|
|
|
|
ownedZoneIds.Add(firstId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddLoadedZone(uint id)
|
|
|
|
|
{
|
|
|
|
|
ownedZoneIds.Add(id);
|
2016-08-22 14:18:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-03 14:00:24 -05:00
|
|
|
|
public bool Connect()
|
2016-08-22 14:18:37 -04:00
|
|
|
|
{
|
|
|
|
|
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;
|
2016-08-29 08:17:14 -04:00
|
|
|
|
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);
|
|
|
|
|
}
|
2016-08-22 14:18:37 -04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
2016-12-03 14:00:24 -05:00
|
|
|
|
{ Program.Log.Error("Failed to connect"); return false; }
|
|
|
|
|
|
|
|
|
|
return true;
|
2016-08-22 14:18:37 -04:00
|
|
|
|
}
|
2016-08-23 16:57:24 -04:00
|
|
|
|
|
|
|
|
|
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); }
|
2017-01-08 21:42:43 -05:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (Connect())
|
|
|
|
|
SendPacket(subpacket);
|
|
|
|
|
}
|
2016-08-23 16:57:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-29 08:17:14 -04:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-03 12:19:59 -05:00
|
|
|
|
public void SendSessionStart(Session session)
|
|
|
|
|
{
|
|
|
|
|
SendPacket(SessionBeginPacket.BuildPacket(session));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SendSessionEnd(Session session)
|
2016-09-26 16:20:01 -04:00
|
|
|
|
{
|
|
|
|
|
SendPacket(SessionEndPacket.BuildPacket(session));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SendSessionEnd(Session session, uint destinationZoneId, string destinationPrivateArea, byte spawnType, float spawnX, float spawnY, float spawnZ, float spawnRotation)
|
|
|
|
|
{
|
2016-12-03 12:19:59 -05:00
|
|
|
|
SendPacket(SessionEndPacket.BuildPacket(session, destinationZoneId, destinationPrivateArea, spawnType, spawnX, spawnY, spawnZ, spawnRotation));
|
2016-09-26 16:20:01 -04:00
|
|
|
|
}
|
2016-08-29 08:17:14 -04:00
|
|
|
|
|
2016-08-22 10:43:04 -04:00
|
|
|
|
}
|
|
|
|
|
}
|