public OutboundQueue(Node node, IPEndPoint remoteEndpoint, Socket socket) { this.remoteEndpoint = remoteEndpoint; this.socket = socket; this.node = node; msgFactory = node.MessageFactory; }
public SmsServerSession(SmsServer server, NetworkMessageFactory factory) { this.Server = server; this.CancellationSource = new CancellationTokenSource(); this.MessageQueue = new ConcurrentQueue <NetworkMessage>(); this.MessageQueueEvent = new AutoResetEvent(false); this.QueueWindowSize = 128; this.MessagesFactory = factory; }
public async Task <bool> StartAsync(NetworkMessageFactory factory = null) { lock (this) if (this.IsRunning) { return(true); } if (factory == null && this.MessagesFactory == null) { this.MessagesFactory = NullNetworkMessageFactory.Default; } bool isStarting = false; lock (this) { if (this.IsRunning) { return(true); } this.Status = SmsClientStatus.Disconnected; isStarting = true; this.CancellationSource = new CancellationTokenSource(); this.SendingTask = this.RunSendLoop(this.CancellationSource.Token); this.ReceivingTask = this.RunReceiveLoop(this.CancellationSource.Token); } if (isStarting) { await this.DoStartAsync(); } var start = DateTime.Now; var timeout = TimeSpan.FromSeconds(10); while (this.Status != SmsClientStatus.Connected) { await Task.Delay(50); if ((DateTime.Now - start) > timeout) { break; } } return(this.Status == SmsClientStatus.Connected); }
// Start is called before the first frame update async void Start() { RealmState initialState = await NetworkConnection.GetStateAsync(); RealmStateManager.UpdateState(initialState); MapManager.InitMap(initialState.map); await NetworkConnection.Connect(); while (NetworkConnection.IsOpen()) { string message = await NetworkConnection.Receive(); if (!string.IsNullOrEmpty(message)) { try { NetworkMessage <RealmEventBase> networkMessage = NetworkMessageFactory.GetNetworkMessage(message); if (networkMessage != null && networkMessage.realmEvent != null && !string.IsNullOrEmpty(networkMessage.realmEventType)) { // Re-parse with the appropriate event type. This is ugly. Type eventType = RealmEventRegistry.GetEventDataType(networkMessage.realmEventType); Type networkMessageType = typeof(NetworkMessage <>).MakeGenericType(eventType); object genericMessage = NetworkMessageFactory.GetNetworkMessage(message, networkMessageType); object realmEvent = networkMessageType.GetField("realmEvent").GetValue(genericMessage); RealmStateManager.UpdateState(networkMessage.realmStateFragment); // TODO: Fire off this event and continue. Don't wait for a result. EventManager.TriggerEvent(networkMessage.realmEventType, realmEvent as RealmEventBase); //RealmStateManager.UpdateState(networkMessage.realmStateFragment); } } catch (Exception e) { Debug.Log("Exception in NetworkHandler: " + e.Message); Debug.Log(e.StackTrace); } } } }
internal void Init(Node node, Socket socket, IPAddress address, int port, string nickname) { this.node = node; msgFactory = node.MessageFactory; Nickname = nickname; this.endpoint = new IPEndPoint(address, port); outboundQueue = new OutboundQueue(node, this.endpoint, socket); ackMessage = msgFactory.Get<AckMessage>(); previousActivityTimeTicks = Environment.TickCount; }