Created
October 5, 2017 07:19
-
-
Save dimmduh/cc22a80e845215454371c615873ad2f3 to your computer and use it in GitHub Desktop.
Unity LLApi MessagesConsumer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void Start() | |
{ | |
Disconnect = false; | |
//@todo tune params! | |
var gConfig = new GlobalConfig | |
{ | |
ThreadPoolSize = threadPoolSize, | |
MaxPacketSize = maxPacketSize | |
}; | |
NetworkTransport.Init(gConfig); | |
SocketId = NetworkTransport.AddHost(Topology, Port); | |
ProcessWorkers = new Thread[workerProcessCount]; | |
// Create and start a separate thread for each worker | |
for (int i = 0; i < workerProcessCount; i++) | |
{ | |
ProcessWorkers[i] = new Thread(MessagesConsumer); | |
ProcessWorkers[i].Name = "LLApi Process Thread " + i; | |
ProcessWorkers[i].Start(); | |
} | |
isConnected = true; | |
} | |
private void MessagesConsumer() | |
{ | |
try | |
{ | |
while (true) | |
{ | |
InputMessage message; | |
lock (lockPocessObject) | |
{ | |
while (InputMessagesQueue.Count == 0) | |
{ | |
Monitor.Wait(lockPocessObject); | |
} | |
message = InputMessagesQueue.Dequeue(); | |
} | |
//@todo is it for stopping trheads? | |
if (message == null) return; | |
lock (lockHandlers) | |
{ | |
SubjectDelegate handlerDelegate; | |
if(SubjectHandlers.TryGetValue(message.MsgSubject, out handlerDelegate)) | |
{ | |
handlerDelegate.Invoke(message); | |
} | |
} | |
} | |
} | |
catch (Exception e) | |
{ | |
Debug.LogException(e); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment