public void SendMessage(Message msg) { try { if (!_isDisconnected) { if (msg.Type == MessageType.Build) { BuildMessage bm = (BuildMessage)msg; InfoLog.WriteInfo("Sending build message to player: " + this.Id, EPrefix.Test); InfoLog.WriteInfo("Buildmessage data: " + bm.ToString(), EPrefix.Test); } if (msg.Type == MessageType.BuildUnitMessage) { BuildUnitMessage bum = (BuildUnitMessage)msg; InfoLog.WriteInfo("Sending build unit message to player: " + this.Id, EPrefix.Test); InfoLog.WriteInfo("BuildUnitMessage data: " + bum.ToString(), EPrefix.Test); } msg.Serialize(_writeStream); _writeStream.Flush(); if (msg.Type == MessageType.DoTurn) { _tac.Unset(this.Login); } } } catch (Exception ex) { InfoLog.WriteException(ex); ExecuteOnConnectionLost(); } }
public void InitConnection(string hostname, int port) { try { tcpClient = new TcpClient(); tcpClient.NoDelay = true; InfoLog.WriteInfo("Connecting to " + hostname + " on port " + port + " ...", EPrefix.ClientInformation); tcpClient.Connect(hostname, port); NetUtils.SetKeepAlive(tcpClient); InfoLog.WriteInfo("Connected succesfully", EPrefix.ClientInformation); sender.Stream = tcpClient.GetStream(); sender.Start(); InfoLog.WriteInfo("Sender run succesfully", EPrefix.ClientInformation); receiver.Stream = tcpClient.GetStream(); receiver.Start(); } catch (Exception ex) { InfoLog.WriteException(ex); if (ex is SocketException) { throw new Exception("Connection exception", ex); } } }
public override void ProcessItem(Message msg) { try { InfoLog.WriteInfo("Sending message : " + msg.Type, EPrefix.ClientInformation); if (msg.Type == MessageType.Build) { InfoLog.WriteInfo("Sending build message: " + ((BuildMessage)msg).ToString()); } if (msg.Type == MessageType.BuildUnitMessage) { InfoLog.WriteInfo("Sending build unit message: " + ((BuildUnitMessage)msg).ToString()); } msg.Serialize(writeStream); writeStream.Flush(); if (MessageSend != null) { lock (MessageSend) { MessageSend(this, new MessageEventArgs((byte)msg.Type, msg)); } } } catch (Exception ex) { InfoLog.WriteException(ex); if (ConnectionLost != null) { lock (ConnectionLost) { ConnectionLost(this, EventArgs.Empty); } } } }
private void onPauseResume(bool isPause) { if (isPause) { try { OnMenuOptionChange(new MenuOptionArg(MenuOption.Pause, this, true)); } catch (NotImplementedException niex) { InfoLog.WriteException(niex); } } else { try { OnMenuOptionChange(new MenuOptionArg(MenuOption.Continue, this, true)); } catch (NotImplementedException niex) { InfoLog.WriteException(niex); } } }
public void Process() { byte type = (byte)MessageType.Unknown; isProcessing = true; InfoLog.WriteInfo("Receiver run succesfully", EPrefix.ClientInformation); while (true) { if (!isProcessing) { return; } try { type = readStream.ReadByte(); } catch (Exception ex) { InfoLog.WriteException(ex); if (ConnectionLost != null && isProcessing) { lock (ConnectionLost) { ConnectionLost(this, EventArgs.Empty); } } return; } Message msg = MessageFactory.Create((MessageType)type); if (msg == null) { InfoLog.WriteInfo("Received unknown message", EPrefix.MessageReceivedInfo); } else { InfoLog.WriteInfo("Client received msg with type: " + msg.Type, EPrefix.ClientInformation); try { msg.Deserialize(readStream); } catch (Exception ex) { InfoLog.WriteException(ex); if (ConnectionLost != null) { lock (ConnectionLost) { ConnectionLost(this, EventArgs.Empty); } } return; } if (messageHandler != null) { messageHandler.ProcessMessage(msg); } if (MessageReceive != null) { lock (MessageReceive) { MessageReceive(this, new MessageEventArgs((byte)msg.Type, msg)); } } } } }