Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 187 additions & 0 deletions tpu_raiden/transport/peregrine/src/internal/channel/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
# Copyright 2026 Google LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")

package(default_visibility = ["//visibility:public"])

cc_library(
name = "channel_types",
hdrs = ["channel_types.h"],
)

cc_library(
name = "channel",
hdrs = ["channel.h"],
deps = [
":channel_types",
"//tpu_raiden/transport/peregrine/src/internal/base:types",
"@com_google_absl//absl/types:span",
],
)

cc_library(
name = "channel_test_util",
testonly = True,
srcs = ["channel_test_util.cc"],
hdrs = ["channel_test_util.h"],
deps = [
":channel",
":channel_msg",
":channel_stream",
":channel_util",
":pipe",
"//tpu_raiden/transport/peregrine/src/internal/socket:socket_test_util",
"@com_google_absl//absl/log:check",
],
)

cc_library(
name = "channel_tcp",
srcs = ["channel_tcp.cc"],
hdrs = ["channel_tcp.h"],
deps = [
":channel",
":channel_types",
"//tpu_raiden/transport/peregrine/src/internal/base:types",
"//tpu_raiden/transport/peregrine/src/internal/socket:socket_tcp",
"//tpu_raiden/transport/peregrine/src/internal/util",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:span",
],
)

cc_library(
name = "channel_udp",
srcs = ["channel_udp.cc"],
hdrs = ["channel_udp.h"],
deps = [
":channel",
":channel_types",
"//tpu_raiden/transport/peregrine/src/internal/base:types",
"//tpu_raiden/transport/peregrine/src/internal/socket:socket_udp",
"//tpu_raiden/transport/peregrine/src/internal/util",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:span",
],
)

cc_library(
name = "pipe",
testonly = True,
srcs = ["pipe.cc"],
hdrs = ["pipe.h"],
deps = [
"//tpu_raiden/transport/peregrine/src/internal/util:test_iov",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/synchronization",
],
)

cc_library(
name = "channel_msg",
testonly = True,
srcs = ["channel_msg.cc"],
hdrs = ["channel_msg.h"],
deps = [
":channel",
":channel_types",
":pipe",
"//tpu_raiden/transport/peregrine/src/internal/base:types",
"//tpu_raiden/transport/peregrine/src/internal/util",
"//tpu_raiden/transport/peregrine/src/internal/util:test_iov",
"//tpu_raiden/transport/peregrine/src/util",
"//util/random:shared_bit_gen",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/synchronization",
"@com_google_absl//absl/types:span",
],
)

cc_library(
name = "channel_stream",
testonly = True,
srcs = ["channel_stream.cc"],
hdrs = ["channel_stream.h"],
deps = [
":channel",
":channel_types",
":pipe",
"//tpu_raiden/transport/peregrine/src/internal/base:types",
"//tpu_raiden/transport/peregrine/src/internal/util",
"//tpu_raiden/transport/peregrine/src/internal/util:test_iov",
"//tpu_raiden/transport/peregrine/src/util",
"//util/random:shared_bit_gen",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/synchronization",
"@com_google_absl//absl/types:span",
],
)

cc_test(
name = "channel_test",
srcs = ["channel_test.cc"],
shard_count = 6,
deps = [
":channel",
":channel_test_util",
"//tpu_raiden/transport/peregrine/src/internal/base:types",
"//tpu_raiden/transport/peregrine/src/util",
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/types:span",
"@com_google_googletest//:gtest_main",
],
)

cc_library(
name = "channel_util",
srcs = ["channel_util.cc"],
hdrs = ["channel_util.h"],
deps = [
":channel",
":channel_tcp",
":channel_udp",
"//tpu_raiden/transport/peregrine/src/internal/base:endpoint",
"//tpu_raiden/transport/peregrine/src/internal/socket:connector",
"//tpu_raiden/transport/peregrine/src/internal/socket:socket_tcp",
"//tpu_raiden/transport/peregrine/src/internal/socket:socket_udp",
"@com_google_absl//absl/log:check",
],
)

cc_test(
name = "channel_util_test",
srcs = ["channel_util_test.cc"],
shard_count = 2,
deps = [
":channel_util",
"//tpu_raiden/transport/peregrine/src/internal/base:endpoint",
"//tpu_raiden/transport/peregrine/src/internal/socket:acceptor",
"//tpu_raiden/transport/peregrine/src/internal/socket:socket_tcp",
"//tpu_raiden/transport/peregrine/src/internal/util:test_util",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/strings:str_format",
"@com_google_googletest//:gtest_main",
],
)
81 changes: 81 additions & 0 deletions tpu_raiden/transport/peregrine/src/internal/channel/channel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2026 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef THIRD_PARTY_PEREGRINE_SRC_INTERNAL_CHANNEL_CHANNEL_H_
#define THIRD_PARTY_PEREGRINE_SRC_INTERNAL_CHANNEL_CHANNEL_H_

#include <cstddef>
#include <ostream>
#include <string>

#include "absl/types/span.h"
#include "tpu_raiden/transport/peregrine/src/internal/base/types.h"
#include "tpu_raiden/transport/peregrine/src/internal/channel/channel_types.h"

namespace peregrine::internal {

// An interface that specifies a channel abstraction. It is used for
// communications between two endpoints.
class Channel {
public:
// Destructor.
virtual ~Channel() = default;

// Returns the channel type.
virtual ChannelType Type() const = 0;

// Writes `len` bytes of data from `buf` to the channel.
// Returns the number of bytes actually written if successful. Zero byte means
// no data has been written due to non-error reasons. Returns -1 on error.
virtual ssize_t Write(const Byte* buf, size_t len) = 0;

// Writes a number of buffers described by the `iovecs` to the channel.
// Returns the number of bytes actually written if successful. Zero byte means
// no data has been written due to non-error reasons. Returns -1 on error.
virtual ssize_t WriteV(absl::Span<const IoVec> iovecs) = 0;

// Reads data from the channel into the `buf`.
// For stream channel, it reads exactly `len` bytes of data.
// For message channel, it reads one message of up to `len` bytes.
// Returns the number of bytes actually read if successful.
// For stream channel, returns 0 if the peer side has closed the connection.
// For message channel, returns 0 if the received packet has no payload.
// Returns -1 on error.
virtual ssize_t Read(Byte* buf, size_t len) = 0;

// Reads data from the channel into the `iovecs` buffers.
// For stream channel, it reads exactly `length(iovecs)` bytes of data.
// For message channel, it reads one message of up to `length(iovecs)` bytes.
// Returns the number of bytes actually read if successful.
// For stream channel, returns 0 if the peer side has closed the connection.
// For message channel, returns 0 if the received packet has no payload.
// Returns -1 on error.
virtual ssize_t ReadV(absl::Span<IoVec> iovecs) = 0;

// Shuts down the channel. After the channel is shutdown, write calls will
// return -1, so no more data can be injected into the channel. Read calls
// will continue to read the remaining data in the channel, if any.
virtual void Shutdown() = 0;

// Returns a string representation for the channel.
virtual std::string ToString() const = 0;
};

inline std::ostream& operator<<(std::ostream& os, const Channel& c) {
return os << c.ToString();
}

} // namespace peregrine::internal

#endif // THIRD_PARTY_PEREGRINE_SRC_INTERNAL_CHANNEL_CHANNEL_H_
Loading
Loading