// Blocking sending of data
// It serializes packet into binary based on selected Data Protocol
public override void Send(DataPacket packet)
{
try {
if (packet != null) {
byte[] data = DataProtocol.Get("json").Write(packet);
byte[] binary = this.transportProtocol.Send(data);
SocketError error = SocketError.Fault;
int send = 0;
bool stop = false;
//lock(this.locker) {
if(this.socket != null && this.socket.Connected) {
send = this.socket.Send(binary, 0, binary.Length, SocketFlags.None, out error);
} else {
stop = true;
}
//}
if(stop || error != SocketError.Success || send != binary.Length) {
Log.Add("client[" + this.id + "] Send, stop");
this.Stop();
}
packet.FinishTracker();
}
} catch(SocketException exception) {
Log.Add("client[" + this.id + "] Send exception: " + exception.ToString());
}
}