void Update() { if (reliableEndpoint != null) { // Update internal buffers reliableEndpoint.Update(); } if (readQueue.Count > 0) { QueuedBuffer queuedBuffer = readQueue.Dequeue(); if (OnMessageReceived != null) { OnMessageReceived( NetworkMessage.ParseMessage(queuedBuffer.buffer, queuedBuffer.size), queuedBuffer.buffer, queuedBuffer.size ); } else { Debug.LogError("[NetworkClient.cs] OnMessageReceived is null. Ignoring received message"); } } if (hasDisconnected) { onDisconnect(); onDisconnect = null; hasDisconnected = false; } }
// Init function, called exactly once public void Init() { // Set up NetcodeIO.NET client object and callbacks client = new Client(); client.OnStateChanged += ClientStateChanged; client.OnMessageReceived += MessageReceivedHandler; for (int i = 0; i < bufferQueue.Length; i++) { bufferQueue[i] = new QueuedBuffer(); } hasInit = true; }
/// <summary> /// Remove data that has been sent from the queue. Returns true if we should send more. /// Make sure slimLock is entered in read mode before calling this, and that disposing is not 1. /// Ensure an error handler is implemented which forwards to ProcessError. /// </summary> private bool CompleteSend(SocketAsyncEventArgs args) { // did it fail? if (args.SocketError != SocketError.Success) { _sendLock.Release(); BufferCache.Set(args.Buffer); // it failed, let's invoke the error handler if (!_disposing) { ProcessError("Socket error '" + args.SocketError + "'."); } args.Dispose(); return(false); } BufferCache.Set(args.Buffer); args.Dispose(); // is there a callback? yes, run it if (_onSent != null) { _onSent.Run(); } // more data to send? yes, return positive if (_sendBuffer.Length > 0) { return(true); } // more buffers in queue? if (_sendQueue.TakeItem().Count > 0) { // yes, recycle send buffer if appropriate if (_enqueueBuffer == null) { _enqueueBuffer = _sendBuffer; } // get the next buffer to send QueuedBuffer queuedBuffer = _sendQueue.Item.RemoveReturn(0); _sendQueue.Release(); // set the send buffer SetSendBuffer(queuedBuffer.Buffer, queuedBuffer.EndPoint); return(true); } _sendQueue.Release(); _sendLock.Release(); // more data to send? yes, take the lock and return positive if (_sendBuffer.Length > 0 && _sendLock.TryTake) { return(true); } // more buffers in the queue? yes, take the lock and return positive if (_sendQueue.TakeItem().Count > 0 && _sendLock.TryTake) { // yes, recycle send buffer if appropriate if (_enqueueBuffer == null) { _enqueueBuffer = _sendBuffer; } // get the next buffer to send QueuedBuffer queuedBuffer = _sendQueue.Item.RemoveReturn(0); _sendQueue.Release(); // set the send buffer SetSendBuffer(queuedBuffer.Buffer, queuedBuffer.EndPoint); return(true); } _sendQueue.Release(); return(false); }