void Start() { Instance = this; inputFieldObj = GameObject.Find("CommandInputField"); CommandInputField = inputFieldObj.GetComponent <InputField>(); CommandInputField.Select(); CommandInputField.ActivateInputField(); CommandDelegates = new Dictionary <string, CommandDelegate>(); commandManager = new CommandManager(); SocketCommands = new SocketCommands(transform.GetComponent <ServerObj>()); CommandBuffer = new LinkedList <string>(); CommandBufferSearchIndex = null; }
private static void AcceptServiceCallback(IAsyncResult ar) { Socket socket; try { socket = ServiceSocket.EndAccept(ar); } catch (ObjectDisposedException) { return; } var clientSocket = SocketCommands.Read(socket); clientSocket.Message = _chatHistoryFixedSizedQueue.ToList(); clientSocket.Guid = Guid.NewGuid(); SocketCommands.Write(socket, clientSocket); ServiceSocket.BeginAccept(AcceptServiceCallback, null); }
private static void MessageReadCallback() { while (true) { if (_client.Guid != Guid.Empty && MessageSocket.Connected) { try { var socketModel = SocketCommands.Read(MessageSocket); foreach (var message in socketModel.Message) { Console.WriteLine(socketModel.Name + ":" + message); } } catch (SocketException) { Exit(); } } } }
private static void MessageWriteCallback() { while (true) { var socketMessage = new SocketClient { Name = _client.Name, Guid = _client.Guid, Message = new List <string>() }; socketMessage.Message.Add( MessageList.ElementAt(new Random().Next(0, MessageList.Count - 1))); try { SocketCommands.Write(MessageSocket, socketMessage); } catch (SocketException) { Exit(); } Thread.Sleep(BetweenMessagePauseTime); } }
private static void ReceiveCallback(IAsyncResult ar) { SocketServer current = (SocketServer)ar.AsyncState; int received; try { received = current.Socket.EndReceive(ar); } catch (SocketException) { Console.WriteLine("Client forcefully disconnected"); // Don't shutdown because the socket may be disposed and its disconnected anyway. current.Socket.Close(); var index = ClientSockets.FindIndex(x => x.Socket == current.Socket); ClientSockets.RemoveAt(index); return; } var data = new byte[received]; Array.Copy(current.Buffer, data, received); var socketModel = (SocketClient)SocketCommands.ByteArrayToObject(data); foreach (var message in socketModel.Message) { _chatHistoryFixedSizedQueue.Enqueue(socketModel.Name + ": " + message); } foreach (var client in ClientSockets) { if (client.Socket != current.Socket) { var message = SocketCommands.ObjectToByteArray(socketModel); client.Socket.Send(message); } } current.Socket.BeginReceive(current.Buffer, 0, BufferSize, SocketFlags.None, ReceiveCallback, current); }
private static void ConnectServiceCallback(IAsyncResult ar) { try { ServiceSocket.EndConnect(ar); MessageSocket.BeginConnect(IPAddress.Loopback, MessagePort, ConnectMessageCallback, null); } catch (SocketException) { Console.WriteLine("Connection was not established!"); } Console.WriteLine("ServiceSocket Connected"); SocketCommands.Write(ServiceSocket, _client); while (_client.Guid == Guid.Empty) { var socketModel = SocketCommands.Read(ServiceSocket); _client.Guid = socketModel.Guid; foreach (var message in socketModel.Message) { Console.WriteLine(message); } } }
public SocketCommand(SocketCommands cmd, SocketCommandData met) { Command = cmd; Metadata = met; }
public SocketCommand(SocketCommands cmd) { Command = cmd; }
public SocketCommand(SocketCommands cmd, object[] data) { _command = cmd; _extraData = data; }
private static object SendCommand(SocketCommands cmd, ulong OriginClientId = 0) { return(new SocketMessage(OriginClientId, new SocketCommand(cmd))); }
// ??? Use this Socket xx, ClientSocket xx in parameters //Not neccesary to get byte[], ... also, I will move this to new class called SocketHandler, Deserialize and Serialize not needed anymore //Reimplement here serialize / deserialize functions? //0 means that the message is not for any client, is a broadcast message sended to the server, so, we have to handle errors when we don't manage it correctly. public static object SendCommand(SocketCommands cmd, SocketCommandData data, ulong OriginClientId = 0) { return(new SocketMessage(OriginClientId, new SocketCommand(cmd, data))); }
public SocketCommand(SocketCommands cmd) { _command = cmd; }