void OnEventConnectCancel(UdpEvent ev) { if (CheckState(UdpSocketState.Running)) { UdpConnection cn; if (connLookup.TryGetValue(ev.EndPoint, out cn)) { // if we are connecting, destroy connection if (cn.CheckState(UdpConnectionState.Connecting)) { // notify user thread Raise(UdpEvent.PUBLIC_CONNECT_FAILED, ev.EndPoint); // destroy this connection cn.ChangeState(UdpConnectionState.Destroy); } // if we are connected, disconnect else if (ev.Connection.CheckState(UdpConnectionState.Connected)) { ev.Connection.SendCommand(UdpCommandType.Disconnected); ev.Connection.ChangeState(UdpConnectionState.Disconnected); } } } }
void OnEventAccept(UdpEvent ev) { if (pendingConnections.Remove(ev.EndPoint)) { AcceptConnection(ev.EndPoint); } }
void OnEventRefuse(UdpEvent ev) { if (pendingConnections.Remove(ev.EndPoint)) { SendRefusedCommand(ev.EndPoint); } }
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()); } }
internal void Raise(int eventType) { UdpEvent ev = new UdpEvent(); ev.Type = eventType; Raise(ev); }
public bool Poll(out UdpEvent ev, out UdpSocket socket) { bool allowRestart = (index != 0); RESTART: for (int i = index; i < sockets.Length; ++i) { if (sockets[i].Poll(out ev)) { index = i + 1; socket = sockets[i]; return(true); } } if (allowRestart) { index = 0; allowRestart = false; goto RESTART; } ev = default(UdpEvent); socket = null; return(false); }
internal void Raise(int eventType, UdpEndPoint endpoint) { UdpEvent ev = new UdpEvent(); ev.Type = eventType; ev.EndPoint = endpoint; Raise(ev); }
void OnEventDisconect(UdpEvent ev) { if (ev.Connection.CheckState(UdpConnectionState.Connected)) { ev.Connection.SendCommand(UdpCommandType.Disconnected); ev.Connection.ChangeState(UdpConnectionState.Disconnected); } }
internal void Raise(int eventType, UdpConnection connection) { UdpEvent ev = new UdpEvent(); ev.Type = eventType; ev.Connection = connection; Raise(ev); }
internal void Raise(int eventType, int intval) { UdpEvent ev = new UdpEvent(); ev.Type = eventType; ev.OptionIntValue = intval; Raise(ev); }
internal void Raise(int eventType, UdpConnection connection, object obj) { UdpEvent ev = new UdpEvent(); ev.Type = eventType; ev.Connection = connection; ev.Object = obj; Raise(ev); }
internal void OnEventConnectionOption(UdpEvent ev) { switch (ev.Option) { case UdpConnectionOption.AlwaysSendMtu: alwaysSendMtu = ev.OptionIntValue == 1; break; } }
internal void Raise(int eventType, UdpEndPoint endpoint, object obj) { UdpEvent ev = new UdpEvent(); ev.Type = eventType; ev.EndPoint = endpoint; ev.Object = obj; Raise(ev); }
internal void Raise(int eventType, UdpConnection connection, UdpConnectionOption option, int value) { UdpEvent ev = new UdpEvent(); ev.Type = eventType; ev.Connection = connection; ev.Option = option; ev.OptionIntValue = value; Raise(ev); }
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()); } } }
internal void Raise(int eventType, UdpConnection connection, object obj, UdpSendFailReason reason) { UdpEvent ev = new UdpEvent(); ev.Type = eventType; ev.Connection = connection; ev.FailedReason = reason; ev.Object = obj; Raise(ev); }
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()); } } }
/// <summary> /// Poll socket for any events /// </summary> /// <param name="ev">The current event on this socket</param> /// <returns>True if a new event is available, False otherwise</returns> public bool Poll(ref UdpEvent ev) { lock (eventQueueOut) { if (eventQueueOut.Count > 0) { ev = eventQueueOut.Dequeue(); return(true); } } return(false); }
void ProcessIncommingEvents(bool returnOnStart) { while (true) { UdpEvent ev = default(UdpEvent); lock (eventQueueIn) { if (eventQueueIn.Count > 0) { ev = eventQueueIn.Dequeue(); } } if (ev.Type == 0) { return; } switch (ev.Type) { case UdpEvent.INTERNAL_START: OnEventStart(ev); if (returnOnStart) { return; } else { break; } case UdpEvent.INTERNAL_CONNECT: OnEventConnect(ev); break; case UdpEvent.INTERNAL_CONNECT_CANCEL: OnEventConnectCancel(ev); break; case UdpEvent.INTERNAL_ACCEPT: OnEventAccept(ev); break; case UdpEvent.INTERNAL_REFUSE: OnEventRefuse(ev); break; case UdpEvent.INTERNAL_DISCONNECT: OnEventDisconect(ev); break; case UdpEvent.INTERNAL_CLOSE: OnEventClose(ev); break; case UdpEvent.INTERNAL_SEND: OnEventSend(ev); break; case UdpEvent.INTERNAL_CONNECTION_OPTION: OnEventConnectionOption(ev); break; case UdpEvent.INTERNAL_SLEEP: OnEventSleep(ev); break; } } }
/// <summary> /// Peek the next event from the socket /// </summary> /// <param name="ev">The next event on this socket</param> /// <returns>True if an event is available, False otherwise</returns> public bool Peek(out UdpEvent ev) { lock (eventQueueOut) { if (eventQueueOut.Count > 0) { ev = eventQueueOut.Peek(); return(true); } } ev = default(UdpEvent); return(false); }
internal void OnEventConnectionOption(UdpEvent ev) { switch (ev.Option) { case UdpConnectionOption.AlwaysSendMtu: alwaysSendMtu = ev.OptionIntValue == 1; break; case UdpConnectionOption.MtuSize: mtu = UdpMath.Clamp(ev.OptionIntValue, socket.Config.MtuMin, socket.Config.MtuMax); break; } }
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 Raise(UdpEvent ev) { if (ev.IsInternal) { lock (eventQueueIn) { eventQueueIn.Enqueue(ev); } } else { lock (eventQueueOut) { eventQueueOut.Enqueue(ev); } } }
void OnEventConnectCancel(UdpEvent ev) { if (CheckState(udpSocketState.Running)) { UdpConnection cn; if (connLookup.TryGetValue(ev.EndPoint, out cn)) { if (cn.CheckState(UdpConnectionState.Connecting)) { // notify user thread Raise(UdpEvent.PUBLIC_CONNECT_FAILED, ev.EndPoint); // destroy this connection cn.ChangeState(UdpConnectionState.Destroy); } } } }
public bool Poll(ref UdpEvent ev) { bool allowRestart = true; RESTART: for (int i = index; i < sockets.Length; ++i) { if (sockets[i].Poll(ref ev)) { index = i + 1; return true; } } if (allowRestart) { index = 0; allowRestart = false; goto RESTART; } return false; }
void Raise(UdpEvent ev) { if (ev.IsInternal) { lock (eventQueueIn) { eventQueueIn.Enqueue(ev); } } else { lock (eventQueueOut) { eventQueueOut.Enqueue(ev); } if (Config.UseAvailableEventEvent) { availableEvent.Set(); } } }
public bool Poll(ref UdpEvent ev) { bool allowRestart = true; RESTART: for (int i = index; i < sockets.Length; ++i) { if (sockets[i].Poll(ref ev)) { index = i + 1; return(true); } } if (allowRestart) { index = 0; allowRestart = false; goto RESTART; } return(false); }
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); } } }
void OnEventConnectionOption(UdpEvent ev) { ev.Connection.OnEventConnectionOption(ev); }
void OnEventSleep(UdpEvent ev) { UdpLog.Debug("sleeping network thread for {0} ms", ev.OptionIntValue); Thread.Sleep(ev.OptionIntValue); }
void OnEventSend(UdpEvent ev) { ev.Connection.SendObject(ev.Object); }