void OnEventClose(UdpEvent ev) { if (ChangeState(UdpSocketState.Running, UdpSocketState.Shutdown)) { for (int i = 0; i < connList.Count; ++i) { UdpConnection cn = connList[i]; cn.SendCommand(UdpCommandType.Disconnected); cn.ChangeState(UdpConnectionState.Disconnected); } if (platform.Close() == false) { UdpLog.Error("failed to shutdown socket interface, platform code: {0}", platform.PlatformError.ToString()); } connList.Clear(); connLookup.Clear(); eventQueueIn.Clear(); pendingConnections.Clear(); GetReadStream().Data = null; GetWriteStream(0, 0).Data = null; UdpLog.Error("OnEventClose: {0}", ev.EndPoint.ToString()); } }
void NetworkLoop() { while (true) { try { UdpLog.Info("socket created"); while (state == udpSocketState.Created) { ProcessIncommingEvents(true); Thread.Sleep(1); } UdpLog.Info("socket started"); while (state == udpSocketState.Running) { RecvDelayedPackets(); RecvNetworkData(); ProcessTimeouts(); ProcessIncommingEvents(false); frame += 1; } UdpLog.Info("socket closed"); } catch (Exception exn) { UdpLog.Error(exn.ToString()); } } }
internal void SendObject(object o) { serializer.SendNext(o); while (serializer.HasQueuedObjects) { UdpSendFailReason reason = CheckCanSend(false); if (reason != UdpSendFailReason.None) { while (serializer.HasQueuedObjects) { socket.Raise(UdpEvent.PUBLIC_OBJECT_SEND_FAILED, this, serializer.NextObject(), reason); } break; } UdpStream stream = socket.GetWriteStream(mtu << 3, UdpSocket.HeaderBitSize); object obj = serializer.NextObject(); if (serializer.Pack(stream, ref obj)) { if (stream.Overflowing && (socket.Config.AllowPacketOverflow == false)) { UdpLog.Error("stream to {0} is overflowing, not sending", endpoint.ToString()); socket.Raise(UdpEvent.PUBLIC_OBJECT_SEND_FAILED, this, obj, UdpSendFailReason.StreamOverflow); return; } UdpHeader header = MakeHeader(true); header.Pack(stream, socket); UdpHandle handle = MakeHandle(ref header); handle.Object = obj; if (SendStream(stream, handle, alwaysSendMtu)) { // track stats stats.PacketSent((uint)stream.Ptr >> 3); socket.Statistics.PacketSent((uint)stream.Ptr >> 3); // push object to user thread socket.Raise(UdpEvent.PUBLIC_OBJECT_SENT, this, obj); Log.info(this, "SendObject##UdpSocket.HeaderBitSize: " + (UdpSocket.HeaderBitSize >> 3)); Log.info(this, "SendObject##size: " + (stream.Ptr >> 3)); } else { socket.Raise(UdpEvent.PUBLIC_OBJECT_SEND_FAILED, this, obj, UdpSendFailReason.SocketError); } } else { socket.Raise(UdpEvent.PUBLIC_OBJECT_SEND_FAILED, this, obj, UdpSendFailReason.SerializerReturnedFalse); } } }
void OnEventClose(UdpEvent ev) { if (ChangeState(udpSocketState.Running, udpSocketState.Shutdown)) { if (platform.Close() == false) { UdpLog.Error("failed to shutdown socket interface, platform code: {0}", platform.PlatformError.ToString()); } } }
void OnEventConnect(UdpEvent ev) { if (CheckState(udpSocketState.Running)) { UdpConnection cn = CreateConnection(ev.EndPoint, UdpConnectionMode.Client); if (cn == null) { UdpLog.Error("could not create connection for endpoint {0}", ev.EndPoint.ToString()); } } }
internal void SendObject(object o) { serializer.SendNext(o); while (serializer.HasQueuedObjects) { UdpSendFailReason reason = CheckCanSend(false); if (reason != UdpSendFailReason.None) { while (serializer.HasQueuedObjects) { socket.Raise(UdpEvent.PUBLIC_OBJECT_SEND_FAILED, this, serializer.NextObject(), reason); } break; } UdpBitStream stream = new UdpBitStream(socket.GetWriteBuffer(), mtu, UdpHeader.GetSize(socket)); object obj = serializer.NextObject(); if (serializer.Pack(ref stream, ref obj)) { if (stream.Overflowing && (socket.Config.AllowPacketOverflow == false)) { UdpLog.Error("stream to {0} is overflowing, not sending", endpoint.ToString()); socket.Raise(UdpEvent.PUBLIC_OBJECT_SEND_FAILED, this, obj, UdpSendFailReason.StreamOverflow); return; } UdpHeader header = MakeHeader(true); header.Pack(new UdpBitStream(stream.Data, mtu, 0), socket); UdpHandle handle = MakeHandle(ref header); handle.Object = obj; if (SendStream(stream, handle, alwaysSendMtu) == false) { socket.Raise(UdpEvent.PUBLIC_OBJECT_SEND_FAILED, this, obj, UdpSendFailReason.SocketError); } else { stats.PacketSent(); } } else { socket.Raise(UdpEvent.PUBLIC_OBJECT_SEND_FAILED, this, obj, UdpSendFailReason.SerializerReturnedFalse); } } }
void OnEventStart(UdpEvent ev) { if (ChangeState(udpSocketState.Created, udpSocketState.Running)) { if (platform.Bind(ev.EndPoint)) { UdpLog.Info("socket bound to {0}", platform.EndPoint.ToString()); } else { UdpLog.Error("could not bind socket, platform code: {0}, platform error: {1}", platform.PlatformError.ToString(), platform.PlatformErrorString); } } }
void OnEventStart(UdpEvent ev) { UdpLog.Info("binding socket using platform '{0}'", platform.GetType()); if (ChangeState(UdpSocketState.Created, UdpSocketState.Running)) { if (platform.Bind(ev.EndPoint)) { // send started event Raise(UdpEvent.PUBLIC_STARTED, platform.EndPoint); // log that we started UdpLog.Info("socket bound to {0}", platform.EndPoint.ToString()); } else { // send started failed event Raise(UdpEvent.PUBLIC_START_FAILED); // log error UdpLog.Error("could not bind socket, platform code: {0}, platform error: {1}", platform.PlatformError.ToString(), platform.PlatformErrorString); } } }