Ejemplo n.º 1
0
// NewGameOverMessageFromBytes returns an instance of GameOverMessage based
// on a slice of bytes
func NewGameOverMessageFromBytes(messageBytes []byte) GameOverMessage {
	message := GameOverMessage{}
	buff := bytes.NewBuffer(messageBytes)

	typeByte, err := buff.ReadByte()
	if err != nil {
		serverlog.Fatal("GameOver ", err)
	}
	message.MessageType = uint8(typeByte)

	yscByte, err := buff.ReadByte()
	if err != nil {
		serverlog.Fatal("GameOver ", err)
	}
	message.Yscore = uint8(yscByte)

	oscByte, err := buff.ReadByte()
	if err != nil {
		serverlog.Fatal("GameOver ", err)
	}
	message.Oscore = uint8(oscByte)

	statByte, err := buff.ReadByte()
	if err != nil {
		serverlog.Fatal("GameOver ", err)
	}
	message.Status = uint8(statByte)

	return message
}
func newFinishedMessage(message []byte) finishedMessage {
	fmessage := finishedMessage{}
	buf := bytes.NewBuffer(message)
	b, err := buf.ReadByte()
	if err != nil {
		serverlog.Fatal("Decoding newFinishedMessage err:", err)
	}
	fmessage.mtype = uint8(b)

	b, err = buf.ReadByte()
	if err != nil {
		serverlog.Fatal("Decoding newFinishedMessage err:", err)
	}
	fmessage.p1won = uint8(b) == 255

	b, err = buf.ReadByte()
	if err != nil {
		serverlog.Fatal("Decoding newFinishedMessage err:", err)
	}
	fmessage.rounds = uint8(b)

	b, err = buf.ReadByte()
	if err != nil {
		serverlog.Fatal("Decoding newFinishedMessage err:", err)
	}
	fmessage.p1score = uint8(b)

	b, err = buf.ReadByte()
	if err != nil {
		serverlog.Fatal("Decoding newFinishedMessage err:", err)
	}
	fmessage.p2score = uint8(b)

	return fmessage
}
Ejemplo n.º 3
0
func (g *Game) createSocket() {
	serverlog.General("Creating socket:", g.UDSPath, "for", g.Identification())
	_, err := os.Create(g.UDSPath)
	if err != nil {
		serverlog.Fatal("Failed to create socket:", g.UDSPath, "for", g.Identification(), "err:", err)
	}
}
Ejemplo n.º 4
0
// startReader starts the connection reader
func (conn *Conn) startReader() {
	serverlog.General("Reader started for", conn.Identification())
	var messageBuffer bytes.Buffer
	var bytesToRead int

	for {
		buf := make([]byte, 1400)
		dataSize, err := conn.Socket.Read(buf)
		if err != nil {
			serverlog.General("TCP connection closed for", conn.Identification())
			serverlog.General("Reader closing net.conn socket for", conn.Identification())
			conn.Socket.Close()
			serverlog.General("Reader closing OutgoingMessages channel for", conn.Identification())
			close(conn.IncommingMessages)
			serverlog.General("Reader closing IncomingMessages channel for", conn.Identification())
			close(conn.outgoingMessages)
			return
		}

		data := buf[0:dataSize]
		messageBuffer.Write(data)

		for messageBuffer.Len() > 1 {
			if bytesToRead == 0 {
				btrBuffer := make([]byte, 2)
				_, err := messageBuffer.Read(btrBuffer)
				if err != nil {
					serverlog.Fatal("Error happened in reader bts:2 for", conn.Identification(), "Error:", err)
					serverlog.Fatal(err)
				}
				bytesToRead = int(binary.LittleEndian.Uint16(btrBuffer))
			}
			if messageBuffer.Len() >= bytesToRead {
				message := make([]byte, bytesToRead)
				_, err := messageBuffer.Read(message)
				if err != nil {
					serverlog.Fatal("Error happened in reader bts:var for", conn.Identification(), "Error:", err)
				}
				conn.IncommingMessages <- message
				bytesToRead = 0
			} else {
				break
			}
		}
	}
}
Ejemplo n.º 5
0
// NewAliasDeniedMessageFromBytes returns an instance of AliasDeniedMessage based
// on a slice of bytes
func NewAliasDeniedMessageFromBytes(messageBytes []byte) AliasDeniedMessage {
	message := AliasDeniedMessage{}
	buff := bytes.NewBuffer(messageBytes)
	typeByte, err := buff.ReadByte()
	if err != nil {
		serverlog.Fatal("AliasDenied ", err)
	}

	message.MessageType = uint8(typeByte)
	message.Reason, err = buff.ReadString(NullTerm)
	if err != nil {
		serverlog.Fatal("AliasDenied ", err)
	}
	message.Reason = strings.TrimSuffix(message.Reason, "\x00")

	return message
}
Ejemplo n.º 6
0
// NewMoveMessageFromBytes returns an instance of MoveMessage based
// on a slice of bytes
func NewMoveMessageFromBytes(messageBytes []byte) MoveMessage {
	message := MoveMessage{}
	buff := bytes.NewBuffer(messageBytes)
	typeByte, err := buff.ReadByte()
	if err != nil {
		serverlog.Fatal("Move ", err)
	}
	message.MessageType = uint8(typeByte)

	posBts := make([]byte, 2)
	_, err = buff.Read(posBts)
	if err != nil {
		serverlog.Fatal("Move ", err)
	}
	message.Position = binary.LittleEndian.Uint16(posBts)
	return message
}
Ejemplo n.º 7
0
// NewCreateGameMessageFromBytes  returns an instance of CreateGameMessage
// based on a slice of bytes
func NewCreateGameMessageFromBytes(messageBytes []byte) CreateGameMessage {
	message := CreateGameMessage{}
	buff := bytes.NewBuffer(messageBytes)
	typeByte, err := buff.ReadByte()
	if err != nil {
		serverlog.Fatal("CreateGame ", err)
	}

	message.MessageType = uint8(typeByte)
	message.GameName, err = buff.ReadString(NullTerm)
	if err != nil {
		serverlog.Fatal("CreateGame ", err)
	}
	message.GameName = strings.TrimSuffix(message.GameName, "\x00")

	return message
}
Ejemplo n.º 8
0
// NewRequestAliasMessageFromBytes returns an instance of RequestAliasMessage
// from a slice of bytes
func NewRequestAliasMessageFromBytes(messageBytes []byte) RequestAliasMessage {
	message := RequestAliasMessage{}
	buff := bytes.NewBuffer(messageBytes)
	typeByte, err := buff.ReadByte()
	if err != nil {
		serverlog.Fatal("RequestAlias ", err)
	}

	message.MessageType = uint8(typeByte)
	message.Alias, err = buff.ReadString(NullTerm)
	if err != nil {
		serverlog.Fatal("RequestAlias", err)
	}
	message.Alias = strings.TrimSuffix(message.Alias, "\x00")

	return message
}
Ejemplo n.º 9
0
// NewStartGameMessageFromBytes returns an instance of StartGameMessage
// from a slice of bytes
func NewStartGameMessageFromBytes(messageBytes []byte) StartGameMessage {
	message := StartGameMessage{}
	buff := bytes.NewBuffer(messageBytes)

	typeByte, err := buff.ReadByte()
	if err != nil {
		serverlog.Fatal("StartGame ", err)
	}
	message.MessageType = uint8(typeByte)

	sideByte, err := buff.ReadByte()
	if err != nil {
		serverlog.Fatal("StartGame ", err)
	}
	message.YourSide = (sideByte == 255)

	yposBytes := make([]byte, 2)
	_, err = buff.Read(yposBytes)
	if err != nil {
		serverlog.Fatal("StartGame ", err)
	}
	message.YourPosition = binary.LittleEndian.Uint16(yposBytes)

	oposBytes := make([]byte, 2)
	_, err = buff.Read(oposBytes)
	if err != nil {
		serverlog.Fatal("StartGame ", err)
	}
	message.OtherPosition = binary.LittleEndian.Uint16(oposBytes)

	ballxBytes := make([]byte, 2)
	_, err = buff.Read(ballxBytes)
	if err != nil {
		serverlog.Fatal("StartGame ", err)
	}
	message.Ballx = binary.LittleEndian.Uint16(ballxBytes)

	ballyBytes := make([]byte, 2)
	_, err = buff.Read(ballyBytes)
	if err != nil {
		serverlog.Fatal("StartGame ", err)
	}
	message.Bally = binary.LittleEndian.Uint16(ballyBytes)

	strs := make([]string, 3, 3)
	for i := range strs {
		strs[i], err = buff.ReadString(NullTerm)
		if err != nil {
			serverlog.Fatal("StartGame ", err)
		}
		strs[i] = strings.TrimSuffix(strs[i], "\x00")
	}
	message.OtherAlias = strs[0]
	message.GameID = strs[1]
	message.GameName = strs[2]
	return message
}
Ejemplo n.º 10
0
func (g *Game) startUDS() {
	serverlog.General("Initiationg gameMessage channel for", g.Identification())
	g.gameMessage = make(chan []byte, 100)
	go g.listenGameMessage()
	g.deleteSocket()
	g.createSocket()
	defer g.deleteSocket()
	defer g.Kill()

	listener, err := net.Listen("unix", g.UDSPath)
	if err != nil {
		serverlog.Fatal("Failed to establish listener for Unix domain socket:", g.UDSPath)
	}

	cmd := exec.Command(path.Join("~", ".pppsrv", "game"), g.UDSPath, "60")
	err = cmd.Start()
	if err != nil {
		serverlog.Fatal("Failed to start game instance at:", path.Join("~", ".pppsrv", "game"), "err:", err)
	}

	g.UDS, err = listener.Accept()
	if err != nil {
		serverlog.Fatal("Failed to accept connection for unix domain socket:", g.UDSPath)
	}
	serverlog.General("Accepted connection on:", g.UDSPath)
	for {
		buffer := make([]byte, 1400)
		mSize, err := g.UDS.Read(buffer)
		if err != nil {
			serverlog.General("Unix domain socket closed for:", g.Identification(), "so closing gameMessage channel")
			close(g.gameMessage)
			return
		}
		g.gameMessage <- buffer[:mSize]
	}
}